From 5cd549c8e512d9a85d7d666db5da8b8a444a281f Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 14 Jan 2013 06:53:05 +0000 Subject: Add sord_node_get() for easily getting single property values. git-svn-id: http://svn.drobilla.net/sord/trunk@280 3d64ff67-21c5-427c-a301-fe4f08042e5a --- NEWS | 3 ++- sord/sord.h | 13 +++++++++++++ sord/sordmm.hpp | 16 ++++++++++++++++ src/sord.c | 25 +++++++++++++++++++++++++ src/sord_test.c | 16 ++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index a442b33..fe6c359 100644 --- a/NEWS +++ b/NEWS @@ -1,11 +1,12 @@ sord (0.10.5) unstable; * Update to waf 1.7.8 and autowaf r90 (install docs to versioned directory) + * Add sord_node_get() for easily getting single property values * sordmm.hpp: Add convenient constructors for decimal and integer literals * sordmm.hpp: Add Node::to_serd_node() * sordmm.hpp: Don't automatically add RDF namespace prefix to world - -- David Robillard Mon, 14 Jan 2013 01:24:56 -0500 + -- David Robillard Mon, 14 Jan 2013 01:52:25 -0500 sord (0.10.4) stable; diff --git a/sord/sord.h b/sord/sord.h index 8f1ef38..ee49764 100644 --- a/sord/sord.h +++ b/sord/sord.h @@ -397,6 +397,19 @@ sord_search(SordModel* model, const SordNode* p, const SordNode* o, const SordNode* g); +/** + Search for a single node that matches a pattern. + Exactly one of @p s, @p p, @p o must be NULL. + This function is mainly useful for predicates that only have one value. + @return the first matching node, or NULL if no matches are found. +*/ +SORD_API +const SordNode* +sord_get(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g); /** Return true iff a statement exists. diff --git a/sord/sordmm.hpp b/sord/sordmm.hpp index 4ab25e2..da1e49c 100644 --- a/sord/sordmm.hpp +++ b/sord/sordmm.hpp @@ -466,6 +466,10 @@ public: const Node& predicate, const Node& object); + inline Node get(const Node& subject, + const Node& predicate, + const Node& object); + inline World& world() const { return _world; } private: @@ -631,6 +635,18 @@ Model::find(const Node& subject, return Iter(_world, sord_find(_c_obj, quad)); } +inline Node +Model::get(const Node& subject, + const Node& predicate, + const Node& object) +{ + return Node(_world, sord_get(_c_obj, + subject.c_obj(), + predicate.c_obj(), + object.c_obj(), + NULL)); +} + } // namespace Sord #endif // SORD_SORDMM_HPP diff --git a/src/sord.c b/src/sord.c index 9510d50..cad3c4d 100644 --- a/src/sord.c +++ b/src/sord.c @@ -844,6 +844,31 @@ sord_search(SordModel* model, return sord_find(model, pat); } +const SordNode* +sord_get(SordModel* model, + const SordNode* s, + const SordNode* p, + const SordNode* o, + const SordNode* g) +{ + if ((bool)s + (bool)p + (bool)o != 2) { + return NULL; + } + + SordIter* i = sord_search(model, s, p, o, g); + const SordNode* ret = NULL; + if (!s) { + ret = sord_iter_get_node(i, SORD_SUBJECT); + } else if (!p) { + ret = sord_iter_get_node(i, SORD_PREDICATE); + } else if (!o) { + ret = sord_iter_get_node(i, SORD_OBJECT); + } + + sord_iter_free(i); + return ret; +} + bool sord_ask(SordModel* model, const SordNode* s, diff --git a/src/sord_test.c b/src/sord_test.c index a7a74e5..36fc408 100644 --- a/src/sord_test.c +++ b/src/sord_test.c @@ -238,6 +238,22 @@ test_read(SordWorld* world, SordModel* sord, SordNode* g, TUP_FMT_ARGS(nomatch)); } + if (sord_get(sord, NULL, NULL, uri(world, 3), g)) { + return test_fail("Fail: Get *,*,3 succeeded\n"); + } else if (!sord_node_equals( + sord_get(sord, uri(world, 1), uri(world, 2), NULL, g), + uri(world, 3))) { + return test_fail("Fail: Get 1,2,* != 3\n"); + } else if (!sord_node_equals( + sord_get(sord, uri(world, 1), NULL, uri(world, 3), g), + uri(world, 2))) { + return test_fail("Fail: Get 1,*,3 != 2\n"); + } else if (!sord_node_equals( + sord_get(sord, NULL, uri(world, 2), uri(world, 3), g), + uri(world, 1))) { + return test_fail("Fail: Get *,2,3 != 1\n"); + } + for (unsigned i = 0; i < NUM_PATTERNS; ++i) { QueryTest test = patterns[i]; SordQuad pat = { test.query[0], test.query[1], test.query[2], g }; -- cgit v1.2.1