SPARQL-查询所有数据属性,但不包含上层属性
我想查询特定个人的所有数据属性 我的数据属性在本体中定义 在我的本体中,我定义了数据属性树 要查询的目标个人 我的目标个人在我的owl中定义如下:SPARQL-查询所有数据属性,但不包含上层属性,sparql,jena,ontology,Sparql,Jena,Ontology,我想查询特定个人的所有数据属性 我的数据属性在本体中定义 在我的本体中,我定义了数据属性树 要查询的目标个人 我的目标个人在我的owl中定义如下: <owl:NamedIndividual rdf:about="http://www.owl.de/ontology/i40component-01#I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest"> <rdf:type
<owl:NamedIndividual rdf:about="http://www.owl.de/ontology/i40component-01#I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest">
<rdf:type rdf:resource="http://www.owl.de/ontology/i40component-01#Manifest"/>
<decription rdf:datatype="http://www.w3.org/2001/XMLSchema#string">An example work cell.</decription>
<ele rdf:datatype="http://www.w3.org/2001/XMLSchema#string">35.0</ele>
<lat rdf:datatype="http://www.w3.org/2001/XMLSchema#string">52.518611</lat>
<lon rdf:datatype="http://www.w3.org/2001/XMLSchema#string">13.376111</lon>
<name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">I40 Work Cell 1</name>
<production_date rdf:datatype="http://www.w3.org/2001/XMLSchema#string">2012-12-31T23:57:00</production_date>
<uuid rdf:datatype="http://www.w3.org/2001/XMLSchema#string">e41bdfaa-7163-46ed-8cb3-350fa226bbaf</uuid>
</owl:NamedIndividual>
我当前的SPARQL查询和结果
我当前的测试方法如下所示。它构建并执行SPARQL查询
@Test
public void showDataPropertiesOfWholeManifest() {
SelectBuilder sb = new SelectBuilder() //Building a Query template
.addPrefix("i40comp", owl.getI40NameSpace() + "#")
.addPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#")
.addPrefix("xsd", "http://www.w3.org/2001/XMLSchema#")
.addPrefix("owl", "http://www.w3.org/2002/07/owl#")
.addPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
//Define Variables
sb.addVar("?I40Component");
sb.addVar("?dataProperty");
sb.addVar("?datatypeValue");
//Find Individuals for Type "Manifest"
sb.addWhere("?I40Component", "rdf:type", URI.generateSparqlURI(I40VOC.Classes.AssetAdministrationShell.Manifest));
//Find Individual with UUID "e41bdfaa-7163-46ed-8cb3-350fa226bbaf"
sb.addWhere("?I40Component", "i40comp:uuid", "e41bdfaa-7163-46ed-8cb3-350fa226bbaf"); //Filter I40Component
//Get all properties of this individual
sb.addWhere("?dataProperty", "?", "owl:DatatypeProperty");
// Results preparation
sb.addWhere("?I40Component", "?dataProperty", "?datatypeValue");
//Filters blanks and literals
try {
sb.addFilter("!isBlank(?datatypeValue)");
sb.addFilter("isLiteral(?datatypeValue)");
} catch (ParseException e) {
e.printStackTrace();
}
//Build query and print result
Query q = sb.build();
executeSPARQLqueryAndPrintResult(q);
}
或再次作为查询字符串:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX i40comp: <http://www.owl.de/ontology/i40component-01#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?I40Component ?dataProperty ?datatypeValue
WHERE
{ ?I40Component
rdf:type i40comp:Manifest ;
i40comp:uuid "e41bdfaa-7163-46ed-8cb3-350fa226bbaf" .
?dataProperty
? owl:DatatypeProperty .
?I40Component
?dataProperty ?datatypeValue
FILTER ( ! isBlank(?datatypeValue) )
FILTER isLiteral(?datatypeValue)
}
SPARQL查询以某种方式转到“上层数据属性”,并选择它们作为结果,并打印真实子属性的值。
比如:
通常应为:
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:lat | "52.518611" |
也许你们中的一位可以向我解释为什么会发生这种情况,也可以支持我改进查询以获得目标结果 解决方案
需要的提示来自AKSW:
查询结果有什么问题?我是说,这显然是因为 推论我猜,你没有展示你喜欢的模特 使用-您正在使用推理模型。我说得对吗?你知道吗 推理是什么?我希望您只需要断言的数据 最简单的情况是使用默认模型并将数据加载到此模型中 一-AKSW 问题是在耶拿使用推理模型(推理又名推理): 这种模型也传递干扰数据 要仅获取我的场景中针对的断言数据,最简单的方法是使用默认模型而不是本体模型(
model
而不是OntModel
):
通过此更改,我只获得断言数据和目标结果:
---------------------------------------------------------------------------------------------------------------------------------------------------------
| I40Component | dataProperty | datatypeValue |
=========================================================================================================================================================
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:lon | "13.376111" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:ele | "35.0" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:decription | "An example work cell." |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:name | "I40 Work Cell 1" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:production_date | "2012-12-31T23:57:00" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:uuid | "e41bdfaa-7163-46ed-8cb3-350fa226bbaf" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:lat | "52.518611" |
---------------------------------------------------------------------------------------------------------------------------------------------------------
谢谢AKSW 这一行显然是错误的:
sb.addWhere(“?dataProperty”、“?”、“owl:DatatypeProperty”)代码>-查询pareser肯定会失败。它必须是rdf:type
作为谓词查询结果有什么问题?我的意思是,这显然是由于推断。所以我猜-你没有展示你使用的模型类型-你使用的是推理模型。我说得对吗?你知道推理是什么吗?I您只需要断言的数据最简单的情况是使用默认模型并将数据加载到此模型中顺便说一句,RDF和OWL支持的数据类型不仅仅是xsd:string
…Hi@AKSW,(1)。关于您的第一条评论:在我将“?”更改为“RDF:type”后,我没有收到错误消息,结果也没有更改。但是谢谢你的提示,以后我将使用“rdf:type”。(2)关于你的第二个命令。非常感谢您的留言,您完全正确。我更改了OntModel mONT=ModelFactory.createOntologyModel()代码>到mONT=ModelFactory.createDefaultModel()然后我得到了目标结果。我是本体论领域的新手,必须学习推理的东西。(3)关于你的第三条评论:谢谢!我将更改数据类型,例如GPS坐标
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:wpt_gps_location | "52.518611" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:lat | "52.518611" |
OntModel mONT = ModelFactory.createOntologyModel();
Model mONT = ModelFactory.createDefaultModel()
---------------------------------------------------------------------------------------------------------------------------------------------------------
| I40Component | dataProperty | datatypeValue |
=========================================================================================================================================================
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:lon | "13.376111" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:ele | "35.0" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:decription | "An example work cell." |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:name | "I40 Work Cell 1" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:production_date | "2012-12-31T23:57:00" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:uuid | "e41bdfaa-7163-46ed-8cb3-350fa226bbaf" |
| i40comp:I40Component_e41bdfaa-7163-46ed-8cb3-350fa226bbaf_I40WorkCell1_AASmanifest | i40comp:lat | "52.518611" |
---------------------------------------------------------------------------------------------------------------------------------------------------------