diff options
Diffstat (limited to 'src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala')
-rw-r--r-- | src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | 111 |
1 files changed, 13 insertions, 98 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala index bbbbcf3..9902fcd 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |||
@@ -71,108 +71,10 @@ object RSAUtil { | |||
71 | // manager.createOntology(axioms.asJava) | 71 | // manager.createOntology(axioms.asJava) |
72 | // } | 72 | // } |
73 | 73 | ||
74 | /** Compute the RSA dependency graph for a set of axioms | ||
75 | * | ||
76 | * @return a tuple containing the dependency graph and a map between | ||
77 | * the newly introduced constants and the corresponding input axioms. | ||
78 | * | ||
79 | * @note no check on the ontology language is performed since the | ||
80 | * construction of the dependency graph is computed regardless. The | ||
81 | * input axioms are assumed to be normalized. | ||
82 | */ | ||
83 | def dependencyGraph( | ||
84 | axioms: List[OWLLogicalAxiom], | ||
85 | datafiles: List[File] | ||
86 | ): (Graph[Resource, DiEdge], Map[String, OWLAxiom]) = { | ||
87 | val unsafe = RSAOntology(axioms, datafiles).unsafeRoles | ||
88 | var nodemap = Map.empty[String, OWLAxiom] | ||
89 | |||
90 | object RSAConverter extends RDFoxConverter { | ||
91 | |||
92 | override def convert( | ||
93 | expr: OWLClassExpression, | ||
94 | term: Term, | ||
95 | unsafe: List[OWLObjectPropertyExpression], | ||
96 | skolem: SkolemStrategy, | ||
97 | suffix: RSASuffix | ||
98 | ): Shards = | ||
99 | (expr, skolem) match { | ||
100 | |||
101 | case (e: OWLObjectSomeValuesFrom, c: Constant) => { | ||
102 | nodemap.update(c.iri.getIRI, c.axiom) | ||
103 | val (res, ext) = super.convert(e, term, unsafe, skolem, suffix) | ||
104 | if (unsafe contains e.getProperty) | ||
105 | (RSA.PE(term, c.iri) :: RSA.U(c.iri) :: res, ext) | ||
106 | else | ||
107 | (RSA.PE(term, c.iri) :: res, ext) | ||
108 | } | ||
109 | |||
110 | case (e: OWLDataSomeValuesFrom, c: Constant) => { | ||
111 | nodemap.update(c.iri.getIRI, c.axiom) | ||
112 | val (res, ext) = super.convert(e, term, unsafe, skolem, suffix) | ||
113 | if (unsafe contains e.getProperty) | ||
114 | (RSA.PE(term, c.iri) :: RSA.U(c.iri) :: res, ext) | ||
115 | else | ||
116 | (RSA.PE(term, c.iri) :: res, ext) | ||
117 | } | ||
118 | |||
119 | case _ => super.convert(expr, term, unsafe, skolem, suffix) | ||
120 | } | ||
121 | } | ||
122 | |||
123 | /* Ontology convertion into LP rules */ | ||
124 | val term = RSAOntology.genFreshVariable() | ||
125 | val result = axioms.map(a => | ||
126 | RSAConverter.convert(a, term, unsafe, new Constant(a), Empty) | ||
127 | ) | ||
128 | |||
129 | val datalog = result.unzip | ||
130 | val facts = datalog._1.flatten | ||
131 | var rules = datalog._2.flatten | ||
132 | |||
133 | /* Open connection with RDFox */ | ||
134 | val (server, data) = RDFoxUtil.openConnection("rsa_dependency_graph") | ||
135 | |||
136 | /* Add additional built-in rules */ | ||
137 | val varX = Variable.create("X") | ||
138 | val varY = Variable.create("Y") | ||
139 | rules = Rule.create( | ||
140 | RSA.E(varX, varY), | ||
141 | RSA.PE(varX, varY), | ||
142 | RSA.U(varX), | ||
143 | RSA.U(varY) | ||
144 | ) :: rules | ||
145 | /* Load facts and rules from ontology */ | ||
146 | RDFoxUtil.addFacts(data, facts) | ||
147 | RDFoxUtil.addRules(data, rules) | ||
148 | /* Load data files */ | ||
149 | RDFoxUtil.addData(data, datafiles: _*) | ||
150 | |||
151 | /* Build the graph */ | ||
152 | val query = "SELECT ?X ?Y WHERE { ?X rsa:E ?Y }" | ||
153 | val answers = RDFoxUtil.submitQuery(data, query, RSA.Prefixes).get | ||
154 | var edges: Seq[DiEdge[Resource]] = | ||
155 | answers.collect { case (_, Seq(n1, n2)) => n1 ~> n2 } | ||
156 | val graph = Graph(edges: _*) | ||
157 | |||
158 | /* Close connection to RDFox */ | ||
159 | RDFoxUtil.closeConnection(server, data) | ||
160 | |||
161 | (graph, nodemap) | ||
162 | } | ||
163 | } | ||
164 | |||
165 | object RSAOntology { | ||
166 | |||
167 | import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ | ||
168 | |||
169 | /** Manager instance to interface with OWLAPI */ | 74 | /** Manager instance to interface with OWLAPI */ |
170 | val manager = OWLManager.createOWLOntologyManager() | 75 | val manager = OWLManager.createOWLOntologyManager() |
171 | val factory = manager.getOWLDataFactory() | 76 | val factory = manager.getOWLDataFactory() |
172 | 77 | ||
173 | /** Name of the RDFox data store used for CQ answering */ | ||
174 | private val DataStore = "answer_computation" | ||
175 | |||
176 | /** Simple fresh variable/class generator */ | 78 | /** Simple fresh variable/class generator */ |
177 | private var counter = -1; | 79 | private var counter = -1; |
178 | def genFreshVariable(): Variable = { | 80 | def genFreshVariable(): Variable = { |
@@ -184,6 +86,19 @@ object RSAOntology { | |||
184 | factory.getOWLClass(s"X$counter") | 86 | factory.getOWLClass(s"X$counter") |
185 | } | 87 | } |
186 | 88 | ||
89 | } | ||
90 | |||
91 | object RSAOntology { | ||
92 | |||
93 | import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ | ||
94 | |||
95 | /** Manager instance to interface with OWLAPI */ | ||
96 | val manager = OWLManager.createOWLOntologyManager() | ||
97 | val factory = manager.getOWLDataFactory() | ||
98 | |||
99 | /** Name of the RDFox data store used for CQ answering */ | ||
100 | private val DataStore = "answer_computation" | ||
101 | |||
187 | def apply( | 102 | def apply( |
188 | axioms: List[OWLLogicalAxiom], | 103 | axioms: List[OWLLogicalAxiom], |
189 | datafiles: List[File] | 104 | datafiles: List[File] |