Hi there, I am trying to play with rdflib and a (my) user defined vocabulary (name: ODE). To do that I have generated a class namespace/_ODE.py derived from DefinedNamespace: 1 from rdflib.term import URIRef 2 from rdflib.namespace import DefinedNamespace, Namespace 3 4 5 class ODE(DefinedNamespace): 6 """ 7 DESCRIPTION_EDIT_ME_! 8 9 Generated from: SOURCE_RDF_FILE_EDIT_ME_! 10 Date: 2022-05-02 08:38:55.619901 11 """ 12 13 _fail = True 14 15 Function: URIRef 16 Equation: URIRef 17 hasDerivative: URIRef 18 Polynomial: URIRef 19 Ode: URIRef 20 21 _NS = Namespace("ode#") 22
As all the new "classes" of the ODE vocabulary are a specialization of the class "Seq" I have created the module rdflib/ode.py: 1 from rdflib import Seq 2 from rdflib.namespace import RDF,ODE,MATH 3 4 __all__ = ["Function", "Equation","Polynomial","Ode"] 5 6 7 class Ode(Seq): 8 def __init__(self, graph, uri, seq=[], rtype="Ode"): 9 """Creates a Container 10 11 :param graph: a Graph instance 12 :param uri: URI or Blank Node of the Container 13 :param seq: the elements of the Container 14 :param rtype: the type of Container, one of "Bag", "Seq" or "Alt" 15 """ 16 17 self.graph = graph 18 self.uri = uri or BNode() 19 self._len = 0 20 self._rtype = rtype # rdf:Bag or rdf:Seq or rdf:Alt 21 22 self.append_multiple(seq) 23 24 # adding triple corresponding to container type 25 self.graph.add((self.uri, RDF.type, ODE[self._rtype])) 26 27 class Function(Ode): 28 def __init__(self, graph, uri, seq=[]): 29 Ode.__init__(self, graph, uri, seq, "Function") 30 31 32 class Equation(Ode): 33 def __init__(self, graph, uri, seq=[]): 34 Ode.__init__(self, graph, uri, seq, "Equation") 35 36 class Polynomial(Ode): 37 def __init__(self, graph, uri, seq=[]): 38 Ode.__init__(self, graph, uri, seq, "Polynomial") With these two classes I can generate a RDF file in a declarative way. For example we can create the Function c(t): 1 from rdflib import Graph, URIRef, RDF, BNode, RDFS, Literal, Seq, Bag, Function, Equation, Times, Minus, Polynomial, Ode 2 from rdflib.namespace import ODE, MATH 3 4 # the time t 5 t = BNode("t") 6 graph.add((t,RDFS.label,Literal("t"))) 7 8 c_of_t_label = BNode("c") 9 graph.add((c_of_t_label,RDFS.label,Literal("c"))) 10 c_of_t_bn = BNode("c_of_t") 11 12 Function(graph,c_of_t_bn,[c_of_t_label,t]) And we obtain the following RDF: _:c rdfs:label "c" . _:t rdfs:label "t" . _:c_of_t a ode:Function ; rdf:_1 _:c ; rdf:_2 _:t . So far, so good. Now I want to execute a SPARQL query on this rdf to retrieve the function. 1 import rdflib 2 3 from rdflib import Graph, URIRef, RDF, BNode, RDFS, Literal, Seq, Bag, Function, Equation, Times, Minus, Polynomial, Ode 4 from rdflib.namespace import ODE, MATH 5 6 def main(): 7 g = rdflib.Graph() 8 g.parse("ode_spe", format="ttl") 9 10 11 function = ODE.Function 12 13 query_test= "SELECT ?e WHERE {?e rdf:type ode:Function . }" 14 qres = g.query(query_test) 15 16 print (len(qres)) 17 if __name__ == "__main__": 18 main() But I have no results. I probably do not do the right thing with ode:Function. I have two questions: - Is it the right way to add a user defined vocabulary ? - And what can I do to retrieve the function with a SPARQL query Thank for your help. Olivier -- http://github.com/RDFLib --- You received this message because you are subscribed to the Google Groups "rdflib-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rdflib-dev/83af5eda-d28c-479e-b9e3-04fb54f072a2n%40googlegroups.com.