Skip to content

Commit

Permalink
Split analysis methods into separate utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tomerk committed Apr 25, 2016
1 parent bb1f6cf commit 7cc71dc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 62 deletions.
59 changes: 59 additions & 0 deletions src/main/scala/internals/AnalysisUtils.scala
@@ -0,0 +1,59 @@
package internals

object AnalysisUtils {
def linearize(graph: Graph): Seq[GraphId] = {
def linearize(graphId: GraphId): Seq[GraphId] = {
val deps: Seq[GraphId] = graphId match {
case source: SourceId => Seq()
case node: NodeId => graph.getDependencies(node)
case sink: SinkId => Seq(graph.getSinkDependency(sink))
}
deps.foldLeft(Seq[GraphId]()) {
case (linearization, dep) => if (!linearization.contains(dep)) {
linearization ++ linearize(dep).filter(id => !linearization.contains(id))
} else {
linearization
}
} :+ graphId
}

graph.sinks.foldLeft(Seq[GraphId]()) {
case (linearization, sink) => {
linearization ++ linearize(sink).filter(id => !linearization.contains(id))
}
}
}

def getChildren(graph: Graph, node: GraphId): Set[GraphId] = {
node match {
case id: NodeOrSourceId => {
val childrenNodes = graph.dependencies.filter(_._2.contains(id)).keySet
val childrenSinks = graph.sinkDependencies.filter(_._2 == id).keySet
childrenNodes ++ childrenSinks
}
case sinkId: SinkId => Set()
}
}

def getDescendants(graph: Graph, node: GraphId): Set[GraphId] = {
val children = getChildren(graph, node)
children.map {
child => getDescendants(graph, child) + child
}.fold(Set())(_ union _)
}

def getParents(graph: Graph, node: GraphId): Set[NodeOrSourceId] = {
node match {
case source: SourceId => Set()
case node: NodeId => graph.getDependencies(node).toSet
case sink: SinkId => Set(graph.getSinkDependency(sink))
}
}

def getAncestors(graph: Graph, node: GraphId): Set[NodeOrSourceId] = {
val parents = getParents(graph, node)
parents.map {
parent => getAncestors(graph, parent) + parent
}.fold(Set())(_ union _)
}
}
62 changes: 0 additions & 62 deletions src/main/scala/internals/Graph.scala
@@ -1,11 +1,5 @@
package internals

sealed trait GraphId { val id: Long }
sealed trait NodeOrSourceId extends GraphId
case class NodeId(id: Long) extends NodeOrSourceId
case class SourceId(id: Long) extends NodeOrSourceId
case class SinkId(id: Long) extends GraphId

class Graph(
val sources: Set[SourceId],
val sinkDependencies: Map[SinkId, NodeOrSourceId],
Expand Down Expand Up @@ -243,60 +237,4 @@ class Graph(
case (graph, sink) => graph.removeSink(sink)
}
}

def linearize(): Seq[GraphId] = {
def linearize(graphId: GraphId): Seq[GraphId] = {
val deps: Seq[GraphId] = graphId match {
case source: SourceId => Seq()
case node: NodeId => getDependencies(node)
case sink: SinkId => Seq(getSinkDependency(sink))
}
deps.foldLeft(Seq[GraphId]()) {
case (linearization, dep) => if (!linearization.contains(dep)) {
linearization ++ linearize(dep).filter(id => !linearization.contains(id))
} else {
linearization
}
} :+ graphId
}

sinks.foldLeft(Seq[GraphId]()) {
case (linearization, sink) => {
linearization ++ linearize(sink).filter(id => !linearization.contains(id))
}
}
}

def getChildren(node: GraphId): Set[GraphId] = {
node match {
case id: NodeOrSourceId => {
val childrenNodes = dependencies.filter(_._2.contains(id)).keySet
val childrenSinks = sinkDependencies.filter(_._2 == id).keySet
childrenNodes ++ childrenSinks
}
case sinkId: SinkId => Set()
}
}

def getDescendants(node: GraphId): Set[GraphId] = {
val children = getChildren(node)
children.map {
child => getDescendants(child) + child
}.fold(Set())(_ union _)
}

def getParents(node: GraphId): Set[NodeOrSourceId] = {
node match {
case source: SourceId => Set()
case node: NodeId => getDependencies(node).toSet
case sink: SinkId => Set(getSinkDependency(sink))
}
}

def getAncestors(node: GraphId): Set[NodeOrSourceId] = {
val parents = getParents(node)
parents.map {
parent => getAncestors(parent) + parent
}.fold(Set())(_ union _)
}
}
11 changes: 11 additions & 0 deletions src/main/scala/internals/GraphId.scala
@@ -0,0 +1,11 @@
package internals

sealed trait GraphId { val id: Long }

case class SinkId(id: Long) extends GraphId

sealed trait NodeOrSourceId extends GraphId

case class NodeId(id: Long) extends NodeOrSourceId

case class SourceId(id: Long) extends NodeOrSourceId

0 comments on commit 7cc71dc

Please sign in to comment.