Skip to content

Commit

Permalink
OCL optimisation for energy use
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Aug 4, 2024
1 parent edd597f commit f4c58e1
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 9 deletions.
42 changes: 35 additions & 7 deletions Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -3083,10 +3083,15 @@ public String setOperationJava7(Entity ent, Vector cons,

Vector v = type.getValues();
String val = nme + "_x";

Attribute par =
new Attribute(val,type,ModelElement.INTERNAL);
par.setElementType(elementType);

BasicExpression attxbe = new BasicExpression(val);
// attxbe.setType(type);
// attxbe.setElementType(elementType);

Vector v1 = new Vector();
v1.add(par);
String t = type.getJava7(); // (elementType)
Expand All @@ -3101,6 +3106,9 @@ public String setOperationJava7(Entity ent, Vector cons,
BehaviouralFeature event =
new BehaviouralFeature("set" + nme,v1,false,null);

java.util.Map env = new java.util.HashMap();
env.put(ename, "this");

String qual = " ";
String code = "";
String sync = "";
Expand All @@ -3117,24 +3125,41 @@ else if (!instanceScope)
}
else
{ code = nme + " = " + val + ";"; } // controller sets static atts, once only

String precode = "";

for (int j = 0; j < cons.size(); j++)
// may be constraints of subclass ent
{ Constraint cc = (Constraint) cons.get(j);

if (cc.isDeltaConstraint(nme) &&
cc.dependsUpon(ename,nme))
{ Constraint cc2 =
(Constraint) cc.substituteEq(nme,attxbe);
String cccode = cc2.updateFormJava7(env,true);

System.out.println(">> Delta constraint " + cc + "\n" +
">> action for set" + nme + " is: " + cccode);
precode = precode + "\n" +
cccode + "\n";
}
}


String opheader;
opheader = "public" + sync + qual + "void set" + nme + "(" + t +
" " + val + ") { " + code;
" " + val + ") { " + precode + "\n " + code;

if (!instanceScope)
{ opheader = opheader + " }\n\n" +
"public" + sync + " void localSet" + nme + "(" + t +
" " + val + ") { ";
" " + val + ") { " + precode;
}

BasicExpression attxbe = new BasicExpression(val);

Vector contexts = new Vector();
contexts.add(ent);
java.util.Map env = new java.util.HashMap();
env.put(ename, "this");


for (int j = 0; j < cons.size(); j++)
// may be constraints of subclass ent
{ Constraint cc = (Constraint) cons.get(j);
Expand All @@ -3146,7 +3171,10 @@ else if (!instanceScope)
" depends on " + nme + " : " +
cc.dependsUpon(ename,nme)); */

if (cc.isBehavioural() &&
if (cc.isDeltaConstraint(nme) &&
cc.dependsUpon(ename,nme))
{ }
else if (cc.isBehavioural() &&
cc.dependsUpon(ename,nme))
{ String cccode = cc.updateFormJava7(env,true);

Expand Down
33 changes: 32 additions & 1 deletion BasicExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -17295,6 +17295,36 @@ public static void main(String[] args)

}

public Expression simplifyOCL()
{ // Replaces energy-expensive processing by simpler.

Expression objR = objectRef;
if (objectRef != null)
{ objR = objectRef.simplifyOCL(); }

Expression arrInd = arrayIndex;
if (arrayIndex != null)
{ arrInd = arrayIndex.simplifyOCL(); }

Vector pars = new Vector();
if (parameters != null)
{ for (int i = 0; i < parameters.size(); i++)
{ Expression par = (Expression) parameters.get(i);
pars.add(par.simplifyOCL());
}
}
else
{ pars = parameters; }

BasicExpression res = (BasicExpression) clone();
res.objectRef = objR;
res.arrayIndex = arrInd;
res.parameters = pars;
return res;
}



public Map energyUse(Map res, Vector rUses, Vector oUses)
{ // Calls to OclType reflection operators and OclDatasource
// connect are red flags.
Expand Down Expand Up @@ -17379,7 +17409,8 @@ public java.util.Map collectionOperatorUses(int level,
}

return res;
}
} // and in the parameters and object ref


public int syntacticComplexity()
{ int res = 0;
Expand Down
9 changes: 9 additions & 0 deletions BehaviouralFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -4239,6 +4239,15 @@ public Vector equivalentsUsedIn()
return res;
}

public void simplifyOCL()
{ if (pre != null)
{ pre = pre.simplifyOCL(); }
if (post != null)
{ post = post.simplifyOCL(); }
if (activity != null)
{ activity = activity.optimiseOCL(); }
}

public Map energyAnalysis(Vector redUses, Vector amberUses)
{ // Scan the postcondition/activity for energy expensive
// expressions/code
Expand Down
160 changes: 160 additions & 0 deletions BinaryExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -20080,6 +20080,166 @@ public Expression featureAdding2(String var, Vector remainder)
return null;
}

public Expression simplifyOCL()
{ // Double iterations ->select(...)->select(...)
// replaced

Expression lexpr = left.simplifyOCL();
Expression rexpr = right.simplifyOCL();


if (operator.equals("|"))
{ BinaryExpression arg = (BinaryExpression) left;
Expression domain = arg.getRight();
if (domain instanceof BinaryExpression)
{ BinaryExpression lbe = (BinaryExpression) domain;

if (lbe.operator.equals("->select"))
{ System.out.println(">> Inefficient nested select: " + this);
Expression newleft = lbe.getLeft();
Expression newright = lbe.getRight();
BasicExpression ref =
(BasicExpression) arg.getLeft();
Expression newpred =
Expression.simplifyAnd(
newright.addReference(ref, ref.getType()), right);
Expression newdomain =
new BinaryExpression(":", ref,
newleft);
return new BinaryExpression("|", newdomain, newpred);
}
else if (lbe.operator.equals("->reject"))
{ System.out.println(">> Inefficient nested select: " + this);
Expression newleft = lbe.getLeft();
Expression newright = lbe.getRight();
BasicExpression ref =
(BasicExpression) arg.getLeft();
Expression newpred =
Expression.simplifyAnd(
Expression.negate(newright.addReference(ref,
ref.getType())),
right);
Expression newdomain =
new BinaryExpression(":", ref,
newleft);
return new BinaryExpression("|", newdomain, newpred);
}
}
}
else if (operator.equals("|R"))
{ BinaryExpression arg = (BinaryExpression) left;
Expression domain = arg.getRight();
if (domain instanceof BinaryExpression)
{ BinaryExpression lbe = (BinaryExpression) domain;

if (lbe.operator.equals("->select"))
{ System.out.println(">> Inefficient nested select/reject: " + this);
Expression newleft = lbe.getLeft();
Expression newright = lbe.getRight();
BasicExpression ref =
(BasicExpression) arg.getLeft();
Expression newpred =
Expression.simplifyAnd(
newright.addReference(ref, ref.getType()),
Expression.negate(right));
Expression newdomain =
new BinaryExpression(":", ref,
newleft);
return new BinaryExpression("|", newdomain, newpred);
}
else if (lbe.operator.equals("->reject"))
{ System.out.println(">> Inefficient nested reject: " + this);
Expression newleft = lbe.getLeft();
Expression newright = lbe.getRight();
BasicExpression ref =
(BasicExpression) arg.getLeft();
Expression newpred =
Expression.simplifyOr(
newright.addReference(ref,
ref.getType()),
right);
Expression newdomain =
new BinaryExpression(":", ref,
newleft);
return new BinaryExpression("|R", newdomain, newpred);
}
}
}
else if (operator.equals("->select"))
{
if (left instanceof BinaryExpression)
{ BinaryExpression lbe = (BinaryExpression) left;
if (lbe.operator.equals("->select"))
{ System.out.println(">> Inefficient nested select: " + this);
Expression predicate1 = lbe.getRight();
Expression combinedPred =
new BinaryExpression("&", predicate1, right);
BinaryExpression res = (BinaryExpression) clone();
res.left = lbe.getLeft();
res.operator = "->select";
res.right = combinedPred;
System.out.println(">> Replacing with: " + res);
return res;
}
else if (lbe.operator.equals("->reject"))
{ System.out.println(">> Inefficient nested select: " + this);
Expression predicate1 = lbe.getRight();
Expression pred2 =
Expression.negate(predicate1);
Expression combinedPred =
new BinaryExpression("&", pred2, right);
BinaryExpression res = (BinaryExpression) clone();
res.left = lbe.getLeft();
res.operator = "->select";
res.right = combinedPred;
System.out.println(">> Replacing with: " + res);
return res;
}
}
}
else if (operator.equals("->reject"))
{
if (left instanceof BinaryExpression)
{ BinaryExpression lbe = (BinaryExpression) left;
if (lbe.operator.equals("->select"))
{ System.out.println(">> Inefficient nested select: " + this);
Expression predicate1 = lbe.getRight();
Expression combinedPred =
new BinaryExpression("&",
Expression.negate(predicate1), right);
BinaryExpression res = (BinaryExpression) clone();
res.left = lbe.getLeft();
res.operator = "->select";
res.right = combinedPred;
System.out.println(">> Replacing with: " + res);
return res;
}
else if (lbe.operator.equals("->reject"))
{ System.out.println(">> Inefficient nested select: " + this);
Expression predicate1 = lbe.getRight();
Expression pred2 =
Expression.negate(predicate1);
pred2.setBrackets(true);
right.setBrackets(true);
Expression combinedPred =
new BinaryExpression("or", pred2, right);
BinaryExpression res = (BinaryExpression) clone();
res.left = lbe.getLeft();
res.operator = "->reject";
res.right = combinedPred;
System.out.println(">> Replacing with: " + res);
return res;
}
}
}

BinaryExpression res = (BinaryExpression) clone();
res.left = lexpr;
res.right = rexpr;
return res;
}


public Map energyUse(Map res, Vector rUses, Vector aUses)
{ // Double iterations ->select(...)->select(...)
// are amber flags.
Expand Down
15 changes: 15 additions & 0 deletions ConditionalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ public Statement generateDesign(java.util.Map env, boolean local)
return new ConditionalStatement(test, ifstat, elsestat);
}

public Expression simplifyOCL()
{ Expression ifstat = ifExp.simplifyOCL();
Expression elsestat = elseExp.simplifyOCL();

Expression testexpr = test.simplifyOCL();

if ((testexpr + "").equals("true"))
{ return ifstat; }

if ((testexpr + "").equals("false"))
{ return elsestat; }

return new ConditionalExpression(testexpr, ifstat, elsestat);
}

public Map energyUse(Map res, Vector rUses, Vector oUses)
{ test.energyUse(res,rUses,oUses);
ifExp.energyUse(res,rUses,oUses);
Expand Down
23 changes: 23 additions & 0 deletions Constraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,29 @@ else if (op.equals("&") && beante.left instanceof BinaryExpression)
public boolean isBehavioural()
{ return behavioural; }

public boolean isDeltaConstraint(String nme)
{ // nme /= nme@pre or nme@pre /= nme
Expression ante = antecedent();

// System.out.println("Antecedent: " + ante);

if (ante instanceof BinaryExpression)
{ BinaryExpression bexpr = (BinaryExpression) ante;
if ("/=".equals(bexpr.getOperator()))
{ if (nme.equals(bexpr.getLeft() + "") &&
(nme + "@pre").equals(bexpr.getRight() + ""))
{ return true; }
if (nme.equals(bexpr.getRight() + "") &&
(nme + "@pre").equals(bexpr.getLeft() + ""))
{ return true; }
return false;
}
return false;
}
return false;
}


public String cg(CGSpec cgs)
{ Expression ante = antecedent();
Expression succ = succedent();
Expand Down
9 changes: 9 additions & 0 deletions Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5540,6 +5540,15 @@ public int sizeof(Vector types, Vector entities)
return sze;
}

public void simplifyOCL()
{ int n = operations.size();

for (int i = 0; i < n; i++)
{ BehaviouralFeature op = (BehaviouralFeature) operations.get(i);
op.simplifyOCL();
}
}

public Map energyAnalysis()
{ Map res = new Map();
res.set("red", 0);
Expand Down
3 changes: 3 additions & 0 deletions Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -2740,6 +2740,9 @@ public boolean confluenceCheck(Vector iterated, Vector created)

abstract public Expression simplify();

public Expression simplifyOCL()
{ return simplify(); }

protected static Expression pruneDuplicates(Vector cnames,
Expression e1,
Expression e2)
Expand Down
Loading

0 comments on commit f4c58e1

Please sign in to comment.