-2

I have created an ontology in protegee and uploaded it online in .owl file format. Now I am able to load it in python using jupyter notebook and rdflib to obtain all statements or the Entity-Attribute-Value (EAV). Now I want to obtain individuals and the value of their properties. For instance, "Bob hasAge 32", "Alice hasAge 23", etc. I want to query the ontology to get the result in the form of the table below. How can I achieve that? I am happy to provide more information if needed. Thank you in advance.

Individual Age
Bob 32
Alice 23
  • just write a SPARQL query with a triple pattern matching your data. I mean, this query is trivial containing a single triple pattern. – UninformedUser Jun 23 '21 at 16:11
  • Thank you that comment. I did that protegee and I obtained the result but I am not sure how to do same in python. Can you show a demo or sample code please? @UninformedUser – Julius Sechang Mboli Jun 23 '21 at 16:33
  • not sure if I understand, if you have the SPARQL query why can't you do it in rdflib as well? why not reading the official documentation? https://rdflib.readthedocs.io/en/stable/intro_to_sparql.html - or is something else unclear – UninformedUser Jun 23 '21 at 17:57
  • As suggested by @UninformedUser, it would be helpful if you could explain with (RDFLib) code what you tried and where you get stuck. Could you update your question, please? – Ben Companjen Jun 25 '21 at 20:20
  • Hi @UninformedUser, I followed the documentation as you suggested and was able to solve the problem. Can you turn your comments to answer so I can approve it? Thanks. – Julius Sechang Mboli Jul 09 '21 at 16:37
  • Hi @UninformedUser, though this question has been answered, I do have another issue about the next stage of my work. I am running a `delete/insert` sparql statement [source](https://www.w3.org/TR/sparql11-update/#deleteInsert) to automatically update data properties values of individuals but it does not accept variables except direct values. Do you have any suggestions for me? I can provide more infomation if needed. Thank you. – Julius Sechang Mboli Sep 06 '21 at 12:23
  • Hello, @BenCompanjen if you can help with my last comment. – Julius Sechang Mboli Sep 06 '21 at 12:29
  • @JuliusSechangMboli what means "does not accept variables"? please provide an example, ideally open a new question with the example as this is unrelated to the current topic – UninformedUser Sep 06 '21 at 12:39
  • @UninformedUser I want to do that but I am not able to ask questions now due to restrictions from Stack Overflow. – Julius Sechang Mboli Sep 06 '21 at 15:06
  • then post the SPARQL statement here. Without it's impossible to say what's going wrong – UninformedUser Sep 06 '21 at 17:39
  • @UninformedUser It is basically something like this: `graph.update(""" PREFIX foaf: WITH DELETE { ?person foaf:givenName 'Bill' } INSERT { ?person foaf:givenName 'William' } WHERE { ?person foaf:givenName 'Bill' } """)` I think my issue is how to escape the special characters {} and ? in my code. This code is from [Here](https://www.w3.org/TR/sparql11-update/#deleteInsert) – Julius Sechang Mboli Sep 06 '21 at 20:44
  • The code above should work but if the strings `'Willian'` and `'Bill'` are now stored in variables, I get errors while running it. I just want to perform the same operation but using integers in place of William and Bill. – Julius Sechang Mboli Sep 06 '21 at 20:50
  • that is just a Python problem isn't it? I mean, it's the same as asked yesterday by somebody else: https://stackoverflow.com/questions/69074199/using-variables-with-stored-values-in-rdflib-and-python-for-sparql-update-statem - are you doing the same lecture at university? Anyways, the answer is in the comment of the link I shared. Use Python string interpolation and don't forget to escape the curly brackets of the SPARQL statement if you prefer to use the Python 3 interpolation via `{var name}` style – UninformedUser Sep 07 '21 at 03:18
  • 1
    Thank you for that link. It is really very useful. I followed it and my issue is solved now. I had to go by the `replace()` function method as given in that [answer](https://stackoverflow.com/questions/69074199/using-variables-with-stored-values-in-rdflib-and-python-for-sparql-update-statem) since I could figure out how to escape the `'{'`. Thank you again @UninformedUser. – Julius Sechang Mboli Sep 08 '21 at 15:35
  • afaik, backslash would have been the way to escape it, no? – UninformedUser Sep 09 '21 at 05:43
  • @JuliusSechangMboli your follow-up comment looks like a new question to me. If the same or a similar question has not been posted yet, please ask a new question so others can find it and learn from it too. – Ben Companjen Oct 18 '21 at 07:29

1 Answers1

0

Just rewriting UninformedUser's comment as an answer for acceptance:

If you have the SPARQL query why can't you do it in rdflib as well? Why not try reading the official documentation? https://rdflib.readthedocs.io/en/stable/intro_to_sparql.html

Nicholas Car
  • 1,164
  • 4
  • 7