@@ -156,6 +156,7 @@ public Expression simplifyOCL()
156156 Expression elsestat = elseExp .simplifyOCL ();
157157
158158 Expression testexpr = test .simplifyOCL ();
159+ testexpr .setBrackets (false );
159160
160161 if ((testexpr + "" ).equals ("true" ))
161162 { return ifstat ; }
@@ -168,16 +169,59 @@ public Expression simplifyOCL()
168169 { BinaryExpression testbe = (BinaryExpression ) testexpr ;
169170 BinaryExpression elsebe = (BinaryExpression ) elsestat ;
170171
172+ Expression testbeLeft = testbe .getLeft ();
173+ Expression testbeRight = testbe .getRight ();
174+ testbeLeft .setBrackets (false );
175+ testbeRight .setBrackets (false );
176+
177+ Expression elsebeLeft = elsebe .getLeft ();
178+ Expression elsebeRight = elsebe .getRight ();
179+ elsebeLeft .setBrackets (false );
180+ elsebeRight .setBrackets (false );
181+
182+ ifstat .setBrackets (false );
183+
171184 if ("->includes" .equals (testbe .getOperator ()) &&
172- (testbe . getLeft () + "" ).equals (ifstat + "" ) &&
185+ (testbeLeft + "" ).equals (ifstat + "" ) &&
173186 ifExp .hasSetType () &&
174- (testbe . getLeft () + "" ).equals (elsebe . getLeft () + "" ) &&
175- (testbe . getRight () + "" ).equals (elsebe . getRight () + "" ))
187+ (testbeLeft + "" ).equals (elsebeLeft + "" ) &&
188+ (testbeRight + "" ).equals (elsebeRight + "" ))
176189 { if (elsebe .getOperator ().equals ("->including" ) ||
177190 elsebe .getOperator ().equals ("->append" ))
178191 { return elsebe ; }
179192 }
180- } // redundant if-then-else-endif
193+ } // redundant if-then-else-endif, though not for OrderedSet
194+
195+ if (testexpr instanceof BinaryExpression &&
196+ ifstat instanceof BinaryExpression )
197+ { // if s->excludes(x) then s->including(x) else s endif
198+ // if s->excludes(x) then s->append(x) else s endif
199+
200+ BinaryExpression testbe = (BinaryExpression ) testexpr ;
201+ BinaryExpression ifbe = (BinaryExpression ) ifstat ;
202+
203+ Expression testbeLeft = testbe .getLeft ();
204+ Expression testbeRight = testbe .getRight ();
205+ testbeLeft .setBrackets (false );
206+ testbeRight .setBrackets (false );
207+
208+ Expression ifbeLeft = ifbe .getLeft ();
209+ Expression ifbeRight = ifbe .getRight ();
210+ ifbeLeft .setBrackets (false );
211+ ifbeRight .setBrackets (false );
212+
213+ elseExp .setBrackets (false );
214+
215+ if ("->excludes" .equals (testbe .getOperator ()) &&
216+ (testbeLeft + "" ).equals (elsestat + "" ) &&
217+ elseExp .hasSetType () &&
218+ (testbeLeft + "" ).equals (ifbeLeft + "" ) &&
219+ (testbeRight + "" ).equals (ifbeRight + "" ))
220+ { if (ifbe .getOperator ().equals ("->including" ) ||
221+ ifbe .getOperator ().equals ("->append" ))
222+ { return ifbe ; }
223+ }
224+ }
181225
182226 return new ConditionalExpression (testexpr , ifstat , elsestat );
183227 }
@@ -195,28 +239,87 @@ public Map energyUse(Map res, Vector rUses, Vector oUses)
195239 elseExp .energyUse (res ,rUses ,oUses );
196240
197241 // if s->includes(x) then s else s->including(x) endif
242+ // if s->includes(x) then s else s->append(x) endif
243+
198244 // for s : Sequence is a potential issue. s : OrderedSet
199245 // or s : Set could be more efficient if uniqueness needed
200246
201- if (test instanceof BinaryExpression &&
247+ Expression testSimplified = test ;
248+ if (test instanceof UnaryExpression &&
249+ "not" .equals (((UnaryExpression ) test ).getOperator ()))
250+ { testSimplified =
251+ Expression .negate (((UnaryExpression ) test ).getArgument ());
252+ }
253+
254+ if (testSimplified instanceof BinaryExpression &&
202255 elseExp instanceof BinaryExpression )
203- { BinaryExpression testbe = (BinaryExpression ) test ;
256+ { BinaryExpression testbe =
257+ (BinaryExpression ) testSimplified ;
204258 BinaryExpression elsebe = (BinaryExpression ) elseExp ;
205259
260+ Expression testbeLeft = testbe .getLeft ();
261+ Expression testbeRight = testbe .getRight ();
262+ testbeLeft .setBrackets (false );
263+ testbeRight .setBrackets (false );
264+
265+ Expression elsebeLeft = elsebe .getLeft ();
266+ Expression elsebeRight = elsebe .getRight ();
267+ elsebeLeft .setBrackets (false );
268+ elsebeRight .setBrackets (false );
269+
270+ ifExp .setBrackets (false );
271+
206272 if ("->includes" .equals (testbe .getOperator ()) &&
207- (testbe . getLeft () + "" ).equals (ifExp + "" ) &&
273+ (testbeLeft + "" ).equals (ifExp + "" ) &&
208274 ifExp .hasSequenceType () &&
209- (testbe . getLeft () + "" ).equals (elsebe . getLeft () + "" ) &&
210- (testbe . getRight () + "" ).equals (elsebe . getRight () + "" ))
275+ (testbeLeft + "" ).equals (elsebeLeft + "" ) &&
276+ (testbeRight + "" ).equals (elsebeRight + "" ))
211277 { if (elsebe .getOperator ().equals ("->including" ) ||
212278 elsebe .getOperator ().equals ("->append" ))
213279 { rUses .add ("!! Using sequence " + ifExp + " as set in " + this + "\n >> Recommend declaring " + ifExp + " as a Set or SortedSet" );
280+
281+ int oscore = (int ) res .get ("red" );
282+ res .set ("red" , oscore + 1 );
283+ }
284+ }
285+ }
286+ else if (testSimplified instanceof BinaryExpression &&
287+ ifExp instanceof BinaryExpression )
288+ { // if s->excludes(x) then s->including(x) else s endif
289+ // if s->excludes(x) then s->append(x) else s endif
290+
291+ BinaryExpression testbe =
292+ (BinaryExpression ) testSimplified ;
293+ BinaryExpression ifbe = (BinaryExpression ) ifExp ;
294+
295+ Expression testbeLeft = testbe .getLeft ();
296+ Expression testbeRight = testbe .getRight ();
297+ testbeLeft .setBrackets (false );
298+ testbeRight .setBrackets (false );
299+
300+ Expression ifbeLeft = ifbe .getLeft ();
301+ Expression ifbeRight = ifbe .getRight ();
302+ ifbeLeft .setBrackets (false );
303+ ifbeRight .setBrackets (false );
304+
305+ elseExp .setBrackets (false );
306+
307+ if ("->excludes" .equals (testbe .getOperator ()) &&
308+ (testbeLeft + "" ).equals (elseExp + "" ) &&
309+ elseExp .hasSequenceType () &&
310+ (testbeLeft + "" ).equals (ifbeLeft + "" ) &&
311+ (testbeRight + "" ).equals (ifbeRight + "" ))
312+ { if (ifbe .getOperator ().equals ("->including" ) ||
313+ ifbe .getOperator ().equals ("->append" ))
314+ { rUses .add ("!! Using sequence " + elseExp + " as set in " + this + "\n >> Recommend declaring " + elseExp + " as a Set or SortedSet" );
315+
214316 int oscore = (int ) res .get ("red" );
215317 res .set ("red" , oscore + 1 );
216318 }
217319 }
218320 }
219321
322+
220323 return res ;
221324 }
222325
0 commit comments