有人能告诉我如何在 Eclipse 中列出 ontology 中的所有用户吗?
Can somebody tell me how to list all the users from the ontology in eclipse?
public class Main {
public static void main(String args[]){
//read an ontology
String filename = "IOTOntology.owl";
Model model = ModelFactory.createDefaultModel();
OntModel model1 = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
//over
// Waits for the device ID
System.out.print("Enter name of man: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String devID = null;
try {
devID = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read device ID!");
System.exit(1);
}
try {
File file=new File(filename);
FileInputStream reader=new FileInputStream(file);
System.out.println("The absolute path of the file is:"+ file.getAbsolutePath());
model.read(reader, "RDF/XML");
// Create a SPARQL query from the given string.
String queryString = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"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 act: <http://www.semanticweb.org/project/ontologies/2016/0/Actor#>" +
"select ?name "+
"where { "+
" ?User a rdf:name"+
"} \n ";
Query query = QueryFactory.create(queryString);
try ( // Execute the query and obtain results
QueryExecution qe = QueryExecutionFactory.create(query, model)) {
ResultSet results = qe.execSelect();
// Output query results
ResultSetFormatter.out(System.out, results, query);
qe.close();
}
model.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
我有一些问题,因为我的 table 是空的,它只在命令行中写入:
--------
| name |
========
--------
这是我 ontology 的 link:
https://github.com/isidoramalkata/IOTOntology.git
我认为您需要先阅读更多关于 RDF 和 SPARQL 的内容...
您的查询是
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
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 act: <http://www.semanticweb.org/project/ontologies/2016/0/Actor#>
select ?name where {
?User a rdf:name
}
这没有任何意义:
- 你select一个变量
?name
在查询 中没有出现
- 您正在使用
a
作为谓词,它是 rdf:type
属性 的快捷方式,它实际上将资源分配给 类,因此,您会要求rdf:name
类型的资源
- 而且您的 ontology
中不存在
看看三重模式:
?用户一个rdf:name
您的数据是
<owl:NamedIndividual rdf:about="file://SHOnt.owl#UserIsidora">
<rdf:type rdf:resource="file://SHOnt.owl#User"/>
<SHOnt:phoneNumber rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0605222024</SHOnt:phoneNumber>
<SHOnt:name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Isidora</SHOnt:name>
<SHOnt:email rdf:datatype="http://www.w3.org/2001/XMLSchema#string">isibojovic@gmail.com</SHOnt:email>
</owl:NamedIndividual>
您的数据中没有匹配此模式的三元组。
RDF 三元组表示
subject predicate object
即
@prefix shont: <file://SHOnt.owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
shont:UserIsidora shont:name "Isidora"^^xsd:string .
这是 SPARQL 所基于的 Turtle 语法,因此,以这种语法查看数据总是好的。
SPARQL 查询:
prefix shont: <file://SHOnt.owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select ?name where {
?User shont:name ?name
}
正如我所说,您真的应该先阅读 RDF 教程。
public class Main {
public static void main(String args[]){
//read an ontology
String filename = "IOTOntology.owl";
Model model = ModelFactory.createDefaultModel();
OntModel model1 = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
//over
// Waits for the device ID
System.out.print("Enter name of man: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String devID = null;
try {
devID = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read device ID!");
System.exit(1);
}
try {
File file=new File(filename);
FileInputStream reader=new FileInputStream(file);
System.out.println("The absolute path of the file is:"+ file.getAbsolutePath());
model.read(reader, "RDF/XML");
// Create a SPARQL query from the given string.
String queryString = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"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 act: <http://www.semanticweb.org/project/ontologies/2016/0/Actor#>" +
"select ?name "+
"where { "+
" ?User a rdf:name"+
"} \n ";
Query query = QueryFactory.create(queryString);
try ( // Execute the query and obtain results
QueryExecution qe = QueryExecutionFactory.create(query, model)) {
ResultSet results = qe.execSelect();
// Output query results
ResultSetFormatter.out(System.out, results, query);
qe.close();
}
model.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
我有一些问题,因为我的 table 是空的,它只在命令行中写入:
--------
| name |
========
--------
这是我 ontology 的 link: https://github.com/isidoramalkata/IOTOntology.git
我认为您需要先阅读更多关于 RDF 和 SPARQL 的内容...
您的查询是
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
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 act: <http://www.semanticweb.org/project/ontologies/2016/0/Actor#>
select ?name where {
?User a rdf:name
}
这没有任何意义:
- 你select一个变量
?name
在查询 中没有出现
- 您正在使用
a
作为谓词,它是rdf:type
属性 的快捷方式,它实际上将资源分配给 类,因此,您会要求rdf:name
类型的资源
- 而且您的 ontology 中不存在
看看三重模式:
?用户一个rdf:name
您的数据是
<owl:NamedIndividual rdf:about="file://SHOnt.owl#UserIsidora">
<rdf:type rdf:resource="file://SHOnt.owl#User"/>
<SHOnt:phoneNumber rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0605222024</SHOnt:phoneNumber>
<SHOnt:name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Isidora</SHOnt:name>
<SHOnt:email rdf:datatype="http://www.w3.org/2001/XMLSchema#string">isibojovic@gmail.com</SHOnt:email>
</owl:NamedIndividual>
您的数据中没有匹配此模式的三元组。 RDF 三元组表示
subject predicate object
即
@prefix shont: <file://SHOnt.owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
shont:UserIsidora shont:name "Isidora"^^xsd:string .
这是 SPARQL 所基于的 Turtle 语法,因此,以这种语法查看数据总是好的。
SPARQL 查询:
prefix shont: <file://SHOnt.owl#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select ?name where {
?User shont:name ?name
}
正如我所说,您真的应该先阅读 RDF 教程。