-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#4] Refactoring Closure package and classes
- Loading branch information
Lucas Peres
committed
Apr 29, 2019
1 parent
e6e362d
commit 0424ada
Showing
5 changed files
with
139 additions
and
129 deletions.
There are no files selected for viewing
127 changes: 3 additions & 124 deletions
127
src/main/scala/br/ufc/insightlab/linkedgraphast/query/closure/Closure.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,10 @@ | ||
package br.ufc.insightlab.linkedgraphast.query.closure | ||
|
||
import scala.collection.JavaConverters._ | ||
import br.ufc.insightlab.graphast.model.Node | ||
import br.ufc.insightlab.linkedgraphast.model.graph.LinkedGraph | ||
import br.ufc.insightlab.linkedgraphast.model.link.{Attribute, Relation} | ||
import br.ufc.insightlab.linkedgraphast.model.node.{Literal, URI} | ||
import br.ufc.insightlab.linkedgraphast.query.mst.Prim | ||
import br.ufc.insightlab.graphast.model.{Edge, Node} | ||
import br.ufc.insightlab.graphast.query.shortestpath.DijkstraStrategy | ||
import org.slf4j.{Logger, LoggerFactory} | ||
|
||
import scala.util.{Success, Try} | ||
trait Closure { | ||
|
||
object Closure { | ||
|
||
private val log: Logger = LoggerFactory.getLogger(this.getClass) | ||
|
||
def apply(g: LinkedGraph)(nodes: List[Node]): LinkedGraph = { | ||
val graph = new LinkedGraph | ||
|
||
for (n <- nodes) graph.addNode(n) | ||
|
||
if (nodes.size == 1) graph | ||
else { | ||
g.getLinksAsStream | ||
.foreach(link => link match { | ||
case Relation(_, l, t) => | ||
if ((l.uri.endsWith("#type") && (t.uri.endsWith("#Class") || t.uri.endsWith("Property"))) || | ||
(l.uri.endsWith("#range") && t.uri.contains("XMLSchema#"))) | ||
link.setWeight(100) | ||
else if(l.uri.contains("rdf-schema#sub")) | ||
link.setWeight(10) | ||
|
||
case _ => | ||
}) | ||
|
||
|
||
val pairNodes = nodes | ||
.combinations(2) | ||
.map(e => (e.head.getId, e.tail.head.getId)) | ||
// .map(x => if(x._1 < x._2) x else (x._2,x._1)) | ||
.filterNot(x => x._1 == x._2) | ||
|
||
val dijkstra = new DijkstraStrategy(g) | ||
|
||
var modifiedEdges = List[(Edge,Double)]() | ||
|
||
var edges = Set[Edge]() | ||
|
||
for ((n1, n2) <- pairNodes) { | ||
log.debug(s"Computing path from ${g.getNode(n1)} to ${g.getNode(n2)}") | ||
val vector = dijkstra.run(n1, n2) | ||
val path: List[Long] = Try(vector | ||
.getPath(n2) | ||
.asScala | ||
.toList | ||
.map(_.asInstanceOf[Long])) match { | ||
|
||
case Success(p) => | ||
log.debug(s"Path found with cost ${vector.getDistance(n2)}: ${p.map(g.getNode).mkString(",")}") | ||
p | ||
case _ => | ||
log.debug("Path not found") | ||
List[Long](-1) | ||
} | ||
|
||
|
||
|
||
var src = path.head | ||
|
||
|
||
for (trg <- path.tail) { | ||
val edge = g.getEdge(src, trg) | ||
// println(edge) | ||
|
||
if(edge.getWeight >= 1) { | ||
modifiedEdges :+= (edge, edge.getWeight) | ||
edge.setWeight(0.5) | ||
} | ||
|
||
if (!graph.containsNode(src)) { | ||
val n = g.getNode(src) match { | ||
case URI(uri) => URI(uri) | ||
case Literal(v) => Literal(v) | ||
} | ||
graph.addNode(n) | ||
// log.debug(s"Adding node $src") | ||
} | ||
|
||
if (!graph.containsNode(trg)) { | ||
val n = g.getNode(trg) match { | ||
case URI(uri) => URI(uri) | ||
case Literal(v) => Literal(v) | ||
} | ||
graph.addNode(n) | ||
// log.debug(s"Adding node $src") | ||
} | ||
|
||
g.getEdge(src, trg) match { | ||
case e: Edge => | ||
edges += e | ||
|
||
case _ => | ||
val e = g.getEdge(trg, src) | ||
edges += e | ||
} | ||
src = trg | ||
} | ||
|
||
} | ||
|
||
|
||
for (e <- edges) { | ||
val l = e match { | ||
case Attribute(s,p,o) => Attribute(s,p,o) | ||
case Relation(s,p,o) => Relation(s,p,o) | ||
} | ||
l.setId(e.getId) | ||
graph.addEdge(l) | ||
} | ||
modifiedEdges.foreach(x => x._1.setWeight(x._2)) | ||
|
||
if(nodes.nonEmpty) | ||
Prim(graph)(nodes.head) | ||
else | ||
graph | ||
} | ||
} | ||
def apply(g: LinkedGraph)(nodes: List[Node]): LinkedGraph | ||
|
||
} | ||
|
131 changes: 131 additions & 0 deletions
131
src/main/scala/br/ufc/insightlab/linkedgraphast/query/closure/DijkstraClosure.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package br.ufc.insightlab.linkedgraphast.query.closure | ||
|
||
import scala.collection.JavaConverters._ | ||
import br.ufc.insightlab.linkedgraphast.model.graph.LinkedGraph | ||
import br.ufc.insightlab.linkedgraphast.model.link.{Attribute, Relation} | ||
import br.ufc.insightlab.linkedgraphast.model.node.{Literal, URI} | ||
import br.ufc.insightlab.linkedgraphast.query.mst.Prim | ||
import br.ufc.insightlab.graphast.model.{Edge, Node} | ||
import br.ufc.insightlab.graphast.query.shortestpath.DijkstraStrategy | ||
import org.slf4j.{Logger, LoggerFactory} | ||
|
||
import scala.util.{Success, Try} | ||
|
||
object DijkstraClosure extends Closure { | ||
|
||
private val log: Logger = LoggerFactory.getLogger(this.getClass) | ||
|
||
def apply(g: LinkedGraph)(nodes: List[Node]): LinkedGraph = { | ||
val graph = new LinkedGraph | ||
|
||
for (n <- nodes) graph.addNode(n) | ||
|
||
if (nodes.size == 1) graph | ||
else { | ||
g.getLinksAsStream | ||
.foreach(link => link match { | ||
case Relation(_, l, t) => | ||
if ((l.uri.endsWith("#type") && (t.uri.endsWith("#Class") || t.uri.endsWith("Property"))) || | ||
(l.uri.endsWith("#range") && t.uri.contains("XMLSchema#"))) | ||
link.setWeight(100) | ||
else if(l.uri.contains("rdf-schema#sub")) | ||
link.setWeight(10) | ||
|
||
case _ => | ||
}) | ||
|
||
|
||
val pairNodes = nodes | ||
.combinations(2) | ||
.map(e => (e.head.getId, e.tail.head.getId)) | ||
// .map(x => if(x._1 < x._2) x else (x._2,x._1)) | ||
.filterNot(x => x._1 == x._2) | ||
|
||
val dijkstra = new DijkstraStrategy(g) | ||
|
||
var modifiedEdges = List[(Edge,Double)]() | ||
|
||
var edges = Set[Edge]() | ||
|
||
for ((n1, n2) <- pairNodes) { | ||
log.debug(s"Computing path from ${g.getNode(n1)} to ${g.getNode(n2)}") | ||
val vector = dijkstra.run(n1, n2) | ||
val path: List[Long] = Try(vector | ||
.getPath(n2) | ||
.asScala | ||
.toList | ||
.map(_.asInstanceOf[Long])) match { | ||
|
||
case Success(p) => | ||
log.debug(s"Path found with cost ${vector.getDistance(n2)}: ${p.map(g.getNode).mkString(",")}") | ||
p | ||
case _ => | ||
log.debug("Path not found") | ||
List[Long](-1) | ||
} | ||
|
||
|
||
|
||
var src = path.head | ||
|
||
|
||
for (trg <- path.tail) { | ||
val edge = g.getEdge(src, trg) | ||
// println(edge) | ||
|
||
if(edge.getWeight >= 1) { | ||
modifiedEdges :+= (edge, edge.getWeight) | ||
edge.setWeight(0.5) | ||
} | ||
|
||
if (!graph.containsNode(src)) { | ||
val n = g.getNode(src) match { | ||
case URI(uri) => URI(uri) | ||
case Literal(v) => Literal(v) | ||
} | ||
graph.addNode(n) | ||
// log.debug(s"Adding node $src") | ||
} | ||
|
||
if (!graph.containsNode(trg)) { | ||
val n = g.getNode(trg) match { | ||
case URI(uri) => URI(uri) | ||
case Literal(v) => Literal(v) | ||
} | ||
graph.addNode(n) | ||
// log.debug(s"Adding node $src") | ||
} | ||
|
||
g.getEdge(src, trg) match { | ||
case e: Edge => | ||
edges += e | ||
|
||
case _ => | ||
val e = g.getEdge(trg, src) | ||
edges += e | ||
} | ||
src = trg | ||
} | ||
|
||
} | ||
|
||
|
||
for (e <- edges) { | ||
val l = e match { | ||
case Attribute(s,p,o) => Attribute(s,p,o) | ||
case Relation(s,p,o) => Relation(s,p,o) | ||
} | ||
l.setId(e.getId) | ||
graph.addEdge(l) | ||
} | ||
modifiedEdges.foreach(x => x._1.setWeight(x._2)) | ||
|
||
if(nodes.nonEmpty) | ||
Prim(graph)(nodes.head) | ||
else | ||
graph | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters