Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
term.h
Go to the documentation of this file.
1#ifndef VOLK_TERM_H
2#define VOLK_TERM_H
3
4//#include <assert.h>
5
6#include "volksdata/buffer.h"
8
13
14#define UUID4_URN_SIZE UUIDSTR_SIZE + 10
15
16// Some common RDF term values.
17#define VOLK_RDF_TYPE "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
18#define VOLK_RDF_TYPE_NS "rdf:type"
20#define DEFAULT_DTYPE "http://www.w3.org/2001/XMLSchema#string"
21#define DEFAULT_DTYPE_NS "xsd:string"
22
23/*
24 * Data types.
25 */
26
28typedef char VOLK_LangTag[8];
29
40
42typedef struct match_coord_t {
43 unsigned int offset;
44 unsigned int size;
46
57
59typedef struct link_map_iter VOLK_LinkMapIterator;
60
62typedef struct term_t {
63 char * data;
64 union {
65 struct term_t * datatype;
68 void * metadata;
69 };
71} VOLK_Term;
72
73
76#define VOLK_IS_LITERAL(term) \
77 ((term)->type == VOLK_TERM_LITERAL || (term)->type == VOLK_TERM_LT_LITERAL)
78
79
86typedef struct triple_t {
91
92
99
100
117typedef struct link_map VOLK_LinkMap;
118
119
124typedef struct hashmap VOLK_TermSet;
125
126
127/*
128 * External variables.
129 */
130
133extern uint32_t VOLK_default_dtype_key;
134
141
147
150
151
152/*
153 * API functions.
154 */
155
174VOLK_Term *
175VOLK_term_new (VOLK_TermType type, const char *data, void *metadata);
176
177
180#define TERM_DUMMY VOLK_term_new (VOLK_TERM_UNDEFINED, NULL, NULL)
181
182
191inline VOLK_Term *
192VOLK_iriref_new (const char *data)
193{ return VOLK_term_new (VOLK_TERM_IRIREF, data, NULL); }
194
195
204inline VOLK_Term *
205VOLK_iriref_new_ns (const char *data)
206{
207 char *fquri;
208 RCNL (VOLK_nsmap_normalize_uri (data, &fquri));
209
210 VOLK_Term *t = VOLK_term_new (VOLK_TERM_IRIREF, fquri, NULL);
211 free (fquri);
212
213 return t;
214}
215
216
234VOLK_Term *
235VOLK_iriref_new_abs (const VOLK_Term *root, const VOLK_Term *iri);
236
237
250VOLK_Term *
251VOLK_iriref_new_rel (const VOLK_Term *root, const VOLK_Term *iri);
252
253
265inline VOLK_Term *
266VOLK_literal_new (const char *data, VOLK_Term *datatype)
267{ return VOLK_term_new (VOLK_TERM_LITERAL, data, datatype); }
268
269
303VOLK_parse_iri (char *iri_str, VOLK_IRIInfo *iri_info);
304
305
316inline VOLK_Term *
317VOLK_lt_literal_new (const char *data, char *lang)
318{ return VOLK_term_new (VOLK_TERM_LT_LITERAL, data, lang); }
319
320
330inline VOLK_Term *
331VOLK_bnode_new (const char *data)
332{ return VOLK_term_new (VOLK_TERM_BNODE, data, NULL); }
333
334
341VOLK_Term *
342VOLK_term_copy (const VOLK_Term *src);
343
344
352VOLK_Term *
354
355
363VOLK_term_serialize (const VOLK_Term *term);
364
365
369VOLK_term_hash (const VOLK_Term *term);
370
371
377inline bool VOLK_term_equals (const VOLK_Term *term1, const VOLK_Term *term2)
378{ return term1 == term2 || VOLK_term_hash (term1) == VOLK_term_hash (term2); }
379
380
381void
383
384
392char *
393VOLK_iriref_prefix (const VOLK_Term *iri);
394
395
404char *
405VOLK_iriref_path (const VOLK_Term *iri);
406
407
415char *
416VOLK_iriref_frag (const VOLK_Term *iri);
417
418/*
419 * TRIPLES
420 */
421
438
439
442#define TRP_DUMMY VOLK_triple_new (NULL, NULL, NULL)
443
444
447
448
451
452
467
468
473void
475
476
484void
486
487
498inline VOLK_Term *
500{
501 if (n == TRP_POS_S) return trp->s;
502 if (n == TRP_POS_P) return trp->p;
503 if (n == TRP_POS_O) return trp->o;
504 return NULL;
505}
506
507
512inline VOLK_Key
514{
516 VOLK_Key hash = VOLK_btriple_hash (strp);
517 VOLK_btriple_free (strp);
518
519 return hash;
520}
521
522
528VOLK_term_set_new (void);
529
530
535void
537
538
559VOLK_term_set_add (VOLK_TermSet *ts, VOLK_Term *term, VOLK_Term **existing);
560
561
571const VOLK_Term *
573
574
588VOLK_term_set_next (VOLK_TermSet *ts, size_t *i, VOLK_Term **term);
589
590
597size_t
599
600
612VOLK_link_map_new (const VOLK_Term *linked_term, VOLK_LinkType type);
613
614
621void
623
624
631
632
654 VOLK_LinkMap *lmap, VOLK_Term *term, VOLK_TermSet *tset);
655
656
661VOLK_LinkMapIterator *
663
664
680 VOLK_LinkMapIterator *it, VOLK_Term **lt, VOLK_TermSet **ts);
681
682
684void
685VOLK_link_map_iter_free (VOLK_LinkMapIterator *it);
686
687
705VOLK_link_map_triples (VOLK_LinkMapIterator *it, VOLK_Triple *spo);
706
708
709#endif // VOLK_TERM_H
VOLK_Key VOLK_btriple_hash(const VOLK_BufferTriple *strp)
Hash a buffer triple.
Definition buffer.h:317
void VOLK_btriple_free(VOLK_BufferTriple *sspo)
Free a buffer triple and all its internal pointers.
Definition buffer.c:147
VOLK_TriplePos
Triple position of s, p, o.
Definition buffer.h:19
@ TRP_POS_O
Definition buffer.h:22
@ TRP_POS_S
Definition buffer.h:20
@ TRP_POS_P
Definition buffer.h:21
VOLK_rc VOLK_nsmap_normalize_uri(const char *pfx_uri, char **fq_uri_p)
Convert a namespace-prefixed string to a FQ URI sring if mapped.
Definition namespace.c:110
#define RCNL(exp)
Return NULL if exp returns a nonzero value.
Definition core.h:345
size_t VOLK_Key
Term key, i.e., hash of a serialized term.
Definition core.h:230
int VOLK_rc
Definition core.h:79
VOLK_Key VOLK_triple_hash(const VOLK_Triple *trp)
Hash a triple.
Definition term.h:513
VOLK_Term * VOLK_lt_literal_new(const char *data, char *lang)
Shortcut to create a language-tagged literal term.
Definition term.h:317
VOLK_rc VOLK_term_set_add(VOLK_TermSet *ts, VOLK_Term *term, VOLK_Term **existing)
Add term to a term set.
Definition term.c:562
VOLK_Triple * VOLK_triple_new_from_btriple(const VOLK_BufferTriple *sspo)
Definition term.c:471
struct hashmap VOLK_TermSet
a set of unique terms.
Definition term.h:124
VOLK_rc VOLK_term_set_next(VOLK_TermSet *ts, size_t *i, VOLK_Term **term)
Iterate trough a term set.
Definition term.c:592
VOLK_Triple * VOLK_triple_new(VOLK_Term *s, VOLK_Term *p, VOLK_Term *o)
Create a new triple from three terms.
Definition term.c:456
char * VOLK_iriref_frag(const VOLK_Term *iri)
Get the fragment portion of a IRI ref.
Definition term.c:436
VOLK_Term * VOLK_iriref_new_rel(const VOLK_Term *root, const VOLK_Term *iri)
Create a new relative IRI from an absolute IRI and a web root IRI.
Definition term.c:273
VOLK_rc VOLK_parse_iri(char *iri_str, VOLK_IRIInfo *iri_info)
scan an IRI string and parse IRI parts.
Definition term.c:887
void VOLK_link_map_iter_free(VOLK_LinkMapIterator *it)
Free a link map iterator.
Definition term.c:707
VOLK_LinkType
Link type.
Definition term.h:94
VOLK_Term * VOLK_iriref_new(const char *data)
Create an IRI reference.
Definition term.h:192
VOLK_TermType
Term type.
Definition term.h:31
bool VOLK_term_equals(const VOLK_Term *term1, const VOLK_Term *term2)
Compare two terms.
Definition term.h:377
size_t VOLK_term_set_size(VOLK_TermSet *ts)
Size of a term set.
Definition term.c:612
VOLK_Term * VOLK_iriref_new_abs(const VOLK_Term *root, const VOLK_Term *iri)
Create a new absolute IRI from a path relative to a root IRI.
Definition term.c:231
VOLK_Term * VOLK_literal_new(const char *data, VOLK_Term *datatype)
Shortcut to create a literal term.
Definition term.h:266
VOLK_LinkMapIterator * VOLK_link_map_iter_new(const VOLK_LinkMap *lmap)
Create a new iterator to loop through a link map.
Definition term.c:695
char * VOLK_iriref_prefix(const VOLK_Term *iri)
Get the prefix portion of a IRI ref.
Definition term.c:403
VOLK_LinkMap * VOLK_link_map_new(const VOLK_Term *linked_term, VOLK_LinkType type)
New link map.
Definition term.c:617
VOLK_Term * VOLK_default_datatype
Default literal data type URI.
Definition term.c:60
VOLK_rc VOLK_link_map_triples(VOLK_LinkMapIterator *it, VOLK_Triple *spo)
Iterate over a link map and generate triples.
Definition term.c:726
void VOLK_triple_done(VOLK_Triple *spo)
Free the internal pointers of a triple.
Definition term.c:521
VOLK_rc VOLK_triple_init(VOLK_Triple *spo, VOLK_Term *s, VOLK_Term *p, VOLK_Term *o)
Initialize internal term pointers in a heap-allocated triple.
Definition term.c:499
const VOLK_Term * VOLK_term_set_get(VOLK_TermSet *ts, VOLK_Key key)
Get a term from a term set.
Definition term.c:581
VOLK_Term * VOLK_bnode_new(const char *data)
Shortcut to create a blank node.
Definition term.h:331
VOLK_TermSet * VOLK_term_set_new(void)
Create a new term set.
Definition term.c:549
uint32_t VOLK_default_dtype_key
Compiled hash of default literal data type.
Definition term.c:58
void VOLK_term_set_free(VOLK_TermSet *ts)
Free a term set.
Definition term.c:604
VOLK_Key VOLK_term_hash(const VOLK_Term *term)
Hash a buffer.
Definition term.c:371
VOLK_rc VOLK_link_map_next(VOLK_LinkMapIterator *it, VOLK_Term **lt, VOLK_TermSet **ts)
Iterate through a link map.
Definition term.c:711
void VOLK_link_map_free(VOLK_LinkMap *lm)
Free a link map.
Definition term.c:637
VOLK_Term * VOLK_iriref_new_ns(const char *data)
Create an IRI reference from a namespace-prefixed string.
Definition term.h:205
char * VOLK_iriref_path(const VOLK_Term *iri)
Get the path portion of a IRI ref.
Definition term.c:420
char VOLK_LangTag[8]
Language tag, currently restricted to 7 characters.
Definition term.h:28
void VOLK_term_free(VOLK_Term *term)
Definition term.c:387
VOLK_Term * VOLK_triple_pos(const VOLK_Triple *trp, const VOLK_TriplePos n)
Get triple by term position.
Definition term.h:499
VOLK_Buffer * VOLK_term_serialize(const VOLK_Term *term)
Serialize a term into a buffer.
Definition term.c:293
VOLK_Term * VOLK_default_ctx
Default context.
Definition term.c:59
VOLK_Term * VOLK_term_new_from_buffer(const VOLK_Buffer *sterm)
Deserialize a buffer into a term.
Definition term.c:189
VOLK_rc VOLK_link_map_add(VOLK_LinkMap *lmap, VOLK_Term *term, VOLK_TermSet *tset)
Add a term - term set pair to a link map.
Definition term.c:652
VOLK_LinkType VOLK_link_map_type(const VOLK_LinkMap *map)
Return the link map type.
Definition term.c:646
VOLK_BufferTriple * VOLK_triple_serialize(const VOLK_Triple *spo)
Definition term.c:485
VOLK_Term * VOLK_term_new(VOLK_TermType type, const char *data, void *metadata)
Create a new term.
Definition term.c:157
VOLK_TermSet * VOLK_term_cache
Global term cache.
Definition term.c:61
void VOLK_triple_free(VOLK_Triple *spo)
Free a triple and all its internal pointers.
Definition term.c:532
VOLK_Term * VOLK_term_copy(const VOLK_Term *src)
Copy a term.
Definition term.c:174
@ VOLK_LINK_EDGE
Edge link (so).
Definition term.h:97
@ VOLK_LINK_INBOUND
Inbound link (sp).
Definition term.h:95
@ VOLK_LINK_OUTBOUND
Outbound link (po).
Definition term.h:96
@ VOLK_TERM_IRIREF
IRI reference.
Definition term.h:35
@ VOLK_TERM_UNDEFINED
Definition term.h:32
@ VOLK_TERM_LT_LITERAL
Language-tagged string literal.
Definition term.h:37
@ VOLK_TERM_LITERAL
Literal without language tag.
Definition term.h:36
@ VOLK_TERM_BNODE
Blank node.
Definition term.h:38
Match coordinates in IRI parsing results.
Definition term.h:42
unsigned int size
Length of match.
Definition term.h:44
unsigned int offset
Offset of match from start of string.
Definition term.h:43
Triple of byte buffers.
Definition buffer.h:60
General-purpose data buffer.
Definition buffer.h:47
Matching sub-patterns for IRI parts.
Definition term.h:48
MatchCoord frag
Fragment (frag).
Definition term.h:55
MatchCoord auth
Authority (example.org).
Definition term.h:51
MatchCoord query
Query (query=blah).
Definition term.h:54
MatchCoord prefix
Prefix (http://example.org).
Definition term.h:49
MatchCoord path
Definition term.h:52
MatchCoord scheme
Scheme (http).
Definition term.h:50
RDF term.
Definition term.h:62
VOLK_Key bnode_id
BN ID for comparison & skolemization.
Definition term.h:67
void * metadata
Generic metadata pointer.
Definition term.h:68
char * data
URI, literal value, or BNode label.
Definition term.h:63
struct term_t * datatype
Data type IRI for VOLK_TERM_LITERAL.
Definition term.h:65
VOLK_TermType type
Term type.
Definition term.h:70
VOLK_LangTag lang
Lang tag for VOLK_TERM_LT_LITERAL.
Definition term.h:66
RDF triple.
Definition term.h:86
VOLK_Term * p
Predicate.
Definition term.h:88
VOLK_Term * s
Subject.
Definition term.h:87
VOLK_Term * o
Object.
Definition term.h:89