Executing a SPARQL query

时间:2016-04-21 22:41:09

标签: sparql ontology

I have created a small ontology. It has only one class called methods and a datatype property action_nb (integer).

Then I created two individuals :

AHP => action_nb = 20

electre => action_nb = 50

I want to execute a simple query . Select method where nb_action < 40 and the results should give me EL1.

<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY untitled-ontology-77 "http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#"
     xml:base="http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:untitled-ontology-77="http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#"
     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/diabolico/ontologies/2016/3/untitled-ontology-77"/>
    


    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    


    <!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#action_nb -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-77;action_nb">
        <rdfs:domain rdf:resource="&untitled-ontology-77;methods"/>
        <rdfs:range rdf:resource="&xsd;integer"/>
    </owl:DatatypeProperty>
    


    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    


    <!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#methods -->

    <owl:Class rdf:about="&untitled-ontology-77;methods"/>
    


    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->

    


    <!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#ahp -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-77;ahp">
        <rdf:type rdf:resource="&untitled-ontology-77;methods"/>
        <action_nb rdf:datatype="&xsd;integer">20</action_nb>
    </owl:NamedIndividual>
    


    <!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#electre -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-77;electre">
        <rdf:type rdf:resource="&untitled-ontology-77;methods"/>
        <action_nb rdf:datatype="&xsd;integer">50</action_nb>
    </owl:NamedIndividual>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->

PREFIX ex: <http://example.org/so#>
SELECT ?methods
WHERE {
   ?inst rdf:type ex:methods .
   ?inst ex:action_nb ?value .
FILTER (?value < 40)
}

this is the code that I have been manipulating: but it gave me only an empty row when filtering with 40 & 2 rows when using 60

1 个答案:

答案 0 :(得分:1)

理解如何解决这个问题的关键是了解如何定义RDF三元组。您已声明action_nb是其值为要比较的整数的属性。因此,使用该属性创建您的查询:

PREFIX untitled-ontology-77: <http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#>
SELECT ?inst
WHERE {
    ?inst untitled-ontology-77:action_nb ?value .
    FILTER (?value < 40)
}

... FILTER子句只允许action_nb的值小于40。

如果该属性在其他类中使用,并且您希望确保只找到methods的成员,那么您还需要查询该类的实例:

PREFIX untitled-ontology-77: <http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#>
SELECT ?inst
WHERE {
   ?inst rdf:type untitled-ontology-77:methods .
   ?inst untitled-ontology-77:action_nb ?value .
   FILTER (?value < 40)
}

我怀疑在这种情况下你需要DISTINCT,并且要注意在查询之后执行查找所有不同的值,但是成对地应用于所有SELECTED值。即对于大型结果集,它可能表现不佳。因此,仅在必要时使用DISTINCT