@@ -24,29 +24,27 @@ object ProcessCanonizer {
24
24
25
25
def uncanonize (
26
26
canonicalProcess : CanonicalProcess ,
27
- allowEndingScenarioWithoutSink : Boolean ,
27
+ missingSinkHandler : MissingSinkHandler ,
28
28
): ValidatedNel [ProcessUncanonizationError , EspProcess ] =
29
- uncanonizeArtificial(canonicalProcess, allowEndingScenarioWithoutSink ).toValidNel
29
+ uncanonizeArtificial(canonicalProcess, missingSinkHandler ).toValidNel
30
30
31
31
def uncanonizeArtificial (
32
32
canonicalProcess : CanonicalProcess ,
33
- allowEndingScenarioWithoutSink : Boolean ,
33
+ missingSinkHandler : MissingSinkHandler ,
34
34
): MaybeArtificial [EspProcess ] = {
35
35
36
36
val branches : MaybeArtificial [NonEmptyList [pl.touk.nussknacker.engine.graph.node.SourceNode ]] =
37
- canonicalProcess.allStartNodes.map(uncanonizeSource(_, allowEndingScenarioWithoutSink )).sequence
37
+ canonicalProcess.allStartNodes.map(uncanonizeSource(_)(missingSinkHandler )).sequence
38
38
39
39
branches.map(bList => EspProcess (canonicalProcess.metaData, bList))
40
40
}
41
41
42
42
private def uncanonizeSource (
43
43
canonicalNode : List [canonicalnode.CanonicalNode ],
44
- allowEndingScenarioWithoutSink : Boolean ,
45
- ): MaybeArtificial [node.SourceNode ] =
44
+ )(implicit missingSinkHandler : MissingSinkHandler ): MaybeArtificial [node.SourceNode ] =
46
45
canonicalNode match {
47
46
case (a @ canonicalnode.FlatNode (data : node.StartingNodeData )) :: tail =>
48
- uncanonize(a, tail, allowEndingScenarioWithoutSink).map(node.SourceNode (data, _))
49
-
47
+ uncanonize(a, tail).map(node.SourceNode (data, _))
50
48
case other :: _ =>
51
49
MaybeArtificial .artificialSource(InvalidRootNode (other.id))
52
50
@@ -57,98 +55,79 @@ object ProcessCanonizer {
57
55
private def uncanonize (
58
56
previous : canonicalnode.CanonicalNode ,
59
57
canonicalNode : List [canonicalnode.CanonicalNode ],
60
- allowEndingScenarioWithoutSink : Boolean ,
61
- ): MaybeArtificial [node.SubsequentNode ] =
58
+ )(implicit missingSinkHandler : MissingSinkHandler ): MaybeArtificial [Option [node.SubsequentNode ]] =
62
59
canonicalNode match {
63
60
case canonicalnode.FlatNode (data : node.BranchEndData ) :: Nil =>
64
- new MaybeArtificial (node.BranchEnd (data), Nil )
61
+ new MaybeArtificial (Some ( node.BranchEnd (data) ), Nil )
65
62
66
63
case canonicalnode.FlatNode (data : node.EndingNodeData ) :: Nil =>
67
- new MaybeArtificial (node.EndingNode (data), Nil )
64
+ new MaybeArtificial (Some ( node.EndingNode (data) ), Nil )
68
65
69
66
case (a @ canonicalnode.FlatNode (data : node.OneOutputSubsequentNodeData )) :: tail =>
70
- uncanonize(a, tail, allowEndingScenarioWithoutSink).map(node.OneOutputSubsequentNode (data, _))
71
-
67
+ uncanonize(a, tail).map(node.OneOutputSubsequentNode (data, _)).map(Some (_): Option [node.SubsequentNode ])
72
68
case (a @ canonicalnode.FilterNode (data, nextFalse)) :: tail if nextFalse.isEmpty =>
73
- uncanonize(a, tail, allowEndingScenarioWithoutSink ).map(nextTrue => node.FilterNode (data, Some ( nextTrue) , None ))
69
+ uncanonize(a, tail).map(nextTrue => Some ( node.FilterNode (data, nextTrue, None ) ))
74
70
75
71
case (a @ canonicalnode.FilterNode (data, nextFalse)) :: tail if tail.isEmpty =>
76
- uncanonize(a, nextFalse, allowEndingScenarioWithoutSink).map { nextFalseV =>
77
- node.FilterNode (data, None , Some (nextFalseV))
78
- }
72
+ uncanonize(a, nextFalse).map { nextFalseV => Some (node.FilterNode (data, None , nextFalseV)) }
79
73
80
74
case (a @ canonicalnode.FilterNode (data, nextFalse)) :: tail =>
81
- (uncanonize(a, tail, allowEndingScenarioWithoutSink), uncanonize(a, nextFalse, allowEndingScenarioWithoutSink))
82
- .mapN { (nextTrue, nextFalseV) =>
83
- node.FilterNode (data, Some (nextTrue), Some (nextFalseV))
84
- }
75
+ (uncanonize(a, tail), uncanonize(a, nextFalse)).mapN { (nextTrue, nextFalseV) =>
76
+ Some (node.FilterNode (data, nextTrue, nextFalseV))
77
+ }
85
78
86
79
case (a @ canonicalnode.SwitchNode (data, Nil , defaultNext)) :: Nil =>
87
- uncanonize(a, defaultNext, allowEndingScenarioWithoutSink).map { defaultNextV =>
88
- node.SwitchNode (data, Nil , Some (defaultNextV))
89
- }
80
+ uncanonize(a, defaultNext).map(defaultNextV => Some (node.SwitchNode (data, Nil , defaultNextV)))
90
81
91
82
case (a @ canonicalnode.SwitchNode (data, nexts, defaultNext)) :: Nil if defaultNext.isEmpty =>
92
83
nexts
93
- .map { casee =>
94
- uncanonize(a, casee.nodes, allowEndingScenarioWithoutSink).map(node.Case (casee.expression, _))
95
- }
84
+ .map(casee => uncanonize(a, casee.nodes).map(node.Case (casee.expression, _)))
96
85
.sequence[MaybeArtificial , node.Case ]
97
86
.map(node.SwitchNode (data, _, None ))
87
+ .map(Some (_))
98
88
99
89
case (a @ canonicalnode.SwitchNode (data, nexts, defaultNext)) :: Nil =>
100
90
val unFlattenNexts = nexts
101
- .map { casee =>
102
- uncanonize(a, casee.nodes, allowEndingScenarioWithoutSink).map(node.Case (casee.expression, _))
103
- }
91
+ .map(casee => uncanonize(a, casee.nodes).map(node.Case (casee.expression, _)))
104
92
.sequence[MaybeArtificial , node.Case ]
105
93
106
- (unFlattenNexts, uncanonize(a, defaultNext, allowEndingScenarioWithoutSink )).mapN { (nextsV, defaultNextV) =>
107
- node.SwitchNode (data, nextsV, Some ( defaultNextV))
94
+ (unFlattenNexts, uncanonize(a, defaultNext)).mapN { (nextsV, defaultNextV) =>
95
+ Some ( node.SwitchNode (data, nextsV, defaultNextV))
108
96
}
109
97
110
98
case canonicalnode.SplitNode (bare, Nil ) :: Nil =>
111
- handleMissingSink(bare.id, allowEndingScenarioWithoutSink )
99
+ missingSinkHandler. handleMissingSink(bare.id)
112
100
113
101
case (a @ canonicalnode.SplitNode (bare, nexts)) :: Nil =>
114
- nexts.map(uncanonize(a, _, allowEndingScenarioWithoutSink)).sequence[MaybeArtificial , node.SubsequentNode ].map {
115
- uncanonized =>
116
- node.SplitNode (bare, uncanonized)
117
- }
102
+ nexts
103
+ .map(uncanonize(a, _))
104
+ .sequence[MaybeArtificial , Option [node.SubsequentNode ]]
105
+ .map { uncanonized =>
106
+ Some (node.SplitNode (bare, uncanonized.flatten))
107
+ }
118
108
119
109
case invalidHead :: _ =>
120
110
MaybeArtificial .missingSinkError(InvalidTailOfBranch (invalidHead.id))
121
111
122
112
case Nil =>
123
- handleMissingSink(previous.id, allowEndingScenarioWithoutSink )
113
+ missingSinkHandler. handleMissingSink(previous.id)
124
114
}
125
115
126
- private def handleMissingSink (
127
- previousNodeId : String ,
128
- allowEndingScenarioWithoutSink : Boolean
129
- ): MaybeArtificial [node.SubsequentNode ] = {
130
- if (allowEndingScenarioWithoutSink) {
131
- MaybeArtificial .addedArtificialDeadEndSink(previousNodeId)
132
- } else {
133
- MaybeArtificial .missingSinkError(InvalidTailOfBranch (previousNodeId))
134
- }
135
- }
136
-
137
116
}
138
117
139
118
object NodeCanonizer {
140
119
141
120
def canonize (n : node.Node ): List [canonicalnode.CanonicalNode ] =
142
121
n match {
143
122
case oneOut : node.OneOutputNode =>
144
- canonicalnode.FlatNode (oneOut.data) :: canonize( oneOut.next)
123
+ canonicalnode.FlatNode (oneOut.data) :: oneOut.next.map(canonize).getOrElse( Nil )
145
124
case node.FilterNode (data, nextTrue, nextFalse) =>
146
125
canonicalnode.FilterNode (data, nextFalse.toList.flatMap(canonize)) :: nextTrue.toList.flatMap(canonize)
147
126
case node.SwitchNode (data, nexts, defaultNext) =>
148
127
canonicalnode.SwitchNode (
149
128
data = data,
150
129
nexts = nexts.map { next =>
151
- canonicalnode.Case (next.expression, canonize( next.node))
130
+ canonicalnode.Case (next.expression, next.node.map(canonize).getOrElse( Nil ))
152
131
},
153
132
defaultNext = defaultNext.toList.flatMap(canonize)
154
133
) :: Nil
0 commit comments