3

I'm currently working on a project where I need to graphically represent a SPARQL-DL query. To do that, I need to get all the entities used in a query (at the end, all the entities used in the query and the results from the query). I'm struggling with getting all the entities of the query. Is there an easy way to get all the atoms of the query ?

The library I'm using is OWL-API 4.2.8 with the latest SPARQL-DL-API. I'm using the Example_Basic.java file to try my method.

Here's the query I used as an example (it gives me all the wines that are located in New Zealand):

PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>
SELECT ?wine 
WHERE { 
    PropertyValue(?wine, wine:locatedIn, wine:NewZealandRegion)    
}

the method I use :

extractAllQueryEntities(QueryResult result) {
        List<QueryAtomGroup> queryAtomGroups = result.getQuery().getAtomGroups();
        for (QueryAtomGroup queryAtomGroup : queryAtomGroups) {
            List<QueryAtom> atoms = queryAtomGroup.getAtoms();
            System.out.println("Size of the atoms: " + atoms.size());

            Iterator<QueryAtom> queryAtom = atoms.iterator();
            while (queryAtom.hasNext()) {
                QueryAtom element = queryAtom.next();
                System.out.println("atom: " + element);
                List<QueryArgument> arguments = element.getArguments();

                for (QueryArgument argument : arguments) {
                    System.out.println("type: " + argument.getType() + " : value: " + argument.getValueAsString());
                }
            }
        }
    }

and here's the result I get from my method:

    Results:
    .
    .
    .
    some wines
    .
    . 
    .
    Size of the atoms: 1
    atom: PropertyValue(?de.derivo.sparqldlapi.Var@37b009, http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#locatedIn, http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#NewZealandRegion)
        type: VAR : value: wine
        type: URI : value: http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#locatedIn
        type: URI : value: http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#NewZealandRegion
Ignazio
  • 10,504
  • 1
  • 14
  • 25
Squick'
  • 45
  • 7
  • what do you mean by "struggling" I mean, you code does return all entities. What else do you need? – UninformedUser Mar 04 '20 at 20:02
  • Well you're right. "Struggling" it's a bit harsh. I'm worried my method could miss some atoms or doesn't handle all the type of queries. That's why I'm asking if someone has already found an easy way to get all the atoms of a query (with a native proper method to do that). You may be right, I'm asking to much. – Squick' Mar 05 '20 at 07:51
  • 1
    no, I think your method is fine and complete given that there is nothing more the API provides and there is no concept of nested constructs (afaik) – UninformedUser Mar 05 '20 at 08:22

0 Answers0