Skip to content
This repository was archived by the owner on Oct 8, 2020. It is now read-only.

Commit c66eb87

Browse files
Cntd BC alg.
1 parent 5e8070a commit c66eb87

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

sansa-inference-spark/src/main/scala/net/sansa_stack/inference/spark/backwardchaining/BackwardChainingReasonerDataframe.scala

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ class BackwardChainingReasonerDataframe(
8484
lookup(tp.asTriple())
8585
}
8686

87-
private def lookupSimple(tp: Triple): Dataset[RDFTriple] = {
87+
private def lookupSimple(tp: Triple, triples: Dataset[RDFTriple] = graph): Dataset[RDFTriple] = {
8888
info(s"Lookup data for $tp")
8989
val s = tp.getSubject.toString()
9090
val p = tp.getPredicate.toString()
9191
val o = tp.getObject.toString()
9292

93-
var filteredGraph = graph
93+
var filteredGraph = triples
9494

9595
if(tp.getSubject.isConcrete) {
9696
filteredGraph.filter(t => t.s.equals(s))
@@ -215,6 +215,9 @@ class BackwardChainingReasonerDataframe(
215215
(RDFS.subPropertyOf, true, "SPO"),
216216
(RDFS.domain, false, "DOM"),
217217
(RDFS.range, false, "RAN"))
218+
219+
220+
218221
val DUMMY_VAR = NodeFactory.createVariable("VAR")
219222

220223
/**
@@ -227,6 +230,15 @@ class BackwardChainingReasonerDataframe(
227230
def extractWithIndex(graph: Dataset[RDFTriple]): Map[Node, Dataset[RDFTriple]] = {
228231
log.info("Started schema extraction...")
229232

233+
val bcProperties = session.sparkContext.broadcast(Set(
234+
RDFS.subClassOf,
235+
RDFS.subPropertyOf,
236+
RDFS.domain,
237+
RDFS.range).map(_.toString))
238+
239+
240+
val schemaTriples = graph.filter(t => bcProperties.value.contains(t.p)).cache()
241+
230242
// for each schema property p
231243
val index =
232244
properties.map { entry =>
@@ -235,7 +247,7 @@ class BackwardChainingReasonerDataframe(
235247
val alias = entry._3
236248

237249
// get triples (s, p, o)
238-
var triples = lookupSimple(Triple.create(DUMMY_VAR, p, DUMMY_VAR))
250+
var triples = lookupSimple(Triple.create(DUMMY_VAR, p, DUMMY_VAR), schemaTriples)
239251

240252
// compute TC if necessary
241253
if (tc) triples = computeTC(triples)
@@ -270,8 +282,15 @@ class BackwardChainingReasonerDataframe(
270282

271283
if (tp.getPredicate.matches(RDF.`type`.asNode())) { // rdf:type data
272284

273-
// get all non rdf:type triples first
274-
var instanceTriples = graph.filter(_.p != RDF.`type`.toString)
285+
var instanceTriples = graph
286+
287+
// if s is concrete, we filter first
288+
if(tp.getSubject.isConcrete) { // find triples where s occurs as subject or object
289+
instanceTriples = instanceTriples.filter(t => t.s == tp.getSubject.toString() || t.o == tp.getSubject.toString())
290+
}
291+
292+
// get all non rdf:type triples
293+
instanceTriples = instanceTriples.filter(_.p != RDF.`type`.toString)
275294

276295
// enrich the instance data with super properties, i.e. rdfs5
277296
if(tp.getSubject.isConcrete) { // find triples where s occurs as subject or object

0 commit comments

Comments
 (0)