title: SPARQL queries and user defined vocabulary
body: <p>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:</p>
  <pre><code>  1 from rdflib.term import URIRef
    2 from rdflib.namespace import DefinedNamespace, Namespace
    3
    4
    5 class ODE(DefinedNamespace):
    6     &quot;&quot;&quot;          
    7     DESCRIPTION_EDIT_ME_!
    8
    9     Generated from: SOURCE_RDF_FILE_EDIT_ME_!
   10     Date: 2022-05-02 08:38:55.619901
   11     &quot;&quot;&quot;              
   12
   13     _fail = True
   14
   15     Function: URIRef
   16     Equation: URIRef
   17     hasDerivative: URIRef
   18     Polynomial: URIRef    
   19     Ode: URIRef
   20
   21     _NS = Namespace(&quot;ode#&quot;)
   22
  </code></pre>
  <p>As all the new &quot;classes&quot; of the ODE vocabulary are a specialization of the class &quot;Seq&quot; I have created the module rdflib/ode.py:</p>
  <pre><code>  1 from rdflib import Seq
    2 from rdflib.namespace import RDF,ODE,MATH
    3
    4 __all__ = [&quot;Function&quot;, &quot;Equation&quot;,&quot;Polynomial&quot;,&quot;Ode&quot;]
    5
    6
    7 class Ode(Seq):
    8     def __init__(self, graph, uri, seq=[], rtype=&quot;Ode&quot;):
    9         &quot;&quot;&quot;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 &quot;Bag&quot;, &quot;Seq&quot; or &quot;Alt&quot;
   15         &quot;&quot;&quot;
   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, &quot;Function&quot;)
   30
   31
   32 class Equation(Ode):
   33     def __init__(self, graph, uri, seq=[]):
   34         Ode.__init__(self, graph, uri, seq, &quot;Equation&quot;)
   35
   36 class Polynomial(Ode):
   37     def __init__(self, graph, uri, seq=[]):
   38         Ode.__init__(self, graph, uri, seq, &quot;Polynomial&quot;)
  </code></pre>
  <p>With these two classes I can generate a RDF file in a declarative way.
  For example we can create the Function c(t):</p>
  <pre><code>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(&quot;t&quot;)
  6  graph.add((t,RDFS.label,Literal(&quot;t&quot;)))
  7
  8  c_of_t_label = BNode(&quot;c&quot;)
  9  graph.add((c_of_t_label,RDFS.label,Literal(&quot;c&quot;)))
  10 c_of_t_bn = BNode(&quot;c_of_t&quot;)
  11
  12 Function(graph,c_of_t_bn,[c_of_t_label,t])
  </code></pre>
  <p>And we obtain the following RDF:</p>
  <pre><code>_:c rdfs:label &quot;c&quot; .
  _:t rdfs:label &quot;t&quot; .
  _:c_of_t a ode:Function ;
      rdf:_1 _:c ;
      rdf:_2 _:t .
  </code></pre>
  <p>So far, so good. Now I want to execute a SPARQL query on this rdf to retrieve the function.</p>
  <pre><code>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(&quot;ode_spe&quot;, format=&quot;ttl&quot;)
  9    
  10    
  11     function = ODE.Function
  12    
  13     query_test= &quot;SELECT ?e WHERE {?e rdf:type ode:Function . }&quot;
  14     qres = g.query(query_test)
  15    
  16     print (len(qres))
  17 if __name__ == &quot;__main__&quot;:
  18     main()
  </code></pre>
  <p>But I have no results.
  I probably do not do the right thing with ode:Function.</p>
  <p>I have two questions:</p>
  <ul>
  <li>Is it the right way to add a user defined vocabulary ?</li>
  <li>And what can I do to retrieve the function with a SPARQL query</li>
  </ul>
  <p>Thank for your help.</p>
  <p>Olivier</p>