문제

My problem is similar to the one asked on this question: Is there a differense between a CONSTRUCT queries sent to a virtuoso endpoint and one sent to a Jena one?

I am using Virtuoso opensource as my graphstore and using the jena provider in order to access the data in that graphstore. I am doing several querys and everything is working fine (except for the amazing amount of memory and time that takes every inference with virtuoso but that should go in another question...).

The problem came when I tried to generate a model using a construct query. I have try using the VirtuosoQueryExecutionFactory and the query as string and the default QueryExecutionFactory with the query factory:

qexec = VirtuosoQueryExecutionFactory.create(queryString,inputModel);

model = qexec.execConstruct();

And

Query query = QueryFactory.create(queryString);
qexec = QueryExecutionFactory.create(query,inputModel);

model = qexec.execConstruct();

The query gives the expected result in the sparql endpoint but an empty model when querying in the code.

LOGGER.info("The model is: {}", model);
LOGGER.info("The size is: {}", model.size());

Gives the following output:

The model is: <ModelCom   {} | >
The size is: 0

The model where I execute the querys is not empty and I did the same query from the sparql endpoint, as I said, recieving the spected results.

Anyone know where could be the mistake?

Thanks.

Daniel.

EDIT: Here is the query I am trying to execute.

PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> 
PREFIX owl:<http://www.w3.org/2002/07/owl#> 
PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>  
PREFIX spatiaCore:<http://www.cedint.upm.es/residentialontology.owl#> 
PREFIX test:<http://test.url#> 
PREFIX spatiaCore: <http://www.cedint.upm.es/residentialontology.owl#> 
CONSTRUCT { 
    ?u ?p ?o1. 
    ?o1 ?p2 ?o2. 
    ?o2 ?p3 ?o3. 
    ?o3 ?p4 ?o4. 
    ?o4 ?p5 ?o5. 
    ?o6 ?p6 ?u. 
    ?o7 ?p7 ?o6
}  

WHERE { 
    ?u rdf:type spatiaCore:User. 
    ?u spatiaCore:id "0000000003B3B474"^^<http://www.w3.org/2001/XMLSchema#string>. 
    ?u ?p ?o1.
    OPTIONAL { 
        ?o1 ?p2 ?o2. 
        OPTIONAL {   
            ?o2 ?p3 ?o3.  
            OPTIONAL {
                ?o3 ?p4 ?o4.  
                OPTIONAL {
                    ?o4 ?p5 ?o5. 
                }
            }
        }
    }
    OPTIONAL { 
        ?o6 ?p6 ?u. 
        OPTIONAL {
            ?o7 ?p7 ?o6
        } 
    }
} 

As you can see, the query tries to construct a graph with all the nodes the user is linked to with a depth of max five relationships and the nodes that are linked to the user with a depth of max two relationships.

도움이 되었습니까?

해결책

What kind of method did you use for create VirtModel object?

NOTE:

If you used:

public static VirtModel openDefaultModel(DataSource ds);
public static VirtModel openDefaultModel(String url, String user, String password);

so the Model will contains only data from "virt:DEFAULT" graph. And VirtuosoQueryExecutionFactory will add next pragma to query text:

define input:default-graph-uri <virt:DEFAULT>

If you used something like:

public static VirtModel openDatabaseModel(String graphName, DataSource ds);
public static VirtModel openDatabaseModel(String graphName, String url, String user, String password) 

so the Model will contains only data from graphName graph. And VirtuosoQueryExecutionFactory will add next pragma to query text:

define input:default-graph-uri <graphName>

If you want to use data from all graphs, you must call:

VirtModel vmodel = ....create model method...
vmodel.setReadFromAllGraphs(true);

If you set above to TRUE, the pragma for default-graph-uri will not added.

The worked example of using Construct with Virtuoso Jena provider:

url = "jdbc:virtuoso://localhost:1111";
VirtGraph set = new VirtGraph ("test1", url, "dba", "dba");

set.clear();

String qry = "INSERT INTO GRAPH <test1> { <aa> <bb> 'cc' . <aa1> <bb> 'zz' }";
VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(qry, set);
vur.exec();                  

Model inputModel = new VirtModel(set);
System.out.println("InputModel :"+inputModel);
System.out.println("InputModel size :"+inputModel.size());
System.out.println();


qry = "CONSTRUCT { ?x <a> ?y } WHERE { ?x <bb> ?y }";
QueryExecution vqe = VirtuosoQueryExecutionFactory.create (qry, inputModel);

Model model = vqe.execConstruct();
System.out.println("Model :"+model);
System.out.println("Model size :"+model.size());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top