1

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.

4

1 回答 1

1

你用什么样的方法来创建 VirtModel 对象?

笔记:

如果您使用:

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

因此模型将仅包含来自“virt:DEFAULT”图表的数据。VirtuosoQueryExecutionFactory 将在查询文本中添加下一个编译指示:

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

如果你使用类似的东西:

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

所以模型将只包含来自 graphName 图表的数据。VirtuosoQueryExecutionFactory 将在查询文本中添加下一个编译指示:

define input:default-graph-uri <graphName>

如果要使用所有图表中的数据,则必须调用:

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

如果您在上面设置为 TRUE,则不会添加 default-graph-uri 的编译指示。

将 Construct 与 Virtuoso Jena 提供程序一起使用的工作示例:

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());
于 2014-03-21T02:13:09.630 回答