将OWL类表达式重写为SPARQL查询?

时间:2018-06-23 13:50:10

标签: sparql owl

我一直熟悉AKSW的https://github.com/SmartDataAnalytics/OWL2SPARQL,即使使用Scala和sbt,它也非常易于使用,但是不会重写类表达式。

我(and at least one other person!)会发现类表达式-> SPARQL重写很有用。也许是因为我(至少)做了双关语?例如,我需要找到所有提及任何片剂(strong)类(间接)具有化学成分“瑞舒伐他汀”作为有效成分的药物处方个人

:X a 'drug prescription' .
:X mentions 'rosuvastatin Oral Tablet' .

其中

“瑞舒伐他汀口服片剂”的超类和断言公理:

drug tablet
  has_proper_part some (scattered molecular aggregate and 
    (is bearer of some active ingredient) and 
        (has granular part some rosuvastatin))

罗苏伐他汀是http://purl.obolibrary.org/obo/DRON_00018679(其他IRI可以在RDF / XML上下文中的http://www.ontobee.org/ontology/rdf/DRON?iri=http://purl.obolibrary.org/obo/DRON_00027869看到)

还有其他工具可以将OWL类表达式重写为SPARQL查询吗?

如果没有,我想我可能会写一个属性链快捷方式...但这会忽略该链中涉及的类的类型,对吗?

我在https://github.com/turbomam/owl2sparql4turbo

有一个例子

1 个答案:

答案 0 :(得分:1)

AKSW添加了我要求的功能,并给了我一些有关使用OWL API的很好的提示。我从Java流到Scala流的桥梁可能有点困惑。

克隆https://github.com/turbomam/owl2sparql4turbo,了解如何设置build.sbt以导入依赖项。

本体 rots.owl 包括这样的公理(以曼彻斯特符号... RDF / XML或Turtle也可以):

Class: obo:DRON_00027869

    Annotations: 
        rdfs:label "rosuvastatin Oral Tablet"^^xsd:string,
        obo:DRON_00010000 "402354"^^xsd:string

    SubClassOf: 
        obo:DRON_00000022,
        <http://www.obofoundry.org/ro/ro.owl#has_proper_part> some 
            (obo:OBI_0000576
             and (obo:BFO_0000053 some obo:DRON_00000028)
             and (obo:BFO_0000071 some obo:DRON_00018679))

运行 https://github.com/turbomam/owl2sparql4turbo/blob/master/src/main/scala/ROTS.scala ...

import java.io.File
import scala.compat.java8.StreamConverters.RichStream
import org.aksw.owl2sparql.OWLClassExpressionToSPARQLSubClassQueryConverter
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.model.IRI
import org.semanticweb.owlapi.search.EntitySearcher

object ROTS {

  // SPARQL searches for Rosuvastatin Oral Tablet classes

  def main(args: Array[String]): Unit = {

    var manager = OWLManager.createOWLOntologyManager()
    var ontology = manager.loadOntologyFromOntologyDocument(new File("rots.owl"))

    // use a factory to convert stings to IRI objects
    var factory = manager.getOWLDataFactory()
    var termIRI = IRI.create("http://purl.obolibrary.org/obo/DRON_00081385")
    var someClass = factory.getOWLClass(termIRI)

    var classSupers = EntitySearcher.getSuperClasses(someClass, ontology).toScala[Stream]

    var oneSuper = classSupers(0)

    var scConverter = new OWLClassExpressionToSPARQLSubClassQueryConverter()
    var targetVar = "?x"

    var queryStr = scConverter.convert(oneSuper, targetVar)

    println(queryStr)

  }
}

生成输出:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?x WHERE {  ?x rdf:type owl:Class ;
     rdfs:subClassOf [ rdf:type owl:Restriction ;
                       owl:onProperty <http://www.obofoundry.org/ro/ro.owl#has_proper_part> ;
                       owl:someValuesFrom [ owl:intersectionOf ( <http://purl.obolibrary.org/obo/OBI_0000576>
                                                                 [ rdf:type owl:Restriction ;
                                                                   owl:onProperty <http://purl.obolibrary.org/obo/BFO_0000053> ;
                                                                   owl:someValuesFrom <http://purl.obolibrary.org/obo/DRON_00000029>
                                                                 ]
                                                                 [ rdf:type owl:Restriction ;
                                                                   owl:onProperty <http://purl.obolibrary.org/obo/BFO_0000071> ;
                                                                   owl:someValuesFrom <http://purl.obolibrary.org/obo/CHEBI_30618>
                                                                 ]
                                                               ) ;
                                            rdf:type owl:Class
                                          ]
                     ] .
}