Skip to content

Commit fcb4f13

Browse files
authored
extended energy-use analysis
1 parent bee0751 commit fcb4f13

15 files changed

Lines changed: 307 additions & 59 deletions

ConditionalExpression.class

1.31 KB
Binary file not shown.

ConditionalExpression.java

Lines changed: 112 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

ConditionalStatement.class

1.54 KB
Binary file not shown.

Expression.class

184 Bytes
Binary file not shown.

Expression.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,7 +3269,14 @@ private static Expression simplifyNeq(final Expression e1,
32693269
public static Expression negate(Expression e)
32703270
{ if (e instanceof BinaryExpression)
32713271
{ BinaryExpression be = (BinaryExpression) e;
3272-
String op = be.operator;
3272+
String op = be.operator;
3273+
3274+
if (op.equals("->includes"))
3275+
{ return new BinaryExpression("->excludes", be.left, be.right); }
3276+
3277+
if (op.equals("->excludes"))
3278+
{ return new BinaryExpression("->includes", be.left, be.right); }
3279+
32733280
if (op.equals("&"))
32743281
{ Expression nleft = negate(be.left);
32753282
Expression nright = negate(be.right);
@@ -3278,55 +3285,69 @@ public static Expression negate(Expression e)
32783285
res.setBrackets(true);
32793286
return res;
32803287
}
3281-
else if (op.equals("or"))
3288+
3289+
if (op.equals("or"))
32823290
{ Expression nleft = negate(be.left);
32833291
Expression nright = negate(be.right);
32843292
Expression res =
32853293
new BinaryExpression("&", nleft, nright);
32863294
res.setBrackets(true);
32873295
return res;
32883296
}
3289-
else if (op.equals("->exists"))
3297+
3298+
if (op.equals("->exists"))
32903299
{ Expression nright = negate(be.right);
32913300
Expression res =
32923301
new BinaryExpression("->forAll", be.left, nright);
32933302
res.setBrackets(true);
32943303
return res;
32953304
}
3296-
else if (op.equals("->forAll"))
3305+
3306+
if (op.equals("->forAll"))
32973307
{ Expression nright = negate(be.right);
32983308
Expression res =
32993309
new BinaryExpression("->exists", be.left, nright);
33003310
res.setBrackets(true);
33013311
return res;
33023312
}
3303-
else if (op.equals("#"))
3313+
3314+
if (op.equals("#"))
33043315
{ Expression nright = negate(be.right);
33053316
Expression res =
33063317
new BinaryExpression("!", be.left, nright);
33073318
res.setBrackets(true);
33083319
return res;
33093320
}
3310-
else if (op.equals("!"))
3321+
3322+
if (op.equals("!"))
33113323
{ Expression nright = negate(be.right);
33123324
Expression res =
33133325
new BinaryExpression("#", be.left, nright);
33143326
res.setBrackets(true);
33153327
return res;
33163328
}
3317-
else if (op.equals("->includesAll"))
3329+
3330+
if (op.equals("->includesAll"))
33183331
{ return new BinaryExpression("/<:",be.right,be.left); }
33193332

33203333

33213334
String nop = negateOp(be.operator);
33223335
if (!(nop.equals(be.operator)))
33233336
{ return new BinaryExpression(nop,be.left,be.right); }
3337+
33243338
return new UnaryExpression("not",e);
33253339
}
33263340
else if (e instanceof UnaryExpression)
33273341
{ UnaryExpression ue = (UnaryExpression) e;
3342+
33283343
if (ue.operator.equals("not"))
33293344
{ return ue.argument; }
3345+
3346+
if (ue.operator.equals("->isEmpty"))
3347+
{ return new UnaryExpression("->notEmpty", ue.argument); }
3348+
3349+
if (ue.operator.equals("->notEmpty"))
3350+
{ return new UnaryExpression("->isEmpty", ue.argument); }
33303351
}
33313352
else if (e instanceof BasicExpression)
33323353
{ if (e.isTrueString())

FinalStatement.class

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)