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

Commit db51f0d

Browse files
Scala Graph updates
1 parent 5d7d9b7 commit db51f0d

File tree

8 files changed

+59
-32
lines changed

8 files changed

+59
-32
lines changed

pom.xml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@
177177
<artifactId>jena-arq</artifactId>
178178
<version>${jena.version}</version>
179179
</dependency>
180+
<dependency>
181+
<groupId>org.apache.jena</groupId>
182+
<artifactId>jena-tdb</artifactId>
183+
<version>${jena.version}</version>
184+
</dependency>
185+
<dependency>
186+
<groupId>org.apache.jena</groupId>
187+
<artifactId>jena-cmds</artifactId>
188+
<version>${jena.version}</version>
189+
</dependency>
180190

181191
<!-- OWL API -->
182192
<dependency>
@@ -202,14 +212,14 @@
202212

203213
<!-- Graph API -->
204214
<dependency>
205-
<groupId>com.assembla.scala-incubator</groupId>
215+
<groupId>org.scala-graph</groupId>
206216
<artifactId>graph-core_${scala.binary.version}</artifactId>
207-
<version>1.10.0</version>
217+
<version>1.12.3</version>
208218
</dependency>
209219
<dependency>
210-
<groupId>com.assembla.scala-incubator</groupId>
220+
<groupId>org.scala-graph</groupId>
211221
<artifactId>graph-dot_${scala.binary.version}</artifactId>
212-
<version>1.9.0</version>
222+
<version>1.11.5</version>
213223
</dependency>
214224
<dependency>
215225
<groupId>org.jgrapht</groupId>

sansa-inference-common/pom.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@
3131
<dependency>
3232
<groupId>org.apache.jena</groupId>
3333
<artifactId>jena-tdb</artifactId>
34-
<version>3.5.0</version>
3534
</dependency>
3635
<dependency>
3736
<groupId>org.apache.jena</groupId>
3837
<artifactId>jena-cmds</artifactId>
39-
<version>3.5.0</version>
4038
</dependency>
4139

4240
<!-- Graph API -->
4341
<dependency>
44-
<groupId>com.assembla.scala-incubator</groupId>
42+
<groupId>org.scala-graph</groupId>
4543
<artifactId>graph-core_${scala.binary.version}</artifactId>
4644
</dependency>
4745
<dependency>
48-
<groupId>com.assembla.scala-incubator</groupId>
46+
<groupId>org.scala-graph</groupId>
4947
<artifactId>graph-dot_${scala.binary.version}</artifactId>
5048
</dependency>
5149
<dependency>
@@ -65,7 +63,7 @@
6563
<artifactId>google-collections</artifactId>
6664
</exclusion>
6765
</exclusions>
68-
<scope>provided</scope>
66+
<scope>compile</scope>
6967
</dependency>
7068

7169
<!-- Guava -->

sansa-inference-common/src/main/resources/log4j.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Root logger option
2-
log4j.rootLogger=INFO, stdout
2+
log4j.rootLogger=DEBUG, stdout
33

44
# Direct log messages to a log file
55
log4j.appender.file=org.apache.log4j.RollingFileAppender
@@ -20,3 +20,5 @@ log4j.logger.akka.remote.Remoting=ERROR
2020

2121
log4j.logger.org.apache.hadoop=ERROR
2222
log4j.logger.org.apache.calcite=ERROR
23+
24+
log4j.logger.scalax.collection.connectivity.GraphComponents=OFF

sansa-inference-common/src/main/scala/net/sansa_stack/inference/rules/RuleDependencyGraphAnalyzer.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ object RuleDependencyGraphAnalyzer extends Logging{
137137
def main(args: Array[String]) {
138138
// we re-use the JENA API for parsing rules
139139
val filenames = List(
140-
// "rules/rdfs-simple.rules"
141-
"rules/owl_horst.rules"
140+
"rules/rdfs-simple.rules"
141+
// "rules/owl_horst.rules"
142142
// "rules/owl_rl.rules"
143143
)
144144

@@ -154,7 +154,7 @@ object RuleDependencyGraphAnalyzer extends Logging{
154154

155155
// print each rule as graph
156156
rules.foreach { r =>
157-
val g = RuleUtils.asGraph(r).export(new File(graphDir, r.getName + ".graphml").toString)
157+
RuleUtils.asGraph(r).export(new File(graphDir, r.getName + ".graphml").toString)
158158
}
159159

160160
// generate graph

sansa-inference-common/src/main/scala/net/sansa_stack/inference/rules/RuleDependencyGraphGenerator.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.util.stream.Collectors
55
import scala.collection
66
import scala.collection.JavaConverters._
77
import scala.language.{existentials, implicitConversions}
8+
89
import scalax.collection.GraphPredef._
910
import scalax.collection.GraphTraversal.Parameters
1011
import scalax.collection._
@@ -13,7 +14,6 @@ import scalax.collection.edge._
1314
import scalax.collection.mutable.DefaultGraphImpl
1415
import scalax.collection.GraphPredef._
1516
import scalax.collection.GraphEdge._
16-
1717
import org.apache.jena.graph.{Node, NodeFactory}
1818
import org.apache.jena.reasoner.TriplePattern
1919
import org.apache.jena.reasoner.rulesys.Rule
@@ -259,12 +259,12 @@ object RuleDependencyGraphGenerator extends Logging {
259259
pairsOfRules :+= (cycle.last, cycle(0))
260260

261261
// map to list of edges
262-
val edges: Buffer[graph.EdgeT] = pairsOfRules.map(e => {
262+
val edges: Buffer[graph.EdgeT] = pairsOfRules.flatMap(e => {
263263
val node1 = graph get e._1
264264
val node2 = graph get e._2
265265

266266
node1.outgoing.filter(_.target == node2)
267-
}).flatten
267+
})
268268
debug("Edges: " + edges.mkString(", "))
269269

270270
// map to edge labels, i.e. the predicates

sansa-inference-common/src/main/scala/net/sansa_stack/inference/rules/minimizer/RuleDependencyGraphMinimizer.scala

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package net.sansa_stack.inference.rules.minimizer
22
import scala.collection.mutable.{ArrayBuffer, Buffer}
3+
34
import scalax.collection.Graph
45
import scalax.collection.edge.LDiEdge
56
import scala.collection.JavaConverters._
6-
import scalax.collection.GraphTraversal.Parameters
7+
import scala.collection.mutable
78

9+
import scalax.collection.GraphTraversal.Parameters
810
import org.apache.jena.graph.{Node, NodeFactory}
911
import org.apache.jena.reasoner.TriplePattern
1012
import org.apache.jena.reasoner.rulesys.Rule
@@ -16,7 +18,6 @@ import net.sansa_stack.inference.rules.RuleDependencyGraphGenerator.{asString, d
1618
import net.sansa_stack.inference.utils.{GraphUtils, RuleUtils}
1719
import net.sansa_stack.inference.utils.graph.LabeledEdge
1820
import net.sansa_stack.inference.utils.RuleUtils._
19-
2021
import scalax.collection.GraphTraversal.Parameters
2122
import scalax.collection._
2223
import scalax.collection.edge.Implicits._
@@ -294,11 +295,13 @@ abstract class RuleDependencyGraphMinimizer extends MinimizationRuleExecutor {
294295
// debug(cycles.asScala.mkString(","))
295296

296297
// cycles that contain the current node
297-
val cyclesWithNode: Buffer[Buffer[Rule]] = allCycles.asScala.filter(cycle => cycle.contains(node.value)).map(cycle => cycle.asScala)
298+
val cyclesWithNode: mutable.Buffer[mutable.Buffer[Rule]] = allCycles.asScala
299+
.filter(cycle => cycle.contains(node.value))
300+
.map(cycle => cycle.asScala)
298301
debug("Cycles: " + cyclesWithNode.map(c => c.map(r => r.getName)).mkString(","))
299302

300303
// cycles that use the same property
301-
val cyclesWithNodeSameProp: Map[Node, scala.List[Buffer[graph.EdgeT]]] = cyclesWithNode.map(cycle => {
304+
val cyclesWithNodeSameProp: Map[Node, scala.List[mutable.Buffer[graph.EdgeT]]] = cyclesWithNode.map(cycle => {
302305

303306
debug("Cycle: " + cycle.map(r => r.getName).mkString(", "))
304307

@@ -307,12 +310,12 @@ abstract class RuleDependencyGraphMinimizer extends MinimizationRuleExecutor {
307310
pairsOfRules :+= (cycle.last, cycle(0))
308311

309312
// map to list of edges
310-
val edges: Buffer[graph.EdgeT] = pairsOfRules.map(e => {
313+
val edges: mutable.Buffer[graph.EdgeT] = pairsOfRules.flatMap(e => {
311314
val node1 = graph get e._1
312315
val node2 = graph get e._2
313316

314317
node1.outgoing.filter(_.target == node2)
315-
}).flatten
318+
})
316319
debug("Edges: " + edges.mkString(", "))
317320

318321
// map to edge labels, i.e. the predicates
@@ -325,9 +328,14 @@ abstract class RuleDependencyGraphMinimizer extends MinimizationRuleExecutor {
325328
if (samePred) Some(predicates(0), edges) else None
326329
}).filter(_.isDefined).map(_.get).groupBy(e => e._1).mapValues(e => e.map(x => x._2).toList)
327330

328-
var removedCycles: collection.mutable.Set[Buffer[graph.EdgeT]] = collection.mutable.Set()
331+
var removedCycles: collection.mutable.Set[mutable.Buffer[graph.EdgeT]] = collection.mutable.Set()
329332

330-
val tmp: Map[Node, Map[Int, List[Buffer[graph.EdgeT]]]] = cyclesWithNodeSameProp.mapValues(value => value.map(cycle => (cycle.size, cycle)).groupBy(_._1).mapValues(e => e.map(x => x._2).toList))
333+
val tmp: Map[Node, Map[Int, List[mutable.Buffer[graph.EdgeT]]]] =
334+
cyclesWithNodeSameProp
335+
.mapValues(value =>
336+
value.map(cycle => (cycle.size, cycle))
337+
.groupBy(_._1)
338+
.mapValues(e => e.map(x => x._2)))
331339

332340
tmp.foreach(predicate2Cycles => {
333341
debug("predicate: " + predicate2Cycles._1)

sansa-inference-common/src/main/scala/net/sansa_stack/inference/utils/GraphUtils.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.apache.jena.graph.Node
88
import org.apache.jena.reasoner.TriplePattern
99
import org.apache.jena.reasoner.rulesys.Rule
1010
import org.apache.jena.shared.PrefixMapping
11+
import org.apache.jena.sparql.util.FmtUtils
1112
import org.gephi.graph.api.GraphController
1213
import org.gephi.io.exporter.api.ExportController
1314
import org.gephi.io.exporter.preview.PDFExporter
@@ -21,6 +22,7 @@ import org.gephi.project.api.ProjectController
2122
import org.jgrapht.{DirectedGraph, Graph}
2223
import org.jgrapht.alg.isomorphism.VF2GraphIsomorphismInspector
2324
import org.openide.util.Lookup
25+
2426
import net.sansa_stack.inference.utils.graph.{EdgeEquivalenceComparator, LabeledEdge, NodeEquivalenceComparator}
2527
import org.jgrapht.graph.{DefaultDirectedGraph, DirectedPseudograph}
2628
import org.jgrapht.io.{ComponentNameProvider, GraphMLExporter, IntegerComponentNameProvider};
@@ -87,7 +89,8 @@ object GraphUtils {
8789
* @param graph the 'Graph for Scala' graph
8890
* @return the JGraphT graph
8991
*/
90-
def asJGraphtRuleSetGraph(graph: scalax.collection.mutable.Graph[Rule, LDiEdge]): Graph[Rule, LabeledEdge[Rule, TriplePattern]] = {
92+
def asJGraphtRuleSetGraph(graph: scalax.collection.mutable.Graph[Rule, LDiEdge],
93+
showInFlowDirection: Boolean = false): Graph[Rule, LabeledEdge[Rule, TriplePattern]] = {
9194
val g = new DefaultDirectedGraph[Rule, LabeledEdge[Rule, TriplePattern]](classOf[LabeledEdge[Rule, TriplePattern]])
9295

9396
val edges = graph.edges.toList
@@ -100,7 +103,11 @@ object GraphUtils {
100103

101104
val label = e.label.asInstanceOf[TriplePattern]
102105

103-
g.addEdge(s, t, LabeledEdge[Rule, TriplePattern](s, t, label))
106+
if (showInFlowDirection) {
107+
g.addEdge(t, s, LabeledEdge[Rule, TriplePattern](t, s, label))
108+
} else {
109+
g.addEdge(s, t, LabeledEdge[Rule, TriplePattern](s, t, label))
110+
}
104111

105112
}
106113

@@ -115,9 +122,10 @@ object GraphUtils {
115122
*
116123
* @param filename the target file
117124
*/
118-
def export(filename: String, showInFlowDirection: Boolean = false): Unit = {
125+
def export(filename: String, showInFlowDirection: Boolean = false,
126+
prefixMapping: PrefixMapping = PrefixMapping.Standard): Unit = {
119127

120-
val g: Graph[Rule, LabeledEdge[Rule, TriplePattern]] = asJGraphtRuleSetGraph(graph)
128+
val g: Graph[Rule, LabeledEdge[Rule, TriplePattern]] = asJGraphtRuleSetGraph(graph, showInFlowDirection)
121129

122130
// In order to be able to export edge and node labels and IDs,
123131
// we must implement providers for them
@@ -136,7 +144,8 @@ object GraphUtils {
136144
}
137145

138146
val edgeLabelProvider = new ComponentNameProvider[LabeledEdge[Rule, TriplePattern]]() {
139-
override def getName(e: LabeledEdge[Rule, TriplePattern]): String = e.label.toString
147+
override def getName(e: LabeledEdge[Rule, TriplePattern]): String =
148+
FmtUtils.stringForNode(e.label.getPredicate, prefixMapping)
140149
}
141150

142151
// val exporter = new GraphMLExporter[String,LabeledEdge](

sansa-inference-common/src/test/scala/net/sansa_stack/inference/common/DependencyGraphTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object DependencyGraphTest {
1919
// val names = Seq("rdfs5", "rdfs7", "rdfp3", "rdfp4") // property TC rule + some instance rules
2020

2121
// define the rules
22-
val rules = RuleSets.OWL_HORST//.filter(r => names.contains(r.getName))
22+
val rules = RuleSets.OWL_HORST.filter(r => names.contains(r.getName))
2323
val profile = ReasoningProfile.OWL_HORST
2424
// val rules = RuleSets.RDFS_SIMPLE
2525
// val profile = ReasoningProfile.RDFS_SIMPLE
@@ -31,10 +31,10 @@ object DependencyGraphTest {
3131

3232
// generate the rule dependency graph
3333
var dependencyGraph = RuleDependencyGraphGenerator.generate(rules)
34-
dependencyGraph.export(s"${path}/rdg-${profile}.graphml")
34+
dependencyGraph.export(s"${path}/rdg-${profile}.graphml", true)
3535

3636
dependencyGraph = minimizer.execute(dependencyGraph) // RuleDependencyGraphGenerator.generate(rules, pruned = true)
37-
dependencyGraph.export(s"${path}/rdg-${profile}-pruned.graphml")
37+
dependencyGraph.export(s"${path}/rdg-${profile}-pruned.graphml", true)
3838
// dependencyGraph.exportAsPDF(s"${path}/rdg-${profile}-pruned.pdf")
3939

4040
// generate the high-level dependency graph

0 commit comments

Comments
 (0)