@@ -46,59 +46,121 @@ public class GeneratorLoader extends GeneratorParserBaseListener {
4646
4747 private StopCondition stopCondition = null ;
4848 private final List <PathGenerator > pathGenerators = new ArrayList <>();
49- private final List <StopCondition > stopConditions = new ArrayList <>();
49+
50+ static public class Node <T >{
51+ private T data = null ;
52+ private List <Node > children = new ArrayList <>();
53+ private Node parent = null ;
54+
55+ public Node (T data ) {
56+ this .data = data ;
57+ }
58+ public void addChild (Node child ) {
59+ child .setParent (this );
60+ this .children .add (child );
61+ }
62+
63+ public void addChild (T data ) {
64+ Node <T > newChild = new Node <>(data );
65+ this .addChild (newChild );
66+ }
67+
68+ public List <Node > getChildren () {
69+ return children ;
70+ }
71+
72+ public T getData () {
73+ return data ;
74+ }
75+
76+ private void setParent (Node parent ) {
77+ this .parent = parent ;
78+ }
79+ }
80+
81+ Node root = null ;
82+ Node currentNode = null ;
83+
84+ @ Override
85+ public void enterAndExpression (GeneratorParser .AndExpressionContext ctx ) {
86+ Node node = new Node (new CombinedCondition ());
87+ currentNode .addChild (node );
88+ currentNode = node ;
89+ }
5090
5191 @ Override
52- public void exitBooleanAndExpression (GeneratorParser .BooleanAndExpressionContext ctx ) {
53- if (!ctx .AND ().isEmpty ()) {
54- CombinedCondition combinedCondition = new CombinedCondition ();
55- stopConditions .forEach (combinedCondition ::addStopCondition );
56- stopCondition = combinedCondition ;
92+ public void exitAndExpression (GeneratorParser .AndExpressionContext ctx ) {
93+ CombinedCondition combinedCondition = (CombinedCondition ) currentNode .getData ();
94+ for ( Object child : currentNode .getChildren () ) {
95+ combinedCondition .addStopCondition ((StopCondition ) ((Node )child ).getData ());
5796 }
97+ currentNode = currentNode .parent ;
98+ }
99+
100+ @ Override
101+ public void enterOrExpression (GeneratorParser .OrExpressionContext ctx ) {
102+ Node node = new Node (new AlternativeCondition ());
103+ currentNode .addChild (node );
104+ currentNode = node ;
105+ }
106+
107+ @ Override
108+ public void exitOrExpression (GeneratorParser .OrExpressionContext ctx ) {
109+ AlternativeCondition alternativeCondition = (AlternativeCondition ) currentNode .getData ();
110+ for ( Object child : currentNode .getChildren () ) {
111+ alternativeCondition .addStopCondition ((StopCondition ) ((Node )child ).getData ());
112+ }
113+ currentNode = currentNode .parent ;
58114 }
59115
60116 @ Override
61117 public void exitStopCondition (GeneratorParser .StopConditionContext ctx ) {
62118 String conditionName = ctx .getChild (0 ).getText ().toLowerCase ();
63119 if ("never" .equals (conditionName )) {
64- stopConditions . add (new Never ());
120+ currentNode . addChild (new Never ());
65121 } else if ("edge_coverage" .equals (conditionName ) || "edgecoverage" .equals (conditionName )) {
66- stopConditions . add (new EdgeCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
122+ currentNode . addChild (new EdgeCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
67123 } else if ("vertex_coverage" .equals (conditionName ) || "vertexcoverage" .equals (conditionName )) {
68- stopConditions . add (new VertexCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
124+ currentNode . addChild (new VertexCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
69125 } else if ("reached_vertex" .equals (conditionName ) || "reachedvertex" .equals (conditionName )) {
70- stopConditions . add (new ReachedVertex (ctx .getChild (2 ).getText ()));
126+ currentNode . addChild (new ReachedVertex (ctx .getChild (2 ).getText ()));
71127 } else if ("reached_shared_state" .equals (conditionName ) || "reachedsharedstate" .equals (conditionName )) {
72- stopConditions . add (new ReachedSharedState (ctx .getChild (2 ).getText ()));
128+ currentNode . addChild (new ReachedSharedState (ctx .getChild (2 ).getText ()));
73129 } else if ("reached_edge" .equals (conditionName ) || "reachededge" .equals (conditionName )) {
74- stopConditions . add (new ReachedEdge (ctx .getChild (2 ).getText ()));
130+ currentNode . addChild (new ReachedEdge (ctx .getChild (2 ).getText ()));
75131 } else if ("time_duration" .equals (conditionName ) || "timeduration" .equals (conditionName )) {
76- stopConditions . add (new TimeDuration (Long .parseLong (ctx .getChild (2 ).getText ()), TimeUnit .SECONDS ));
132+ currentNode . addChild (new TimeDuration (Long .parseLong (ctx .getChild (2 ).getText ()), TimeUnit .SECONDS ));
77133 } else if ("dependency_edge_coverage" .equals (conditionName ) || "dependencyedgecoverage" .equals (conditionName )) {
78- stopConditions . add (new DependencyEdgeCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
134+ currentNode . addChild (new DependencyEdgeCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
79135 } else if ("requirement_coverage" .equals (conditionName ) || "requirementcoverage" .equals (conditionName )) {
80- stopConditions . add (new RequirementCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
136+ currentNode . addChild (new RequirementCoverage (Integer .parseInt (ctx .getChild (2 ).getText ())));
81137 } else if ("length" .equals (conditionName )) {
82- stopConditions . add (new Length (Integer .parseInt (ctx .getChild (2 ).getText ())));
138+ currentNode . addChild (new Length (Integer .parseInt (ctx .getChild (2 ).getText ())));
83139 } else if ("predefined_path" .equals (conditionName ) || "predefinedpath" .equals (conditionName )) {
84- stopConditions . add (new PredefinedPathStopCondition ());
140+ currentNode . addChild (new PredefinedPathStopCondition ());
85141 }
86142 }
87143
88- @ Override
89- public void exitLogicalExpression (GeneratorParser .LogicalExpressionContext ctx ) {
90- if (!ctx .OR ().isEmpty ()) {
91- AlternativeCondition alternativeCondition = new AlternativeCondition ();
92- stopConditions .forEach (alternativeCondition ::addStopCondition );
93- stopCondition = alternativeCondition ;
94- }
144+ public void enterGenerator (GeneratorParser .GeneratorContext context ) {
145+ root = new Node <StopCondition >(null );
146+ currentNode = root ;
95147 }
96148
97149 @ Override
98150 public void exitGenerator (GeneratorParser .GeneratorContext context ) {
99- if (stopConditions .size () == 1 ) {
100- stopCondition = stopConditions .get (0 );
151+ if ( root .getData () != null ) {
152+ stopCondition = (StopCondition )root .getData ();
153+ } else {
154+ if (root .getChildren ().size () == 0 ) {
155+ stopCondition = null ;
156+ } else {
157+ Node node = (Node ) root .getChildren ().get (0 );
158+ stopCondition = (StopCondition ) node .getData ();
159+ }
101160 }
161+
162+
163+
102164 String generatorName = context .getChild (0 ).getText ().toLowerCase ();
103165 if ("random" .equals (generatorName ) || "randompath" .equals (generatorName )) {
104166 pathGenerators .add (new RandomPath (stopCondition ));
@@ -118,7 +180,6 @@ public void exitGenerator(GeneratorParser.GeneratorContext context) {
118180 pathGenerators .add (pathGenerator );
119181 logger .debug ("Generator: " + pathGenerator .getClass ().getName () + " is loaded" );
120182 }
121- stopConditions .clear ();
122183 }
123184
124185 public PathGenerator getGenerator () {
0 commit comments