From dd02840dcb298a63a7fadd5817a71d020786e95e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 3 Jun 2018 19:24:59 +0200 Subject: Add SerdStatement --- include/serd/serd.h | 95 +++++++++++++++++++++++++++++--- meson.build | 2 + src/reader.c | 8 ++- src/sink.c | 17 ++++-- src/statement.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++ src/statement.h | 38 +++++++++++++ src/writer.c | 14 ++--- test/meson.build | 1 + test/test_read_chunk.c | 14 ++--- test/test_reader_writer.c | 16 +++--- test/test_sink.c | 44 +++++++-------- test/test_statement.c | 121 +++++++++++++++++++++++++++++++++++++++++ 12 files changed, 441 insertions(+), 63 deletions(-) create mode 100644 src/statement.c create mode 100644 src/statement.h create mode 100644 test/test_statement.c diff --git a/include/serd/serd.h b/include/serd/serd.h index 611b0176..49cfcee4 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -893,6 +893,9 @@ serd_caret_column(const SerdCaret* SERD_NONNULL caret); @{ */ +/// A subject, predicate, and object, with optional graph context +typedef struct SerdStatementImpl SerdStatement; + /// Index of a node in a statement typedef enum { SERD_SUBJECT = 0, ///< Subject @@ -901,6 +904,81 @@ typedef enum { SERD_GRAPH = 3, ///< Graph ("context") } SerdField; +/** + Create a new statement. + + Note that, to minimise model overhead, statements do not own their nodes, so + they must have a longer lifetime than the statement for it to be valid. For + statements in models, this is the lifetime of the model. For user-created + statements, the simplest way to handle this is to use `SerdNodes`. + + @param s The subject + @param p The predicate ("key") + @param o The object ("value") + @param g The graph ("context") + @param caret Optional caret at the origin of this statement + @return A new statement that must be freed with serd_statement_free() +*/ +SERD_API +SerdStatement* SERD_ALLOCATED +serd_statement_new(const SerdNode* SERD_NONNULL s, + const SerdNode* SERD_NONNULL p, + const SerdNode* SERD_NONNULL o, + const SerdNode* SERD_NULLABLE g, + const SerdCaret* SERD_NULLABLE caret); + +/// Return a copy of `statement` +SERD_API +SerdStatement* SERD_ALLOCATED +serd_statement_copy(const SerdStatement* SERD_NULLABLE statement); + +/// Free `statement` +SERD_API +void +serd_statement_free(SerdStatement* SERD_NULLABLE statement); + +/// Return the given node of the statement +SERD_PURE_API +const SerdNode* SERD_NULLABLE +serd_statement_node(const SerdStatement* SERD_NONNULL statement, + SerdField field); + +/// Return the subject of the statement +SERD_PURE_API +const SerdNode* SERD_NONNULL +serd_statement_subject(const SerdStatement* SERD_NONNULL statement); + +/// Return the predicate of the statement +SERD_PURE_API +const SerdNode* SERD_NONNULL +serd_statement_predicate(const SerdStatement* SERD_NONNULL statement); + +/// Return the object of the statement +SERD_PURE_API +const SerdNode* SERD_NONNULL +serd_statement_object(const SerdStatement* SERD_NONNULL statement); + +/// Return the graph of the statement +SERD_PURE_API +const SerdNode* SERD_NULLABLE +serd_statement_graph(const SerdStatement* SERD_NONNULL statement); + +/// Return the source location where the statement originated, or NULL +SERD_PURE_API +const SerdCaret* SERD_NULLABLE +serd_statement_caret(const SerdStatement* SERD_NONNULL statement); + +/** + Return true iff `a` is equal to `b`, ignoring statement caret metadata. + + Only returns true if nodes are equivalent, does not perform wildcard + matching. +*/ +SERD_PURE_API +bool +serd_statement_equals(const SerdStatement* SERD_NULLABLE a, + const SerdStatement* SERD_NULLABLE b); + /** @} @defgroup serd_world World @@ -1007,12 +1085,10 @@ typedef SerdStatus (*SerdPrefixFunc)(void* SERD_NULLABLE handle, Called for every RDF statement in the serialisation. */ -typedef SerdStatus (*SerdStatementFunc)(void* SERD_NULLABLE handle, - SerdStatementFlags flags, - const SerdNode* SERD_NULLABLE graph, - const SerdNode* SERD_NONNULL subject, - const SerdNode* SERD_NONNULL predicate, - const SerdNode* SERD_NONNULL object); +typedef SerdStatus (*SerdStatementFunc)(void* SERD_NULLABLE handle, + SerdStatementFlags flags, + const SerdStatement* SERD_NONNULL + statement); /** Sink function for anonymous node end markers. @@ -1092,6 +1168,13 @@ serd_sink_write_prefix(const SerdSink* SERD_NONNULL sink, const SerdNode* SERD_NONNULL name, const SerdNode* SERD_NONNULL uri); +/// Write a statement +SERD_API +SerdStatus +serd_sink_write_statement(const SerdSink* SERD_NONNULL sink, + SerdStatementFlags flags, + const SerdStatement* SERD_NONNULL statement); + /// Write a statement from individual nodes SERD_API SerdStatus diff --git a/meson.build b/meson.build index 55894241..0f457809 100644 --- a/meson.build +++ b/meson.build @@ -93,6 +93,8 @@ sources = [ 'src/node.c', 'src/reader.c', 'src/sink.c', + 'src/statement.c', + 'src/statement.c', 'src/string.c', 'src/syntax.c', 'src/system.c', diff --git a/src/reader.c b/src/reader.c index 9957e55c..0f97d237 100644 --- a/src/reader.c +++ b/src/reader.c @@ -20,6 +20,7 @@ #include "node.h" #include "serd_internal.h" #include "stack.h" +#include "statement.h" #include "system.h" #include "world.h" @@ -127,8 +128,11 @@ emit_statement(SerdReader* const reader, (subject and predicate) were already zeroed by subsequent pushes. */ serd_node_zero_pad(o); - const SerdStatus st = serd_sink_write( - reader->sink, *ctx.flags, ctx.subject, ctx.predicate, o, ctx.graph); + const SerdStatement statement = {{ctx.subject, ctx.predicate, o, ctx.graph}, + &reader->source.caret}; + + const SerdStatus st = + serd_sink_write_statement(reader->sink, *ctx.flags, &statement); *ctx.flags &= SERD_ANON_CONT | SERD_LIST_CONT; // Preserve only cont flags return st; diff --git a/src/sink.c b/src/sink.c index 959a92b3..f78a0f18 100644 --- a/src/sink.c +++ b/src/sink.c @@ -16,6 +16,8 @@ #include "sink.h" +#include "statement.h" + #include "serd/serd.h" #include @@ -86,6 +88,15 @@ serd_sink_write_prefix(const SerdSink* sink, return sink->prefix ? sink->prefix(sink->handle, name, uri) : SERD_SUCCESS; } +SerdStatus +serd_sink_write_statement(const SerdSink* sink, + const SerdStatementFlags flags, + const SerdStatement* statement) +{ + return sink->statement ? sink->statement(sink->handle, flags, statement) + : SERD_SUCCESS; +} + SerdStatus serd_sink_write(const SerdSink* sink, const SerdStatementFlags flags, @@ -99,10 +110,8 @@ serd_sink_write(const SerdSink* sink, assert(predicate); assert(object); - return sink->statement - ? sink->statement( - sink->handle, flags, graph, subject, predicate, object) - : SERD_SUCCESS; + const SerdStatement statement = {{subject, predicate, object, graph}, NULL}; + return serd_sink_write_statement(sink, flags, &statement); } SerdStatus diff --git a/src/statement.c b/src/statement.c new file mode 100644 index 00000000..51788e05 --- /dev/null +++ b/src/statement.c @@ -0,0 +1,134 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "statement.h" + +#include "caret.h" + +#include +#include +#include +#include + +static bool +is_resource(const SerdNode* const node) +{ + const SerdNodeType type = serd_node_type(node); + + return type == SERD_URI || type == SERD_CURIE || type == SERD_BLANK; +} + +bool +serd_statement_is_valid(const SerdNode* const subject, + const SerdNode* const predicate, + const SerdNode* const object, + const SerdNode* const graph) +{ + return subject && predicate && object && is_resource(subject) && + is_resource(predicate) && serd_node_type(predicate) != SERD_BLANK && + (!graph || is_resource(graph)); +} + +SerdStatement* +serd_statement_new(const SerdNode* const s, + const SerdNode* const p, + const SerdNode* const o, + const SerdNode* const g, + const SerdCaret* const caret) +{ + assert(serd_statement_is_valid(s, p, o, g)); + + SerdStatement* statement = (SerdStatement*)malloc(sizeof(SerdStatement)); + if (statement) { + statement->nodes[0] = s; + statement->nodes[1] = p; + statement->nodes[2] = o; + statement->nodes[3] = g; + statement->caret = serd_caret_copy(caret); + } + return statement; +} + +SerdStatement* +serd_statement_copy(const SerdStatement* const statement) +{ + if (!statement) { + return NULL; + } + + SerdStatement* copy = (SerdStatement*)malloc(sizeof(SerdStatement)); + memcpy(copy, statement, sizeof(SerdStatement)); + if (statement->caret) { + copy->caret = (SerdCaret*)malloc(sizeof(SerdCaret)); + memcpy(copy->caret, statement->caret, sizeof(SerdCaret)); + } + return copy; +} + +void +serd_statement_free(SerdStatement* const statement) +{ + if (statement) { + free(statement->caret); + free(statement); + } +} + +const SerdNode* +serd_statement_node(const SerdStatement* const statement, const SerdField field) +{ + return statement->nodes[field]; +} + +const SerdNode* +serd_statement_subject(const SerdStatement* const statement) +{ + return statement->nodes[SERD_SUBJECT]; +} + +const SerdNode* +serd_statement_predicate(const SerdStatement* const statement) +{ + return statement->nodes[SERD_PREDICATE]; +} + +const SerdNode* +serd_statement_object(const SerdStatement* const statement) +{ + return statement->nodes[SERD_OBJECT]; +} + +const SerdNode* +serd_statement_graph(const SerdStatement* const statement) +{ + return statement->nodes[SERD_GRAPH]; +} + +const SerdCaret* +serd_statement_caret(const SerdStatement* const statement) +{ + return statement->caret; +} + +bool +serd_statement_equals(const SerdStatement* const a, + const SerdStatement* const b) +{ + return (a == b || (a && b && serd_node_equals(a->nodes[0], b->nodes[0]) && + serd_node_equals(a->nodes[1], b->nodes[1]) && + serd_node_equals(a->nodes[2], b->nodes[2]) && + serd_node_equals(a->nodes[3], b->nodes[3]))); +} diff --git a/src/statement.h b/src/statement.h new file mode 100644 index 00000000..d780afed --- /dev/null +++ b/src/statement.h @@ -0,0 +1,38 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef SERD_STATEMENT_H +#define SERD_STATEMENT_H + +#include "serd/serd.h" + +#include + +typedef const SerdNode* SerdQuad[4]; + +struct SerdStatementImpl { + SerdQuad nodes; + SerdCaret* caret; +}; + +SERD_PURE_FUNC +bool +serd_statement_is_valid(const SerdNode* subject, + const SerdNode* predicate, + const SerdNode* object, + const SerdNode* graph); + +#endif // SERD_STATEMENT_H diff --git a/src/writer.c b/src/writer.c index 8bbd6519..eaea9f3c 100644 --- a/src/writer.c +++ b/src/writer.c @@ -754,13 +754,15 @@ write_list_obj(SerdWriter* const writer, } static SerdStatus -serd_writer_write_statement(SerdWriter* const writer, - const SerdStatementFlags flags, - const SerdNode* const graph, - const SerdNode* const subject, - const SerdNode* const predicate, - const SerdNode* const object) +serd_writer_write_statement(SerdWriter* const writer, + const SerdStatementFlags flags, + const SerdStatement* const statement) { + const SerdNode* const subject = serd_statement_subject(statement); + const SerdNode* const predicate = serd_statement_predicate(statement); + const SerdNode* const object = serd_statement_object(statement); + const SerdNode* const graph = serd_statement_graph(statement); + if (!is_resource(subject) || !is_resource(predicate) || !object) { return SERD_ERR_BAD_ARG; } diff --git a/test/meson.build b/test/meson.build index 692bafe4..57163f23 100644 --- a/test/meson.build +++ b/test/meson.build @@ -13,6 +13,7 @@ unit_tests = [ 'read_chunk', 'reader_writer', 'sink', + 'statement', 'string', 'uri', 'world', diff --git a/test/test_read_chunk.c b/test/test_read_chunk.c index db2c210a..f5ba8d93 100644 --- a/test/test_read_chunk.c +++ b/test/test_read_chunk.c @@ -48,19 +48,13 @@ on_prefix(void* handle, const SerdNode* name, const SerdNode* uri) } static SerdStatus -on_statement(void* const handle, - const SerdStatementFlags flags, - const SerdNode* const graph, - const SerdNode* const subject, - const SerdNode* const predicate, - const SerdNode* const object) +on_statement(void* const handle, + const SerdStatementFlags flags, + const SerdStatement* const statement) { (void)handle; (void)flags; - (void)graph; - (void)subject; - (void)predicate; - (void)object; + (void)statement; ++n_statement; return SERD_SUCCESS; diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 9a592c3a..bbec9377 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -25,18 +25,12 @@ #include static SerdStatus -test_sink(void* handle, - SerdStatementFlags flags, - const SerdNode* graph, - const SerdNode* subject, - const SerdNode* predicate, - const SerdNode* object) +test_sink(void* handle, + SerdStatementFlags flags, + const SerdStatement* statement) { (void)flags; - (void)subject; - (void)predicate; - (void)object; - (void)graph; + (void)statement; ++*(size_t*)handle; @@ -163,6 +157,8 @@ test_read_string(void) serd_sink_set_statement_func(sink, test_sink); + serd_sink_set_statement_func(sink, test_sink); + // Test reading a string that ends exactly at the end of input (no newline) assert( !serd_reader_start_string(reader, diff --git a/test/test_sink.c b/test/test_sink.c index 982fc1c9..9f029a10 100644 --- a/test/test_sink.c +++ b/test/test_sink.c @@ -26,15 +26,12 @@ #define NS_EG "http://example.org/" typedef struct { - const SerdNode* last_base; - const SerdNode* last_name; - const SerdNode* last_namespace; - const SerdNode* last_end; - const SerdNode* last_subject; - const SerdNode* last_predicate; - const SerdNode* last_object; - const SerdNode* last_graph; - SerdStatus return_status; + const SerdNode* last_base; + const SerdNode* last_name; + const SerdNode* last_namespace; + const SerdNode* last_end; + const SerdStatement* last_statement; + SerdStatus return_status; } State; static SerdStatus @@ -57,21 +54,15 @@ on_prefix(void* handle, const SerdNode* name, const SerdNode* uri) } static SerdStatus -on_statement(void* handle, - SerdStatementFlags flags, - const SerdNode* const graph, - const SerdNode* const subject, - const SerdNode* const predicate, - const SerdNode* const object) +on_statement(void* handle, + SerdStatementFlags flags, + const SerdStatement* const statement) { (void)flags; State* state = (State*)handle; - state->last_subject = subject; - state->last_predicate = predicate; - state->last_object = object; - state->last_graph = graph; + state->last_statement = statement; return state->return_status; } @@ -93,13 +84,17 @@ test_callbacks(void) SerdNode* const uri = serd_new_uri(SERD_STRING(NS_EG "uri")); SerdNode* const blank = serd_new_blank(SERD_STRING("b1")); SerdEnv* env = serd_env_new(serd_node_string_view(base)); - State state = {0, 0, 0, 0, 0, 0, 0, 0, SERD_SUCCESS}; + State state = {0, 0, 0, 0, 0, SERD_SUCCESS}; + + SerdStatement* const statement = + serd_statement_new(base, uri, blank, NULL, NULL); // Call functions on a sink with no functions set SerdSink* null_sink = serd_sink_new(&state, NULL); assert(!serd_sink_write_base(null_sink, base)); assert(!serd_sink_write_prefix(null_sink, name, uri)); + assert(!serd_sink_write_statement(null_sink, 0, statement)); assert(!serd_sink_write(null_sink, 0, base, uri, blank, NULL)); assert(!serd_sink_write_end(null_sink, blank)); serd_sink_free(null_sink); @@ -119,16 +114,15 @@ test_callbacks(void) assert(serd_node_equals(state.last_name, name)); assert(serd_node_equals(state.last_namespace, uri)); - assert(!serd_sink_write(sink, 0, base, uri, blank, NULL)); - assert(serd_node_equals(state.last_subject, base)); - assert(serd_node_equals(state.last_predicate, uri)); - assert(serd_node_equals(state.last_object, blank)); - assert(!state.last_graph); + assert(!serd_sink_write_statement(sink, 0, statement)); + assert(serd_statement_equals(state.last_statement, statement)); assert(!serd_sink_write_end(sink, blank)); assert(serd_node_equals(state.last_end, blank)); serd_sink_free(sink); + + serd_statement_free(statement); serd_env_free(env); serd_node_free(blank); serd_node_free(uri); diff --git a/test/test_statement.c b/test/test_statement.c new file mode 100644 index 00000000..e4219b68 --- /dev/null +++ b/test/test_statement.c @@ -0,0 +1,121 @@ +/* + Copyright 2011-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#undef NDEBUG + +#include "serd/serd.h" + +#include +#include + +#define NS_EG "http://example.org/" + +static void +test_copy(void) +{ + assert(!serd_statement_copy(NULL)); + + SerdNode* const f = serd_new_string(SERD_STRING("file")); + SerdNode* const s = serd_new_uri(SERD_STRING(NS_EG "s")); + SerdNode* const p = serd_new_uri(SERD_STRING(NS_EG "p")); + SerdNode* const o = serd_new_uri(SERD_STRING(NS_EG "o")); + SerdNode* const g = serd_new_uri(SERD_STRING(NS_EG "g")); + + SerdCaret* const caret = serd_caret_new(f, 1, 1); + SerdStatement* const statement = serd_statement_new(s, p, o, g, caret); + SerdStatement* const copy = serd_statement_copy(statement); + + assert(serd_statement_equals(copy, statement)); + assert(serd_caret_equals(serd_statement_caret(copy), caret)); + + serd_statement_free(copy); + serd_caret_free(caret); + serd_statement_free(statement); + serd_node_free(g); + serd_node_free(o); + serd_node_free(p); + serd_node_free(s); + serd_node_free(f); +} + +static void +test_free(void) +{ + serd_statement_free(NULL); +} + +static void +test_fields(void) +{ + SerdNode* const f = serd_new_string(SERD_STRING("file")); + SerdNode* const s = serd_new_uri(SERD_STRING(NS_EG "s")); + SerdNode* const p = serd_new_uri(SERD_STRING(NS_EG "p")); + SerdNode* const o = serd_new_uri(SERD_STRING(NS_EG "o")); + SerdNode* const g = serd_new_uri(SERD_STRING(NS_EG "g")); + + SerdCaret* const caret = serd_caret_new(f, 1, 1); + SerdStatement* const statement = serd_statement_new(s, p, o, g, caret); + + assert(serd_statement_equals(statement, statement)); + assert(!serd_statement_equals(statement, NULL)); + assert(!serd_statement_equals(NULL, statement)); + + assert(serd_statement_node(statement, SERD_SUBJECT) == s); + assert(serd_statement_node(statement, SERD_PREDICATE) == p); + assert(serd_statement_node(statement, SERD_OBJECT) == o); + assert(serd_statement_node(statement, SERD_GRAPH) == g); + + assert(serd_statement_subject(statement) == s); + assert(serd_statement_predicate(statement) == p); + assert(serd_statement_object(statement) == o); + assert(serd_statement_graph(statement) == g); + assert(serd_statement_caret(statement) != caret); + assert(serd_caret_equals(serd_statement_caret(statement), caret)); + + SerdStatement* const diff_s = serd_statement_new(o, p, o, g, caret); + assert(!serd_statement_equals(statement, diff_s)); + serd_statement_free(diff_s); + + SerdStatement* const diff_p = serd_statement_new(s, o, o, g, caret); + assert(!serd_statement_equals(statement, diff_p)); + serd_statement_free(diff_p); + + SerdStatement* const diff_o = serd_statement_new(s, p, s, g, caret); + assert(!serd_statement_equals(statement, diff_o)); + serd_statement_free(diff_o); + + SerdStatement* const diff_g = serd_statement_new(s, p, o, s, caret); + assert(!serd_statement_equals(statement, diff_g)); + serd_statement_free(diff_g); + + serd_statement_free(statement); + serd_caret_free(caret); + serd_node_free(g); + serd_node_free(o); + serd_node_free(p); + serd_node_free(s); + serd_node_free(f); +} + +int +main(void) +{ + test_copy(); + test_free(); + test_fields(); + + return 0; +} -- cgit v1.2.1