aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/rsacomb/RSAOntology.scala
blob: 7fc87815ea9a8865a7945f99ff74ff847b642eb6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package rsacomb

/* Java imports */
// import java.io.File
import java.util.stream.{Collectors,Stream}

import org.semanticweb.owlapi.model.OWLOntology
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression
// import org.semanticweb.owlapi.model.OWLAxiomVisitorEx
import org.semanticweb.owlapi.model.parameters.Imports
// import org.semanticweb.owlapi.reasoner.OWLReasoner
import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory

import tech.oxfordsemantic.jrdfox.logic.Variable

/* Scala imports */
import scala.collection.JavaConverters._

/* Local imports */
// import rsacomb.RSAAxiom 

/* Debug only */
import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer

// import java.util.HashMap
// import java.util.stream.{Stream,Collectors}

// import org.semanticweb.owlapi.model.{AxiomType, ClassExpressionType, OWLObjectSomeValuesFrom}
// import org.semanticweb.owlapi.model.{OWLSubClassOfAxiom, OWLEquivalentClassesAxiom}
// import org.semanticweb.owlapi.model.OWLClassExpression
// import org.semanticweb.owlapi.model.IRI
// import org.semanticweb.owlapi.reasoner.{OWLReasonerFactory, OWLReasoner}
// import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl

// import tech.oxfordsemantic.jrdfox.Prefixes
// import tech.oxfordsemantic.jrdfox.client.{ConnectionFactory, ServerConnection, DataStoreConnection}
// import tech.oxfordsemantic.jrdfox.client.UpdateType
// import tech.oxfordsemantic.jrdfox.logic.{Rule, Atom, Literal, Term, Variable}
// import tech.oxfordsemantic.jrdfox.logic.{BuiltinFunctionCall, TupleTableName}
// import tech.oxfordsemantic.jrdfox.logic.{LogicFormat}
// import rsacomb.SkolemStrategy

trait RSAOntology {

  implicit class RSAOntology(ontology: OWLOntology) extends RSAAxiom {

    def isRSA: Boolean = {

      /* TODO: Steps for RSA check
       * 1) convert ontology axioms into LP rules
       * 2) call RDFox on the onto and compute materialization
       * 3) build graph from E(x,y) facts
       * 4) check if the graph is tree-like
       *    ideally this annotates the graph with info about the reasons
       *    why the ontology might not be RSA. This could help a second
       *    step of approximation of an Horn-ALCHOIQ to RSA
       */

      val tbox = 
        Stream.concat(ontology.tboxAxioms(Imports.INCLUDED), ontology.rboxAxioms(Imports.INCLUDED))
              .collect(Collectors.toList()).asScala
      val unsafe = ontology.getUnsafeRoles

      /* DEBUG: print rules in DL syntax */
      val renderer = new DLSyntaxObjectRenderer()
      println("\nDL rules:")
      tbox.foreach(x => println(renderer.render(x)))

      /* Ontology convertion into LP rules */
      println("\nLP rules:")
      for {
        axiom <- tbox
        visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe)
        rule  <- axiom.accept(visitor)
      } yield println(rule)

      /* DEBUG */
      true
    }

    def getUnsafeRoles: List[OWLObjectPropertyExpression] = {

      val factory = new StructuralReasonerFactory()
      val reasoner = factory.createReasoner(ontology)
      val tbox = ontology.tboxAxioms(Imports.INCLUDED).collect(Collectors.toList()).asScala
      val rbox = ontology.rboxAxioms(Imports.INCLUDED).collect(Collectors.toList()).asScala

      /* DEBUG: print rules in DL syntax */
      val renderer = new DLSyntaxObjectRenderer()
      println("\nT5 axioms:")
      for {
        axiom <- tbox
        if axiom.isT5
      } yield println(renderer.render(axiom))

            // def isT3(axiom : OWLAxiom) : Boolean = {
            // println(axiom)
            // axiom.getAxiomType match {
            //     case AxiomType.SUBCLASS_OF => 
            //     val axiom1 = axiom.asInstanceOf[OWLSubClassOfAxiom]
            //     axiom1.getSubClass().getClassExpressionType() == ClassExpressionType.OBJECT_SOME_VALUES_FROM &&
            //         axiom1.getSuperClass().getClassExpressionType() == ClassExpressionType.OWL_CLASS
            //     case AxiomType.EQUIVALENT_CLASSES => false // TODO
            // }
            // }

            // def isT4(axiom : OWLAxiom) : Boolean = {
            // axiom.getAxiomType match {
            //     case AxiomType.SUBCLASS_OF => 
            //     val axiom1 = axiom.asInstanceOf[OWLSubClassOfAxiom]
            //     axiom1.getSubClass().getClassExpressionType() == ClassExpressionType.OWL_CLASS &&
            //         axiom1.getSuperClass().getClassExpressionType() == ClassExpressionType.OBJECT_MAX_CARDINALITY
            //     case AxiomType.EQUIVALENT_CLASSES => false // TODO
            // }
            // }

            // def isT5(axiom : OWLAxiom) : Boolean = {
            // axiom.getAxiomType match {
            //     case AxiomType.SUBCLASS_OF => {
            //     val axiom1 = axiom.asInstanceOf[OWLSubClassOfAxiom]
            //     axiom1.getSubClass().getClassExpressionType() == ClassExpressionType.OWL_CLASS &&
            //         axiom1.getSuperClass().getClassExpressionType() == ClassExpressionType.OBJECT_SOME_VALUES_FROM
            //     }
            //     case AxiomType.EQUIVALENT_CLASSES => false // TODO
            // }
            // } 


            // println("T3")
            // for {
            // axiom <- ontology.tboxAxioms(Imports.INCLUDED)
            // //role <- axiom.objectPropertiesInSignature()
            // } yield println(axiom)

            // println("T4")
            // for {
            // axiom <- ontology.tboxAxioms(Imports.INCLUDED).filter(isT4)
            // //role <- axiom.objectPropertiesInSignature()
            // } yield println(axiom)


      /* DEBUG */
      List()
    }

  } // implicit class RSAOntology

} // trait RSAOntology