2016-08-09 36 views
0

我有兩個包含屬性has_answer和has_choice的個人。使用sparql顯示具有多個屬性的個人

<!-- http://www.semanticweb.org/myontology#Which_of_the_following_planet_has_the_average_speed_of_about_30Km/Seconds --> 

<owl:NamedIndividual rdf:about="&myontology;Which_of_the_following_planet_has_the_average_speed_of_about_30Km/Seconds"> 
    <rdf:type rdf:resource="&myontology;Question"/> 
    <rdfs:label>Which of the following planet has the average speed of about 30Km/Seconds ?</rdfs:label> 
    <myontology:QuestionNumber>1</myontology:QuestionNumber> 
    <myontology:has_answer rdf:resource="http://dbpedia.org/resource/Earth"/> 
    <myontology:has_choice rdf:resource="http://dbpedia.org/resource/Mars"/> 
    <myontology:has_choice rdf:resource="http://dbpedia.org/resource/Moon"/> 
    <myontology:has_score rdf:resource="&myontology;4_points"/> 
    <myontology:has_Level rdf:resource="&myontology;Expert"/> 
</owl:NamedIndividual> 

我想要做的是從個人

+ "SELECT distinct ?Qs ?CorrAns ?Choice " 
     + "WHERE {?Question rdf:type owl:NamedIndividual." 
     + "?Question rdfs:label ?Qs. " 
     + "?Question myontology:has_answer ?CorrAns." 
     + "?Question myontology:has_choice ?Choice." 
    // 
     + "}" 
     // + "GROUP BY ?Qs" 
     + ""; 
... 
    while (rs.hasNext()) { 
      QuerySolution soln = rs.nextSolution(); 
      String Qs = soln.getLiteral("Qs").getString(); 
      RDFNode choice = soln.get("Choice"); 
      String ans = choice.asNode().getLocalName(); 
      RDFNode Canswer = soln.get("CorrAns"); 
      String cans = Canswer.asNode().getLocalName(); 
.... 

,讓我產生如下得到的屬性列表:

Which of the following planet has the average speed of about 30Km/Seconds ? Choice : Mars CorrectAns: Earth 
Which of the following planet has the average speed of about 30Km/Seconds ? Choice : Moon CorrectAns: Earth 

我的問題是,我該怎麼做才能得到結果如下:

Which of the following planet has the average speed of about 30Km/Seconds ? || choice 1 : Mars || choice 2 : Moon || CorrecAns : Earth 

Sparql可以這樣做嗎?

+2

是,與SPARQL 1.1聚合函數'GROUP_CONCAT'同時通過'?Question' – AKSW

+0

分組我怎麼沒看到你'd得到你期望的結果'select ...?choice「+」WHERE {...「'導致一個名爲'choiceWHERE'的變量 –

+0

@JoshuaTaylor感謝您的回放,是的它只是在這裏沒有espace它就像這個'?choice'+我的代碼中的'WHERE ...' –

回答

0

我解決我的問題通過以下@AKSW的答案,在我的OWL文件解決一些問題