0

I use Jena API to read TTL files and get information form them using Sparql queries. This is the query I am using:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX test: <http://www.test.com/Test#>
select ?property (str(?value) as ?valueLiteral) (str(count(distinct ? 
   ind)) as ?noOfValueOccurrences)
        where { ?ind rdf:type test:Lecturer. ?ind  ?property ? 
           value.
                    ?property a owl:DatatypeProperty .}
group by ?property ?value
order by ?property

My goal is to run Sparql queries on different TTL files, each of which will have different URI or IRIs. Since I m trying to automate the queries in Java, I need to be able to get <http://www.test.com/Test#> or the IRI (from what I understood) when I read the .TTL file.

This is how I am trying to retrieve that:

public class Main {
     BasicConfigurator.configure();
     FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
     Model model = FileManager.get().loadModel(pathToFile, "TTL");
     System.out.println(model.getNsPrefixURI(""));
     ...
  }

which returns

null

I m new to ontologies, I appreciate if you could help figure this out. Please let me know if the information I provided is not sufficient.

Thank you in advance

DjSh
  • 2,776
  • 2
  • 19
  • 32
  • not sure what you're asking and why you need the namespace but in your example the prefix is `test:` so you should do `model.getNsPrefixURI("test")` - ti's also obvious that prefixes are not a logical element of an ontology but just part of some serialization formats like Turtle. Moreover, you could have multiple prefix declarations. But in any case, why wouldn't you use the full URIs when writing queries – UninformedUser Jan 29 '20 at 16:53
  • So I m reading about 20 files and I need to retrieve information about them but I dont know what the IRI are for each file. so that I can use it in my queries. basically I want to make `` a variable in my queries, since it will be different foreach file. – DjSh Jan 29 '20 at 18:22
  • @AKSW my goal is to run sparql queries on several TTL files. But I would not know their URIs in advance. So, I was wondering if there is a way to get them when I read each file – DjSh Jan 29 '20 at 20:52
  • @DjSh - could you say some more about what you are trying to do. When you say "their URIs" do you mean the files themselves? Or data in the file? If the latter, why do you need URIs? – AndyS Jan 29 '20 at 21:51
  • @AndyS I added the whole query. I will not know what the URI or will be for each file so how could I make the query like above and get properties and values. Also I would not know (:Lecturer) part for other files, so these two will be different for each file(like a variable). I think I m terrible at explaining :( – DjSh Jan 30 '20 at 14:45
  • @AndyS so the URI is inside the TTL file somewhere. I need to be able to find it automatically when reading a TTL file – DjSh Jan 30 '20 at 17:43
  • To query a file, you give the model object to the QueryExecutionFactory.create function, not the URI of the file. – AndyS Jan 30 '20 at 22:24
  • @AndyS Yes I do that. I m just saying the `` will be different for each file so it has to be a variable in the query. How do I get that when I read a file? – DjSh Jan 31 '20 at 15:26
  • I still don't understand what you're trying to achieve. Namespaces declarations are just for convenience, you can always use full URIs without prefixes. Obviously, you have to know the full URI in oder to use it in a query. But I don't see what you would do here automatically? A dataset can contain resource with different namespaces. – UninformedUser Jan 31 '20 at 19:45
  • @AKSW so where do I get the full URI from? I need to use the query on different endpoints. – DjSh Jan 31 '20 at 20:27

0 Answers0