Sparql ';过滤器不存在';操作员没有使用apache jena?

Sparql ';过滤器不存在';操作员没有使用apache jena?,sparql,jena,Sparql,Jena,在这里,我的sparql查询代码是查找最不常见的包含项,查询的输出必须是“A”。我在这里得到一个空白结果。我认为我的查询是正确的,但似乎ApacheJena的“过滤器不存在”不起作用。我还检查了其他示例。是否有“过滤器不存在”的替代解决方案,或者我需要为此修改java代码 OWL文件结构 Thing: | A |_P(p1,p2) |_M(m1,m2) | B import org.apache.jena.iri.impl.Main;

在这里,我的sparql查询代码是查找最不常见的包含项,查询的输出必须是“A”。我在这里得到一个空白结果。我认为我的查询是正确的,但似乎ApacheJena的“过滤器不存在”不起作用。我还检查了其他示例。是否有“过滤器不存在”的替代解决方案,或者我需要为此修改java代码

OWL文件结构

 Thing:
    |
    A 
    |_P(p1,p2)
      |_M(m1,m2) 
    |
    B
import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.util.FileManager;

public class SPARQLReasoner {

    public static void main(String args[]) {
          sparqlTest();
    }

    public static void sparqlTest() {
        FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
        Model model;
        model = FileManager.get().loadModel("C:\\Users\\Chetan\\Desktop\\test.owl");
        Reasoner reasoner=ReasonerRegistry.getOWLReasoner();
        reasoner = reasoner.bindSchema(model);
         InfModel infmodel = ModelFactory.createInfModel(reasoner, model);
        String queryString;
        queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                + "PREFIX owl: <http://www.w3.org/2002/07/owl#> "
                + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                + "PREFIX : <http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#> "

                +"SELECT ?lcs WHERE{ " 
                +"?lcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
                +"?lcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
                +"?lcs rdf:type owl:Class . "

                +"FILTER NOT EXISTS {?llcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
                +"?llcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
                +"?llcs rdf:type owl:Class . "
                +"?llcs (rdfs:subClassOf)+ ?lcs . "
                +"} "
                +"}";

        Query query= QueryFactory.create(queryString);
        QueryExecution qexec= QueryExecutionFactory.create(query, infmodel);
        try{
            ResultSet results=qexec.execSelect();
            while(results.hasNext()){
                QuerySolution soln=results.nextSolution();
                Resource r;
                r=soln.getResource("lcs");
                System.out.println(r);
            }
        }finally{
            qexec.close();
        }
    }
}
这里A、p、M和B是概念。p1、p2、m1和m2是实例。M是P的子类,P是A的子类,A和B是事物的子类

JAVA代码

 Thing:
    |
    A 
    |_P(p1,p2)
      |_M(m1,m2) 
    |
    B
import org.apache.jena.iri.impl.Main;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.util.FileManager;

public class SPARQLReasoner {

    public static void main(String args[]) {
          sparqlTest();
    }

    public static void sparqlTest() {
        FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
        Model model;
        model = FileManager.get().loadModel("C:\\Users\\Chetan\\Desktop\\test.owl");
        Reasoner reasoner=ReasonerRegistry.getOWLReasoner();
        reasoner = reasoner.bindSchema(model);
         InfModel infmodel = ModelFactory.createInfModel(reasoner, model);
        String queryString;
        queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                + "PREFIX owl: <http://www.w3.org/2002/07/owl#> "
                + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
                + "PREFIX : <http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#> "

                +"SELECT ?lcs WHERE{ " 
                +"?lcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
                +"?lcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
                +"?lcs rdf:type owl:Class . "

                +"FILTER NOT EXISTS {?llcs ^(rdf:type/(rdfs:subClassOf)*) :p1 . "
                +"?llcs ^(rdf:type/(rdfs:subClassOf)*) :m1 . "
                +"?llcs rdf:type owl:Class . "
                +"?llcs (rdfs:subClassOf)+ ?lcs . "
                +"} "
                +"}";

        Query query= QueryFactory.create(queryString);
        QueryExecution qexec= QueryExecutionFactory.create(query, infmodel);
        try{
            ResultSet results=qexec.execSelect();
            while(results.hasNext()){
                QuerySolution soln=results.nextSolution();
                Resource r;
                r=soln.getResource("lcs");
                System.out.println(r);
            }
        }finally{
            qexec.close();
        }
    }
}
import org.apache.jena.iri.impl.Main;
导入com.hp.hpl.jena.query.query;
导入com.hp.hpl.jena.query.QueryExecution;
导入com.hp.hpl.jena.query.QueryExecutionFactory;
导入com.hp.hpl.jena.query.QueryFactory;
导入com.hp.hpl.jena.query.QuerySolution;
导入com.hp.hpl.jena.query.ResultSet;
导入com.hp.hpl.jena.rdf.model.InfModel;
导入com.hp.hpl.jena.rdf.model.model;
导入com.hp.hpl.jena.rdf.model.ModelFactory;
导入com.hp.hpl.jena.rdf.model.Resource;
导入com.hp.hpl.jena.reasoner.reasoner;
导入com.hp.hpl.jena.reasoner.ReasonerRegistry;
导入com.hp.hpl.jena.util.FileManager;
公共类sparqler推理器{
公共静态void main(字符串参数[]){
sparqlTest();
}
公共静态无效测试(){
FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
模型;
model=FileManager.get().loadModel(“C:\\Users\\Chetan\\Desktop\\test.owl”);
Reasoner Reasoner=ReasonerRegistry.getOWLReasoner();
reasoner=reasoner.bindSchema(模型);
InfModel InfModel=ModelFactory.createInfModel(推理机,模型);
串查询串;
queryString=“前缀rdf:”
+“前缀owl:”
+“前缀xsd:”
+“前缀rdfs:”
+“前缀:”
+“选择?lcs,其中{”
+“lcs^(rdf:类型/(rdf:子类别)*):p1。”
+“lcs^(rdf:类型/(rdf:子类别)*):m1。”
+“?lcs rdf:类型owl:类。”
+“筛选器不存在{?llcs^(rdf:type/(rdfs:subassof)*):p1。”
+“?有限责任公司^(rdf:类型/(rdf:子类别)*):m1。”
+“?llcs rdf:类型owl:类。”
+“有限责任公司(RDF:子类)+有限责任公司。”
+"} "
+"}";
Query=QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query,infmodel);
试一试{
ResultSet results=qexec.execSelect();
while(results.hasNext()){
QuerySolution soln=results.nextSolution();
资源r;
r=soln.getResource(“lcs”);
系统输出println(r);
}
}最后{
qexec.close();
}
}
}
OWL代码:

<rdf:RDF xmlns="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

    <owl:Ontology rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11"/>

    <owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#A"/>

    <owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#B"/>

    <owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
    </owl:Class>

    <owl:Class rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P">
        <rdfs:subClassOf rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#A"/>
    </owl:Class>

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#m1">
        <rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M"/>
    </owl:NamedIndividual>

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#m2">
        <rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#M"/>
    </owl:NamedIndividual>

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#p1">
        <rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
    </owl:NamedIndividual>

    <owl:NamedIndividual rdf:about="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#p2">
        <rdf:type rdf:resource="http://www.semanticweb.org/chetan/ontologies/2014/5/untitled-ontology-11#P"/>
    </owl:NamedIndividual>

</rdf:RDF>


请在此发表您的看法。

您是否正在运行最新版本

“空白资源”是什么意思

将成为

"SELECT ?lcsWHERE {" 
该部分:

soln.getResource("Disease_Observation_List");

为空(无变量“疾病观察列表”)。

是否运行最新版本

“空白资源”是什么意思

将成为

"SELECT ?lcsWHERE {" 
该部分:

soln.getResource("Disease_Observation_List");

为空(无变量“疾病观察列表”)。

是否运行最新版本

“空白资源”是什么意思

将成为

"SELECT ?lcsWHERE {" 
该部分:

soln.getResource("Disease_Observation_List");

为空(无变量“疾病观察列表”)。

是否运行最新版本

“空白资源”是什么意思

将成为

"SELECT ?lcsWHERE {" 
该部分:

soln.getResource("Disease_Observation_List");
为空(无变量“疾病观察列表”)。

过滤器不存在
工作正常 查询中的
过滤器不存在
工作正常。问题是推理模型中存在的数据比原始模型中的数据多,因此原始模型中不存在的东西比推理模型中不存在的东西多

这在没有推理模型的情况下有效 预期答案是
:p
。如果使用上一个问题的答案(稍加修改),则可以使用Jena的
sparql
命令行工具获得预期结果:

前缀:
前缀owl:
前缀rdf:
前缀rdfs:
选择?lcs何处{
lcs^(rdf:类型/rdfs:子类*):p1,:m1;
猫头鹰:上课。
筛选器不存在{
有限责任公司^(rdf:类型/rdf:子类*):p1,:m1;
猫头鹰:类;
rdfs:子类+?lcs。
}
}
-------
|lcs|
=======
|:P|
-------
这与推理模型不同 查询与非推理模型一起工作,但与推理模型一起中断,因此推理模型必须有一些额外的数据,使
过滤器不存在
失败。什么可以改变?在
过滤器不存在中
,我们检查是否不存在以下匹配项:

?llcs rdfs:subClassOf+ ?lcs .
在原始数据中,没有三重
:p rdfs:subClassOf:p
,因此我们不会从结果中筛选出
:p
。然而,在推理模型中,对于每个类x,我们都有
x rdfs:subassof x
,因此我们确实有
:P rdfs:subassof:P
,因此您不会得到任何结果。这意味着您永远不会得到结果,因此您需要添加另一个筛选器,以确保
?llc
?lcs
不同:

  filter not exists { 
    ?llcs ^(rdf:type/rdfs:subClassOf*) :p1, :m1 ;
          a owl:Class ;
          rdfs:subClassOf+ ?lcs .
    filter ( ?llcs != ?lcs )
  }
过滤器不存在
工作正常 查询中的
过滤器不存在
工作正常。问题是推理模型中存在的数据比原始模型中的数据多,因此原始模型中不存在的东西比推理模型中不存在的东西多

这在没有推理模型的情况下有效 预期答案是
:p
。如果您使用(稍微修改