use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.
the class LinearizationRewriter method generateAgreeConstraintForm.
private static NodeDef generateAgreeConstraintForm(LinearizationDef linDef, BoundingSegments segs) {
NodeDef result = af.createNodeDef();
result.setName(getConstraintFormName(AgreeUtils.getNodeName(linDef)));
Arg inputArg = af.createArg();
PrimType inputArgType = af.createPrimType();
inputArgType.setName("real");
inputArg.setType(inputArgType);
inputArg.setName("inp");
result.getArgs().add(inputArg);
Arg outputArg = af.createArg();
PrimType outputArgType = af.createPrimType();
outputArgType.setName("real");
outputArg.setType(outputArgType);
outputArg.setName("result");
result.getArgs().add(outputArg);
Arg constraintArg = af.createArg();
PrimType constraintArgType = af.createPrimType();
constraintArgType.setName("bool");
constraintArg.setType(constraintArgType);
constraintArg.setName("constraint");
result.getRets().add(constraintArg);
NamedElmExpr inputId = af.createNamedElmExpr();
inputId.setElm(EcoreUtil.copy(inputArg));
RealLitExpr domainCheckLowerLit = af.createRealLitExpr();
domainCheckLowerLit.setVal(Double.toString(segs.lower.getFirst().startX));
BinaryExpr domainCheckLowerExpr = af.createBinaryExpr();
domainCheckLowerExpr.setOp("<=");
domainCheckLowerExpr.setLeft(domainCheckLowerLit);
domainCheckLowerExpr.setRight(EcoreUtil.copy(inputId));
RealLitExpr domainCheckUpperLit = af.createRealLitExpr();
domainCheckUpperLit.setVal(Double.toString(segs.lower.getLast().stopX));
BinaryExpr domainCheckUpperExpr = af.createBinaryExpr();
domainCheckUpperExpr.setOp("<=");
domainCheckUpperExpr.setLeft(EcoreUtil.copy(inputId));
domainCheckUpperExpr.setRight(domainCheckUpperLit);
BinaryExpr domainCheckExpr = af.createBinaryExpr();
domainCheckExpr.setOp("and");
domainCheckExpr.setLeft(domainCheckLowerExpr);
domainCheckExpr.setRight(domainCheckUpperExpr);
BoolLitExpr trueLitExpr = af.createBoolLitExpr();
BooleanLiteral trueLitValue = aadlF.createBooleanLiteral();
trueLitValue.setValue(true);
trueLitExpr.setVal(trueLitValue);
Expr upperBoundExpr = EcoreUtil.copy(trueLitExpr);
for (Segment seg : segs.upper) {
BinaryExpr andExpr = af.createBinaryExpr();
andExpr.setOp("and");
andExpr.setLeft(upperBoundExpr);
andExpr.setRight(generateAgreeLinearBoundImplicationExpr(inputArg, outputArg, "<=", seg));
upperBoundExpr = andExpr;
}
Expr lowerBoundExpr = EcoreUtil.copy(trueLitExpr);
for (Segment seg : segs.lower) {
BinaryExpr andExpr = af.createBinaryExpr();
andExpr.setOp("and");
andExpr.setLeft(lowerBoundExpr);
andExpr.setRight(generateAgreeLinearBoundImplicationExpr(inputArg, outputArg, ">=", seg));
lowerBoundExpr = andExpr;
}
BinaryExpr boundsCheckExpr = af.createBinaryExpr();
boundsCheckExpr.setOp("and");
boundsCheckExpr.setLeft(upperBoundExpr);
boundsCheckExpr.setRight(lowerBoundExpr);
BinaryExpr constraintExpr = af.createBinaryExpr();
constraintExpr.setOp("and");
constraintExpr.setLeft(domainCheckExpr);
constraintExpr.setRight(boundsCheckExpr);
NodeEq constraintEq = af.createNodeEq();
constraintEq.getLhs().add(constraintArg);
constraintEq.setExpr(constraintExpr);
NodeBodyExpr nodeBody = af.createNodeBodyExpr();
nodeBody.getStmts().add(constraintEq);
result.setNodeBody(nodeBody);
NodeLemma domainCheckLemma = af.createNodeLemma();
domainCheckLemma.setStr(result.getName() + " domain check");
domainCheckLemma.setExpr(EcoreUtil.copy(domainCheckExpr));
nodeBody.getStmts().add(domainCheckLemma);
return result;
}
use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.
the class LinearizationRewriter method generateAgreeLinearBoundImplicationExpr.
private static Expr generateAgreeLinearBoundImplicationExpr(Arg inputArg, Arg resultArg, String relop, Segment seg) {
RealLitExpr inputMinExpr = af.createRealLitExpr();
inputMinExpr.setVal(Double.toString(seg.startX));
RealLitExpr inputMaxExpr = af.createRealLitExpr();
inputMaxExpr.setVal(Double.toString(seg.stopX));
RealLitExpr resultOriginExpr = af.createRealLitExpr();
resultOriginExpr.setVal(Double.toString(seg.startY));
RealLitExpr resultSlopeExpr = af.createRealLitExpr();
resultSlopeExpr.setVal(Double.toString((seg.stopY - seg.startY) / (seg.stopX - seg.startX)));
NamedElmExpr inputId = af.createNamedElmExpr();
inputId.setElm(EcoreUtil.copy(inputArg));
NamedElmExpr resultId = af.createNamedElmExpr();
resultId.setElm(EcoreUtil.copy(resultArg));
// =======
//
// NestedDotID resultId = af.createNestedDotID();
// resultId.setBase(EcoreUtil.copy(resultArg));
// >>>>>>> origin/develop
BinaryExpr rangeMinExpr = af.createBinaryExpr();
rangeMinExpr.setOp(">=");
rangeMinExpr.setLeft(EcoreUtil.copy(inputId));
rangeMinExpr.setRight(EcoreUtil.copy(inputMinExpr));
BinaryExpr rangeMaxExpr = af.createBinaryExpr();
rangeMaxExpr.setOp("<=");
rangeMaxExpr.setLeft(EcoreUtil.copy(inputId));
rangeMaxExpr.setRight(EcoreUtil.copy(inputMaxExpr));
BinaryExpr rangeExpr = af.createBinaryExpr();
rangeExpr.setOp("and");
rangeExpr.setLeft(EcoreUtil.copy(rangeMinExpr));
rangeExpr.setRight(EcoreUtil.copy(rangeMaxExpr));
BinaryExpr shiftExpr = af.createBinaryExpr();
shiftExpr.setOp("-");
shiftExpr.setLeft(EcoreUtil.copy(inputId));
shiftExpr.setRight(EcoreUtil.copy(inputMinExpr));
BinaryExpr multiplyExpr = af.createBinaryExpr();
multiplyExpr.setOp("*");
multiplyExpr.setLeft(EcoreUtil.copy(resultSlopeExpr));
multiplyExpr.setRight(shiftExpr);
BinaryExpr additionExpr = af.createBinaryExpr();
additionExpr.setOp("+");
additionExpr.setLeft(EcoreUtil.copy(resultOriginExpr));
additionExpr.setRight(multiplyExpr);
BinaryExpr linearBoundExpr = af.createBinaryExpr();
linearBoundExpr.setOp(relop);
linearBoundExpr.setLeft(EcoreUtil.copy(resultId));
linearBoundExpr.setRight(additionExpr);
BinaryExpr result = af.createBinaryExpr();
result.setOp("=>");
result.setLeft(rangeExpr);
result.setRight(linearBoundExpr);
return result;
}
use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.
the class Util method getDoubleValue.
public static Double getDoubleValue(Expr expr) {
Double result = Double.valueOf(0.0);
assert (AgreeJavaValidator.exprIsConst(expr));
if (expr instanceof NamedElement) {
if (expr instanceof ConstStatement) {
result = getDoubleValue(((ConstStatement) expr).getExpr());
}
} else if (expr instanceof SelectionExpr) {
NamedElement finalId = ((SelectionExpr) expr).getField();
if (finalId instanceof ConstStatement) {
result = getDoubleValue(((ConstStatement) finalId).getExpr());
}
} else if (expr instanceof RealLitExpr) {
result = Double.valueOf(((RealLitExpr) expr).getVal());
} else if (expr instanceof IntLitExpr) {
result = Double.valueOf(((IntLitExpr) expr).getVal());
} else if (expr instanceof BinaryExpr) {
BinaryExpr binExpr = (BinaryExpr) expr;
Double left = getDoubleValue(binExpr.getLeft());
Double right = getDoubleValue(binExpr.getRight());
switch(binExpr.getOp()) {
case "+":
result = left + right;
break;
case "-":
result = left - right;
break;
case "*":
result = left * right;
break;
case "/":
result = left / right;
break;
case "^":
result = Math.pow(left, right);
break;
default:
throw new AgreeException("binary expression is not evaluable as integer constant");
}
return result;
} else if (expr instanceof UnaryExpr) {
UnaryExpr unExpr = (UnaryExpr) expr;
Double right = getDoubleValue(unExpr.getExpr());
switch(unExpr.getOp()) {
case "-":
result = -right;
break;
default:
throw new AgreeException("unary expression is not evaluable as integer constant");
}
} else {
throw new AgreeException("expression is not evaluable as integer constant");
}
return result;
}
use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.
the class AgreeValidator method checkWhenHoldsStatement.
@Check(CheckType.FAST)
public void checkWhenHoldsStatement(WhenHoldsStatement when) {
Expr condition = when.getCondition();
Expr event = when.getEvent();
TimeInterval condInterval = when.getConditionInterval();
checkExprIsIdentifier(condition);
checkExprIsIdentifier(event);
if (condInterval != null) {
Expr lowExpr = condInterval.getLow();
if (lowExpr instanceof RealLitExpr) {
RealLitExpr realExpr = (RealLitExpr) lowExpr;
if (!realExpr.getVal().equals("0.0")) {
error(lowExpr, "The lower bound of this interval must be zero");
}
}
} else {
error(when, "Statement most of a cause interval");
}
TypeDef type = AgreeTypeSystem.infer(condition);
if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, type)) {
error(condition, "The condition of a when statement is of type '" + type + "'" + " but must be of type 'bool'");
}
type = AgreeTypeSystem.infer(event);
if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, type)) {
error(event, "The effect of a when statement is of type '" + type + "'" + " but must be of type 'bool'");
}
}
use of com.rockwellcollins.atc.agree.agree.RealLitExpr in project AGREE by loonwerks.
the class AgreeValidator method checkPeriodicStatement.
@Check(CheckType.FAST)
public void checkPeriodicStatement(PeriodicStatement statement) {
Expr event = statement.getEvent();
Expr jitter = statement.getJitter();
Expr period = statement.getPeriod();
checkExprIsIdentifier(event);
TypeDef eventType = AgreeTypeSystem.infer(event);
if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, eventType)) {
error(event, "Expression is of type '" + eventType + "' but must be of type 'bool'");
}
if (jitter != null) {
if (!(jitter instanceof RealLitExpr || isTimingConst(jitter))) {
error(jitter, "The specified jitter must be a real literal");
} else {
Double val = getRealConstVal(jitter);
if (val < 0) {
error(jitter, "The specified jitter must be positive");
}
}
}
if (!(period instanceof RealLitExpr || isTimingConst(period))) {
error(period, "The specified period must be a real literal");
} else {
Double val = getRealConstVal(period);
if (val < 0) {
error(period, "The specified period must be positive");
}
}
}
Aggregations