SPARQL and Rules for ontologies

别等时光非礼了梦想. 提交于 2019-12-13 08:56:32

问题


Is it possible to create if/then rules using SPARQL and infer new relationships on my data? For example, could I encode rules like the following?

  • if (blood_sugar > 126 and blood_sugar < 500) then blood_sugar_level = High
  • if (blood_sugar_level = High) then (service = adjust_insulin_dose)

回答1:


The question really doesn't provide enough data to figure out exactly what you're trying to do, but you can certainly bind values based on particular conditions. For instance, the following query includes some inline data (to associate patients with blood sugar levels), and binds the values of the variables ?bloodSugarLevel and ?service accordingly.

prefix : <http://stackoverflow.com/q/20840035/1281433/>

select ?patient ?service where {
  # some sample data of patients and
  # their blood sugar levels
  values (?patient ?bloodSugar) {
    (:alice 120)
    (:bill  150)
  }

  # bind ?bloodSugarLevel to :high or :low as appropriate.
  bind( if( 126 < ?bloodSugar && ?bloodSugar < 500,
            :high,
            :low )
        as ?bloodSugarLevel )

  # bind ?service to :adjust-insulin-dose if the
  # ?bloodSugarLevel is :high, else to :do-nothing.
  bind( if( ?bloodSugarLevel = :high,
            :adjust-insulin-dose,
            :do-nothing )
        as ?service )
}
----------------------------------
| patient | service              |
==================================
| :alice  | :do-nothing          |
| :bill   | :adjust-insulin-dose |
----------------------------------

Alternatively, you might look into 3.1.3 DELETE/INSERT with which you can write an update query like the following to add triples to the graph (where the … is the same as above).

insert { ?patient :hasService ?service }
where { … }


来源:https://stackoverflow.com/questions/20840035/sparql-and-rules-for-ontologies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!