You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

87 lines
2.6 KiB

package parser.visitor;
import java.util.ArrayList;
import parser.ast.AccumulationSymbol;
import parser.ast.Expression;
import parser.ast.ExpressionAccumulation;
import parser.ast.ExpressionBinaryOp;
import parser.ast.ExpressionLabel;
import parser.ast.ExpressionTemporal;
import parser.ast.ExpressionUnaryOp;
import prism.PrismLangException;
public class ReplaceAccumulationExpression extends ASTTraverseModify {
private ExpressionAccumulation accexp;
private ArrayList<String> initLabels;
private ArrayList<String> runLabels;
private ArrayList<String> goalLabels;
private int accLength;
public ReplaceAccumulationExpression(ExpressionAccumulation accexp, ArrayList<String> initLabels, ArrayList<String> runLabels, ArrayList<String> goalLabels, int accLength) {
super();
this.accexp = accexp;
this.initLabels = initLabels;
this.runLabels = runLabels;
this.goalLabels = goalLabels;
this.accLength = accLength;
}
public Object visit(ExpressionAccumulation expr) throws PrismLangException
{
if (expr == accexp) {
AccumulationSymbol sym = expr.getSymbol();
if (sym.isMinus()) {
throw(new PrismLangException("Accumulation with past is not supported."));
}
// This expression is of the form OR{0..(l-1)}(init_i AND (run_i UNTIL goal_i))
// Build all the subexpressions...
ArrayList<ExpressionBinaryOp> clauses = new ArrayList<>();
Expression result = null;
for(int i=0; i<accLength; i++) {
Expression init = new ExpressionLabel(initLabels.get(i));
Expression run = new ExpressionLabel(runLabels.get(i));
Expression goal = new ExpressionLabel(goalLabels.get(i));
// if this is until:
// append first operand to run
// append second operand to goal
if(accexp.getOperand1() != null) {
run = new ExpressionBinaryOp(ExpressionBinaryOp.AND, run, accexp.getOperand1());
}
if(accexp.getOperand2() != null) {
goal = new ExpressionBinaryOp(ExpressionBinaryOp.AND, goal, accexp.getOperand2());
}
// until: (run_i UNTIL goal_i)
ExpressionTemporal until = new ExpressionTemporal(ExpressionTemporal.P_U, run, goal);
// and: (init_i AND until)
ExpressionBinaryOp and = new ExpressionBinaryOp(ExpressionBinaryOp.AND, init, until);
clauses.add(and);
if(i==0) {
result = and;
} else {
result = new ExpressionBinaryOp(ExpressionBinaryOp.OR, result, and);
}
}
// If its a BOX, negate it
if (sym.isBox()) {
return new ExpressionUnaryOp(ExpressionUnaryOp.NOT, result);
} else {
return result;
}
} else {
return expr;
}
}
}