Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split analysis methods into separate utils
- Loading branch information
Showing
3 changed files
with
70 additions
and
62 deletions.
There are no files selected for viewing
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,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 _) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |