|
| 1 | +package net.sansa_stack.inference.spark.forwardchaining |
| 2 | + |
| 3 | +import net.sansa_stack.inference.data.RDFTriple |
| 4 | +import net.sansa_stack.inference.utils.Profiler |
| 5 | +import org.apache.spark.rdd.RDD |
| 6 | +import org.apache.spark.sql.DataFrame |
| 7 | + |
| 8 | +import scala.collection.mutable |
| 9 | +import scala.reflect.ClassTag |
| 10 | + |
| 11 | +/** |
| 12 | + * An engine to compute the transitive closure (TC) for a set of triples given in several datastructures. |
| 13 | + * |
| 14 | + * @author Lorenz Buehmann |
| 15 | + */ |
| 16 | +trait TransitiveReasoner extends Profiler{ |
| 17 | + |
| 18 | + // def computeTransitiveClosure[A, B, C](s: mutable.Set[(A, B, C)]): mutable.Set[(A, B, C)] = { |
| 19 | + // val t = addTransitive(s) |
| 20 | + // // recursive call if set changed, otherwise stop and return |
| 21 | + // if (t.size == s.size) s else computeTransitiveClosure(t) |
| 22 | + // } |
| 23 | + |
| 24 | + /** |
| 25 | + * Computes the transitive closure on a set of triples, i.e. it is computed in-memory by the driver. |
| 26 | + * Note, that the assumption is that all triples do have the same predicate. |
| 27 | + * |
| 28 | + * @param triples the set of triples |
| 29 | + * @return a set containing the transitive closure of the triples |
| 30 | + */ |
| 31 | + def computeTransitiveClosure(triples: mutable.Set[RDFTriple]): mutable.Set[RDFTriple] = { |
| 32 | + val tc = addTransitive(triples) |
| 33 | + // recursive call if set changed, otherwise stop and return |
| 34 | + if (tc.size == triples.size) triples else computeTransitiveClosure(tc) |
| 35 | + } |
| 36 | + |
| 37 | + // def addTransitive[A, B, C](s: mutable.Set[(A, B, C)]) = { |
| 38 | + // s ++ (for ((s1, p1, o1) <- s; (s2, p2, o2) <- s if o1 == s2) yield (s1, p1, o2)) |
| 39 | + // } |
| 40 | + |
| 41 | + private def addTransitive(triples: mutable.Set[RDFTriple]) = { |
| 42 | + triples ++ (for (t1 <- triples; t2 <- triples if t1.`object` == t2.subject) yield RDFTriple(t1.subject, t1.predicate, t2.`object`)) |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Computes the transitive closure on an RDD of triples. |
| 47 | + * Note, that the assumption is that all triples do have the same predicate. |
| 48 | + * |
| 49 | + * @param triples the RDD of triples |
| 50 | + * @return an RDD containing the transitive closure of the triples |
| 51 | + */ |
| 52 | + def computeTransitiveClosure(triples: RDD[RDFTriple]): RDD[RDFTriple] = { |
| 53 | + // get the predicate |
| 54 | + val predicate = triples.take(1)(0).predicate |
| 55 | + |
| 56 | + // compute TC |
| 57 | + computeTransitiveClosure(triples, predicate) |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Computes the transitive closure for the given predicate on an RDD of triples. |
| 62 | + * |
| 63 | + * @param triples the RDD of triples |
| 64 | + * @param predicate the predicate |
| 65 | + * @return an RDD containing the transitive closure of the triples |
| 66 | + */ |
| 67 | + def computeTransitiveClosure(triples: RDD[RDFTriple], predicate: String): RDD[RDFTriple] = { |
| 68 | + if(triples.isEmpty()) return triples |
| 69 | + log.info(s"computing TC for property $predicate...") |
| 70 | + |
| 71 | + profile { |
| 72 | + // compute the TC |
| 73 | + var subjectObjectPairs = triples.map(t => (t.subject, t.`object`)).cache() |
| 74 | + |
| 75 | + // because join() joins on keys, in addition the pairs are stored in reversed order (o, s) |
| 76 | + val objectSubjectPairs = subjectObjectPairs.map(t => (t._2, t._1)) |
| 77 | + |
| 78 | + // the join is iterated until a fixed point is reached |
| 79 | + var i = 1 |
| 80 | + var oldCount = 0L |
| 81 | + var nextCount = triples.count() |
| 82 | + do { |
| 83 | + log.info(s"iteration $i...") |
| 84 | + oldCount = nextCount |
| 85 | + // perform the join (s1, o1) x (o2, s2), obtaining an RDD of (s1=o2, (o1, s2)) pairs, |
| 86 | + // then project the result to obtain the new (s2, o1) paths. |
| 87 | + subjectObjectPairs = subjectObjectPairs |
| 88 | + .union(subjectObjectPairs.join(objectSubjectPairs).map(x => (x._2._2, x._2._1))) |
| 89 | + .filter(tuple => tuple._1 != tuple._2) // omit (s1, s1) |
| 90 | + .distinct() |
| 91 | + .cache() |
| 92 | + nextCount = subjectObjectPairs.count() |
| 93 | + i += 1 |
| 94 | + } while (nextCount != oldCount) |
| 95 | + |
| 96 | + log.info(s"TC for $predicate has " + nextCount + " triples.") |
| 97 | + subjectObjectPairs.map(p => new RDFTriple(p._1, predicate, p._2)) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Computes the transitive closure for an RDD of tuples |
| 103 | + * |
| 104 | + * @param edges the RDD of triples |
| 105 | + * @return an RDD containing the transitive closure of the tuples |
| 106 | + */ |
| 107 | + def computeTransitiveClosure[A:ClassTag](edges: RDD[(A, A)]): RDD[(A, A)] = { |
| 108 | + log.info("computing TC...") |
| 109 | + // we keep the transitive closure cached |
| 110 | + var tc = edges |
| 111 | + tc.cache() |
| 112 | + |
| 113 | + // because join() joins on keys, in addition the pairs are stored in reversed order (o, s) |
| 114 | + val edgesReversed = tc.map(t => (t._2, t._1)) |
| 115 | + |
| 116 | + // the join is iterated until a fixed point is reached |
| 117 | + var i = 1 |
| 118 | + var oldCount = 0L |
| 119 | + var nextCount = tc.count() |
| 120 | + do { |
| 121 | + log.info(s"iteration $i...") |
| 122 | + oldCount = nextCount |
| 123 | + // perform the join (x, y) x (y, x), obtaining an RDD of (x=y, (y, x)) pairs, |
| 124 | + // then project the result to obtain the new (x, y) paths. |
| 125 | + tc = tc |
| 126 | + .union(tc.join(edgesReversed).map(x => (x._2._2, x._2._1))) |
| 127 | + .distinct() |
| 128 | + .cache() |
| 129 | + nextCount = tc.count() |
| 130 | + i += 1 |
| 131 | + } while (nextCount != oldCount) |
| 132 | + |
| 133 | + println("TC has " + nextCount + " edges.") |
| 134 | + tc |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Computes the transitive closure for a Dataframe of triples |
| 139 | + * |
| 140 | + * @param edges the Dataframe of triples |
| 141 | + * @return a Dataframe containing the transitive closure of the triples |
| 142 | + */ |
| 143 | + def computeTransitiveClosure[A:ClassTag](edges: DataFrame): DataFrame = { |
| 144 | + log.info("computing TC...") |
| 145 | + |
| 146 | + profile { |
| 147 | + // we keep the transitive closure cached |
| 148 | + var tc = edges |
| 149 | + tc.cache() |
| 150 | + |
| 151 | + // the join is iterated until a fixed point is reached |
| 152 | + var i = 1 |
| 153 | + var oldCount = 0L |
| 154 | + var nextCount = tc.count() |
| 155 | + do { |
| 156 | + log.info(s"iteration $i...") |
| 157 | + oldCount = nextCount |
| 158 | + |
| 159 | + // val df1 = tc.alias("df1") |
| 160 | + // val df2 = tc.alias("df2") |
| 161 | + // perform the join (x, y) x (y, x), obtaining an RDD of (x=y, (y, x)) pairs, |
| 162 | + // then project the result to obtain the new (x, y) paths. |
| 163 | + |
| 164 | + tc.createOrReplaceTempView("SC") |
| 165 | + var joined = tc.sqlContext.sql("SELECT A.subject, A.predicate, B.object FROM SC A INNER JOIN SC B ON A.object = B.subject") |
| 166 | + |
| 167 | + // joined.explain() |
| 168 | + // var joined = df1.join(df2, df1("object") === df2("subject"), "inner") |
| 169 | + // println("JOINED:\n" + joined.collect().mkString("\n")) |
| 170 | + // joined = joined.select(df2(s"df1.$col1"), df1(s"df1.$col2")) |
| 171 | + // println(joined.collect().mkString("\n")) |
| 172 | + |
| 173 | + tc = tc |
| 174 | + .union(joined) |
| 175 | + .distinct() |
| 176 | + .cache() |
| 177 | + nextCount = tc.count() |
| 178 | + i += 1 |
| 179 | + } while (nextCount != oldCount) |
| 180 | + |
| 181 | + tc.sqlContext.uncacheTable("SC") |
| 182 | + log.info("TC has " + nextCount + " edges.") |
| 183 | + tc |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments