zoukankan      html  css  js  c++  java
  • SPARQL查询语句整理

    本文大多内容来自Joshua Taylor的回答

    https://stackoverflow.com/users/1281433/joshua-taylor

    查询子类或等价关系

    https://stackoverflow.com/questions/21092246/sparql-query-subclass-or-equivalentto/21093154#21093154

    even though owl:equivalentClass is a symmetric property (i.e., from a owl:equivalentClass b we can infer b owl:equivalentClass a), the triple might be present in only one direction in the data

    在数据里面等价关系是单向表示的,因此查询等价类的语句为

    ?myClass (owl:equivalentClass|^owl:equivalentClass)* :MyClass

     查询等价属性的语句为

    ?p (owl:equivalentProperty|^ owl:equivalentProperty)* :order_no

     查询<http://class/加工过程> class的property

    SELECT  ?subclass 
    WHERE {
    ?subclass rdfs:domain <http://class/加工过程>.#加工过程类的所有属性
    }

    查询<http://class/加工过程>的所有属性的所有数据(s p o)

    SELECT  ?s ?subclass ?o 
    WHERE {
    ?subclass rdfs:domain <http://class/加工过程>.#加工过程类的所有属性
      ?s ?subclass ?o#返回属性关联的所有数据
    }
    LIMIT 100

    查询所有实例及其对应类型

    SELECT ?instance ?s WHERE {
    ?instance rdf:type ?s. #找出实例的类型
    } 

     查询值为1600KN20173的所有数据

    SELECT ?instance ?p WHERE {
    ?instance ?p “1600KN20173”. #找出属性值为V101208的所有实例
    }

    如何获取实例对应本体

    https://stackoverflow.com/questions/18458150/how-to-query-classes-with-object-property-in-sparql/18466646#18466646

    本体之间存在关系  Human ----(hasPizza)---> Pizzas

    为本体添加实例  Human:Jim ----(hasPizza)---> Pizzas:cheesePizza

    执行语句:

    select ?x ?y where {
      ?x hasPizza ?y
    }

    返回?x=Jim  和 ?y=cheesePizza

    如何得到实例对应本体?

    用turtle表示rdf

    @prefix :        <http://example.org/pizzas#> .
    @prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix pizzas:  <http://example.org/pizzas#> .
    @prefix owl:     <http://www.w3.org/2002/07/owl#> .
    @prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
    @prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    
    pizzas:Jim
          a       pizzas:Human , owl:NamedIndividual ;
          pizzas:hasPizza pizzas:CheesePizza .
    
    pizzas:hasPizza
          a       owl:ObjectProperty ;
          rdfs:domain pizzas:Human ;
          rdfs:range pizzas:Pizza .
    
    pizzas:Human
          a       owl:Class .
    
    pizzas:Pizza
          a       owl:Class .
    
    <http://example.org/pizzas>
          a       owl:Ontology .
    
    pizzas:CheesePizza
          a       pizzas:Pizza , owl:NamedIndividual .

    可以看到在模型定义中,

    pizzas:hasPizza
          a       owl:ObjectProperty ;
          rdfs:domain pizzas:Human ;
          rdfs:range pizzas:Pizza .

    因此可以利用属性hasPizza的rdfs:domain和rdfs:range两个关系来获得

    查询语句为:

    prefix :        <http://example.org/pizzas#>
    prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
    
    select ?domain ?range where { 
      :hasPizza rdfs:domain ?domain ;
                rdfs:range ?range .
    }

    返回:

    -------------------
    | domain | range  |
    ===================
    | :Human | :Pizza |
    -------------------

    2.利用rdf:type获取

    SELECT DISTINCT ?s WHERE {
    ?instance rdf:type ?s. #找出所有三元组的类型
    } 

    返回了本体和实例数据的类型,混在一起了,因为本体和数据都是用三元组表示的

    所以限定到具体数据,返回类型

    SELECT DISTINCT ?s WHERE {
    ?instance ?p "三月".
    ?instance rdf:type ?s. #找出实例的类型
    } 

    实例的rdf:

      <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:j.0="http://class/"
        xmlns:j.1="http://dataproperty/"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns:j.2="http://objectproperty/"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
    <j.0:维保 rdf:about="http://individual/维保/1600KN20173">
        <j.1:保养人>empty</j.1:保养人>
        <j.1:实际保养日期>empty</j.1:实际保养日期>
        <j.1:所需工时_人数_时间>2*0.1</j.1:所需工时_人数_时间>
        <j.1:计划保养时间>3月5日——3月11日</j.1:计划保养时间>
        <j.1:计划保养周>第10周</j.1:计划保养周>
        <j.1:设备保养级别>1级</j.1:设备保养级别>
        <j.1:月份>三月</j.1:月份>
        <j.1:维保计划编号>1600KN20173</j.1:维保计划编号>
      </j.0:维保>

    查询结果:

    上述查询语句是对整个数据库进行查询,所以查询了所有路径下的数据:

    "http://class/"
    "http://dataproperty/"
    "http://objectproperty/"

    等等

    可以指定PREFIX : <http://class/> 减小查询范围

    比如下面语句

    就指定了在‘:’的范围中查找等价属性

    PREFIX : <http://www.siemens.com/semantic_web#>
    PREFIX owl: <http://www.w3.org/2002/07/owl#>
    SELECT ?property  WHERE {
         ? property (owl:equivalentProperty|^owl:equivalentProperty)* :order_id.
     }
  • 相关阅读:
    html5不能播放视频的方法
    mysql找出重复数据的方法
    jquery each循环遍历完再执行的方法
    Android:TextView跑马灯-详解
    日志处理(一) log4j 入门和详解(转)
    周记 2014.11.08
    周记 2014.11.01
    linux下解压命令大全
    关于Context []startup failed due to previous errors
    周记 2014.10.25
  • 原文地址:https://www.cnblogs.com/34fj/p/10913298.html
Copyright © 2011-2022 走看看