rdf4h-1.2.2: A library for RDF processing in Haskell

Safe HaskellNone



TriplesGraph contains a list-backed graph implementation suitable for smallish graphs or for temporary graphs that will not be queried. It maintains the triples in the order that they are given in, and is especially useful for holding N-Triples, where it is often desirable to preserve the order of the triples when they were originally parsed. Duplicate triples are not filtered. If you might have duplicate triples, use MGraph instead, which is also more efficient. However, the query functions of this graph (select, query) remove duplicates from their result triples (but triplesOf does not) since it is usually cheap to do so.



data TriplesGraph Source

A simple implementation of the RDF type class that represents the graph internally as a list of triples.

Note that this type of RDF is fine for interactive experimentation and querying of smallish (<10,000 triples) graphs, but there are better options for larger graphs or graphs that you will do many queries against (e.g., MGraph is faster for queries).

The time complexity of the functions (where n == num_triples) are:

empty :: RDF rdf => rdfSource

Return an empty RDF.

mkRdf :: RDF rdf => Triples -> Maybe BaseUrl -> PrefixMappings -> rdfSource

Return a RDF containing all the given triples. Handling of duplicates in the input depend on the particular RDF implementation.

triplesOf :: RDF rdf => rdf -> TriplesSource

Return all triples in the RDF, as a list.

select :: RDF rdf => rdf -> NodeSelector -> NodeSelector -> NodeSelector -> TriplesSource

Select the triples in the RDF that match the given selectors.

The three NodeSelector parameters are optional functions that match the respective subject, predicate, and object of a triple. The triples returned are those in the given graph for which the first selector returns true when called on the subject, the second selector returns true when called on the predicate, and the third selector returns true when called on the ojbect. A Nothing parameter is equivalent to a function that always returns true for the appropriate node; but implementations may be able to much more efficiently answer a select that involves a Nothing parameter rather than an (id True) parameter.

The following call illustrates the use of select, and would result in the selection of all and only the triples that have a blank node as subject and a literal node as object:

 select gr (Just isBNode) Nothing (Just isLNode)

Note: this function may be very slow; see the documentation for the particular RDF implementation for more information.

query :: RDF rdf => rdf -> Maybe Node -> Maybe Node -> Maybe Node -> TriplesSource

Return the triples in the RDF that match the given pattern, where the pattern (3 Maybe Node parameters) is interpreted as a triple pattern.

The Maybe Node params are interpreted as the subject, predicate, and object of a triple, respectively. Just n is true iff the triple has a node equal to n in the appropriate location; Nothing is always true, regardless of the node in the appropriate location.

For example, query rdf (Just n1) Nothing (Just n2) would return all and only the triples that have n1 as subject and n2 as object, regardless of the predicate of the triple.