Skip to content

Commit e6f5532

Browse files
committed
Added comments about using the zoo of Option operations.
1 parent 2cd9448 commit e6f5532

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/main/scala/net/walend/graph/semiring/AllPathsFirstSteps.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,14 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
109109
def leastPathsOfInnerNodes(fromInner:Option[leastPathDigraph.InnerNodeType],
110110
toInner:Option[leastPathDigraph.InnerNodeType]):Seq[Path] = {
111111
val fromToOption: Option[(leastPathDigraph.InnerNodeType, leastPathDigraph.InnerNodeType)] = for (f <- fromInner; t <- toInner) yield (f, t)
112+
//If fromToOption is None, then one node or the other isn't in the graph. Return no Path
112113
fromToOption.fold(Seq.empty[Path])(fromTo => {
113114
val label: Label = leastPathDigraph.label(fromTo._1, fromTo._2)
115+
//If label is None, then there's no path from one node to the other. Return no Path
114116
label.fold(Seq.empty[Path])(firstSteps => {
115117
if (firstSteps.choices == Set.empty) Seq(Seq(fromTo._2)) //No further steps. from should be to and the label should be I
116118
else {
119+
//Follow each choice and prepend the node this starts from
117120
for (choice <- firstSteps.choices.to[Seq]) yield {
118121
val tails: Seq[Path] = leastPathsOfInnerNodes(leastPathDigraph.innerNode(choice), toInner)
119122
for (tail <- tails) yield {
@@ -128,7 +131,6 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
128131
val fromInner = leastPathDigraph.innerNode(from)
129132
val toInner = leastPathDigraph.innerNode(to)
130133

131-
//prepend fromInner to all?
132134
leastPathsOfInnerNodes(fromInner,toInner)
133135
}
134136
/**
@@ -142,6 +144,8 @@ class AllPathsFirstSteps[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLab
142144
def recurse(innerFrom:labelGraph.InnerNodeType,innerTo:labelGraph.InnerNodeType):Set[(labelGraph.InnerNodeType,labelGraph.InnerNodeType,Label)] = {
143145
val label:Label = labelGraph.label(innerFrom,innerTo)
144146

147+
//if the label is None then return an empty set
148+
//otherwise, follow the choices
145149
label.fold(Set.empty[(labelGraph.InnerNodeType,labelGraph.InnerNodeType,Label)])(firstSteps => {
146150
val innerChoices = firstSteps.choices.map(choice => labelGraph.innerNode(choice).get)
147151
val closeEdges:Set[(labelGraph.InnerNodeType,labelGraph.InnerNodeType,Label)] = innerChoices.map((innerFrom,_,label))

src/main/scala/net/walend/graph/semiring/OnePathFirstStep.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ class OnePathFirstStep[Node,CoreLabel,Key](coreSupport:SemiringSupport[CoreLabel
106106
def leastPathOfInnerNodes(fromInner:Option[leastPathDigraph.InnerNodeType],
107107
toInner:Option[leastPathDigraph.InnerNodeType]):Option[Seq[leastPathDigraph.InnerNodeType]] = {
108108
val fromToOption: Option[(leastPathDigraph.InnerNodeType, leastPathDigraph.InnerNodeType)] = for (f <- fromInner; t <- toInner) yield (f, t)
109+
//If from or to is not in the digraph, return None
109110
fromToOption.flatMap(fromTo => {
110111
val label:Label = leastPathDigraph.label(fromTo._1,fromTo._2)
112+
//If label is None then no Path exists, return None
111113
label.flatMap(firstStep => {
114+
//If there's no step then from == to, return an empty path
115+
//Else follow the path
112116
firstStep.step.fold(Option(Seq.empty[leastPathDigraph.InnerNodeType]))(step => {
113117
val tailOption:Option[Seq[leastPathDigraph.InnerNodeType]] = leastPathOfInnerNodes(leastPathDigraph.innerNode(step),toInner)
114118
tailOption.flatMap(tail => {val iNodeOption = leastPathDigraph.innerNode(step)

0 commit comments

Comments
 (0)