2017-05-09 25 views
0
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()); 
     }  
    } 
} 

我有一些問題,因爲我的表是空的,它寫在命令行中只有這個:有人可以告訴我如何在eclipse中列出本體中的所有用戶?

-------- 
| name | 
======== 
-------- 

,這是鏈接到我的本體論:我認爲你需要 https://github.com/isidoramalkata/IOTOntology.git

回答

1

閱讀一些更多關於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 
} 

這並沒有任何意義:

  • 您選擇一個變量?name不會在你使用a作謂語這是rdf:type財產的快捷
  • 查詢發生,這實際上分配資源類,因此,你會AAK爲其而且沒有在你的本體存在的資源是一種rdf:name

看一看三重模式:

用戶一個RDF:命名

你的數據是

<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">[email protected]</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是基於,因此,這是一件好事,在這句法看看你的數據。

SPARQL查詢:

prefix shont: <file://SHOnt.owl#> 
prefix xsd: <http://www.w3.org/2001/XMLSchema#> 
select ?name where { 
     ?User shont:name ?name 
} 

正如我所說的,你真的應該先讀RDF教程。

相關問題