sparql查询实例

SParsql语法:(后面的eg:是prifix中定义,指向建立的本体文件)
1)查询某个类的子类名称
Select ?x where {?x rdfs:subClassOf eg:类名. }
2) 查询某个实例的某个属性值
Select ?x where {eg:实例名称 eg:属性名称 ?x .}"
3)查询某几列的数据,相当于sql: select a,b from table
SELECT ?DataID ?SensorName WHERE { ?x eg:DataID ?DataID . ?x egSensorName ?SensorName}

4)查询某类的实例及对应实例的某个属性值
"SELECT ?name ?DataID " +
         "WHERE {" +
         "?name rdf:type Global:OrginData ." +
         "?name Global:DataID ?DataID }"
5)查询某类的实例及满足某个过滤条件
prefix ex:<http://aaa/a.owl>
SELECT ?person ?name
where
{
?person rdf:type ex:Person .
   ?person ex:name ?name .
   ?person ex:age ?age .
    Filter (?age>17)
}

-----------------------------------------
"SELECT ?name ?DataID   " +
         "WHERE {" +
         "?name rdf:type Global:OrginData . " +
         "?name Global:DataID ?DataID . " +
         "?name Global:rightUpY ?rightUpY . " +
         "FILTER ( ?rightUpY > '20' && ?rightUpY < '50' ) }"

6)综合条件
select ?name
where {
?person ex:name ?name .
{
   { ?person rdf:type ex:adult }
UNION
   {
           ?person ex:age ?age .
           filter (?age>17) }
}
}







3)
这个例子是在java环境中运行,用jena API解析,完整代码如下

OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );
   try {
    model.read(new FileInputStream("E:\\MakingCoolFun.owl"), "");
   } catch (FileNotFoundException e) {
    System.out.println(e.toString());
   }

   String name="ToCool";
   String prefix="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
     "PREFIX xsd:<http://www.w3.org/2000/10/XMLSchema#>"+
     "PREFIX owl:<http://www.w3.org/2002/07/owl#>"+
     "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>"+
     "PREFIX base:<http://www.hozo.jp/rdf/MakingCoolFun.ont>"+
     "PREFIX eg:<http://www.hozo.jp/owl/MakingCoolFun.owl#>";
   String strquery="SELECT ?x "+"\n"+
        " WHERE { "+
        "?x rdfs:subClassOf eg:"+name+      
        "}";

Query query=QueryFactory.create(prefix+strquery);

QueryExecution qe=QueryExecutionFactory.create(query,model);
ResultSet results=qe.execSelect();

//输出结果,这样可以去掉前缀
while (results.hasNext()) {
        QuerySolution soln = results.nextSolution(); //查询结果中的每一条(称之为满足条件的一个solution)
        String s = soln.get("x").toString();
        StringTokenizer token = new StringTokenizer(s,"#");
        token.nextToken();       
        System.out.println(token.nextToken());       
}
qe.close();


SELECT ?p ?fname ?title
  WHERE {
  ?p p: fname “Donald” .
  ?p p: lname “Chamberlin” .
  ?p p: age ?age .
  ?p p: fname ?fname .
  FILTER ( ?age > 30 )
  ?pub pub: author ?p .
  ?pub pub: title ?title .

你可能感兴趣的:(sparsql,prifix,rdfs)