Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

rdf - SPARQL and Rules for ontologies

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 { … }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...