1

This is What I wrote after importing rdflib and my owl data. I wrote 3 query part, and I couldnt take any answer from all. *

PREFIX : "<http://xmlns.com/foaf/0.1/>"
    g = Graph()
    g.bind("foaf", FOAF)

First one:

 qres = g.query(
            """SELECT DISTINCT ?Name ?Microsoft
               WHERE {
                  ?a foaf:subclassof ?Microsoft .
                  ?a foaf:Name ?Name .
                  ?b foaf:Microsoft ?Microsoft .
               }""")
        for row in qres:
            print("%s subclassof %s" % row)
       
     from rdflib.namespace import FOAF

Second One:

 qres = g.query('SELECT * WHERE { ?p a foaf:Engineers}', initNs={ 'foaf': FOAF })
    for row in qres:
        print("%s" % row)
 Third One:


qres = g.query(
        """SELECT DISTINCT ?aName ?bOracle
       WHERE {
          ?a foaf:individual_of ?b .
          ?a foaf:Name ?aName.
          ?b foaf:Oracle ?bOracle .
       }""")
for row in qres:
    print("%s individual_of %s" % row)
GamzeGG
  • 11
  • 3
  • the error message is absolutely clear ... *"unknown namespace prefix : foaf"* - the SPARQL parser needs the namespace prefixes defined. You can't use prefixed URIs like `foaf:is_an` as the SPARQL parser has to resolve all those things to full URIs eventually. – UninformedUser Jan 24 '21 at 19:59
  • That said, why do you use `foaf` as prefix in the query? FOAF is an existing vocabulary and the prefix `foaf:` is almost always used for this vocabulary. I mean, it's not forbidden as long as you declare the prefix `foaf:` in your query with the corresponding namespace. But it's weird. Also I'm not happy with your naming convention of the predicates, it's common practice to use camel case style for predicates and Pascal Case for classes. Again, not an issue or forbidden to do it differently, it's just not best practice. – UninformedUser Jan 24 '21 at 20:05
  • Anyways, you got the solution for your problem, add the prefix declaration to your query and it will work – UninformedUser Jan 24 '21 at 20:06
  • actually I have no idea about query, coding is not my expertise area, this is my final homework so I am trying to learn. I am trying to apply example queries on website of rdflib. It has been 2 days that I learned how to import libraries, that's why it is weird:) Thank you so much for answering:) – GamzeGG Jan 24 '21 at 20:32
  • 1
    no worries, everybody starts from scratch. if you got the query working, feel free to post your solution as an answer. – UninformedUser Jan 25 '21 at 07:34
  • qres = g.query( """SELECT DISTINCT ?aName ?bOracle WHERE { ?a foaf:individual_of ?b . ?a foaf:Name ?aName. ?b foaf:Oracle ?bOracle . }""") for row in qres: print("%s individual_of %s" % row) I Wrote this query but couldnt take any answer. I changed ?a and?b upper lower letters etc, but still not having any answer. no error no result nothing – GamzeGG Jan 26 '21 at 14:55
  • Honestly, you should read again about RDF and especially how to model data iin RDF. I mean, your SPARQL query tries to match your data, but you can't invent predicates like `foaf:Oracle` - what should this relation mean? Also almost all other predicates you used in the query do not belong to FOAF vocabulary - nor do I think they are contained in your data. So again, is there such an RDF triple in your data with such a predicate? You're missing the basic things, not everything is in the `foaf:` prefix - nor make properties like `foaf:Microsoft` any sense at all – UninformedUser Jan 26 '21 at 20:19
  • 1
    I actually solved my problem, I tried jena fuseki, and I took the result that I wanted to. Yes exactly I was missing basic points, thank you for answering. – GamzeGG Jan 27 '21 at 01:16

0 Answers0