use of org.osate.aadl2.DataType in project VERDICT by ge-high-assurance.
the class Agree2Vdm method getVdmExpressionFromAgreeExpression.
// method to translate expression in Agree to expression in vdm
private Expression getVdmExpressionFromAgreeExpression(Expr agreeExpr, HashSet<String> dataTypeDecl, HashSet<String> nodeDecl, Model model) {
Expression vdmExpr = new Expression();
if (agreeExpr instanceof IfThenElseExpr) {
IfThenElseExpr ifexpr = (IfThenElseExpr) agreeExpr;
// for vdm model
IfThenElse ifThenElseVal = new IfThenElse();
Expression condExpr = getVdmExpressionFromAgreeExpression(ifexpr.getA(), dataTypeDecl, nodeDecl, model);
ifThenElseVal.setCondition(condExpr);
Expression thenBranchExpr = getVdmExpressionFromAgreeExpression(ifexpr.getB(), dataTypeDecl, nodeDecl, model);
ifThenElseVal.setThenBranch(thenBranchExpr);
Expression elseBranchExpr = getVdmExpressionFromAgreeExpression(ifexpr.getC(), dataTypeDecl, nodeDecl, model);
ifThenElseVal.setElseBranch(elseBranchExpr);
vdmExpr.setConditionalExpression(ifThenElseVal);
} else if (agreeExpr instanceof CallExpr) {
CallExpr callExpr = (CallExpr) agreeExpr;
DoubleDotRef ddref = (DoubleDotRef) callExpr.getRef();
if (ddref.getElm() instanceof NodeDef) {
NodeDef nodeDef = (NodeDef) ddref.getElm();
addNodeDefToTypeDeclarations(nodeDef, dataTypeDecl, nodeDecl, model);
// create node call in vdm model
NodeCall vdmNodeCall = new NodeCall();
// setting node name
vdmNodeCall.setNodeId(nodeDef.getName());
EList<Expr> callExprArgs = callExpr.getArgs();
// below are the parameters passed to the function call
for (Expr callExprArg : callExprArgs) {
Expression argExpr = getVdmExpressionFromAgreeExpression(callExprArg, dataTypeDecl, nodeDecl, model);
// setting node arguments
vdmNodeCall.getArgument().add(argExpr);
}
vdmExpr.setCall(vdmNodeCall);
} else {
System.out.println("Unmapped Typed");
}
} else if (agreeExpr instanceof NamedElmExpr) {
NamedElmExpr nmExpr = (NamedElmExpr) agreeExpr;
vdmExpr.setIdentifier(nmExpr.getElm().getName());
// define corresponding types in the VDM if not already defined
if (nmExpr.getElm() instanceof Arg) {
Arg nmElmArg = (Arg) nmExpr.getElm();
// define corresponding type in the VDM if not already defined
Type argType = nmElmArg.getType();
defineDataTypeDataImplementationTypeInVDM(argType, dataTypeDecl, model);
} else if (nmExpr.getElm() instanceof Port) {
Port nmElmPort = (Port) nmExpr.getElm();
// define corresponding type in the VDM if not already defined
if (nmElmPort instanceof DataPortImpl) {
DataPort nmElmDataPort = (DataPort) nmElmPort;
DataSubcomponentType dSubCompType = nmElmDataPort.getDataFeatureClassifier();
defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
} else if (nmElmPort instanceof EventDataPortImpl) {
EventDataPort nmElmDataPort = (EventDataPort) nmElmPort;
DataSubcomponentType dSubCompType = nmElmDataPort.getDataFeatureClassifier();
defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
} else {
if (!(nmElmPort instanceof EventPort)) {
System.out.println("Unresolved Port Type");
}
}
} else if (nmExpr.getElm() instanceof ConstStatement) {
ConstStatement nmElmConstStatement = (ConstStatement) nmExpr.getElm();
String nmElmConstStatementName = nmElmConstStatement.getName();
// add const declaration to VDM if not already defined
if (!dataTypeDecl.contains(nmElmConstStatementName)) {
dataTypeDecl.add(nmElmConstStatementName);
ConstantDeclaration vdmConstDeclaration = new ConstantDeclaration();
vdmConstDeclaration.setName(nmElmConstStatementName);
vdmConstDeclaration.setDefinition(getVdmExpressionFromAgreeExpression(nmElmConstStatement.getExpr(), dataTypeDecl, nodeDecl, model));
vdmConstDeclaration.setDataType(getVdmTypeFromAgreeType(nmElmConstStatement.getType(), dataTypeDecl, model));
LustreProgram lustreProgram = model.getDataflowCode();
lustreProgram.getConstantDeclaration().add(vdmConstDeclaration);
model.setDataflowCode(lustreProgram);
}
} else {
System.out.println("Unresolved/unmapped NamedExprElm: " + nmExpr.getElm().getName());
}
} else if (agreeExpr instanceof SelectionExpr) {
// selection expression corresponds to record projection in VDM
RecordProjection vdmRecordProj = new RecordProjection();
SelectionExpr selExpr = (SelectionExpr) agreeExpr;
if (selExpr.getField() == null) {
System.out.println("Null Selection Expr field: " + selExpr.getField());
} else {
NamedElement field = selExpr.getField();
// set record-projection's reference
if (selExpr.getTarget() != null) {
// target can be NamedElmExpr or a SelectionExpr
vdmRecordProj.setRecordReference(getVdmExpressionFromAgreeExpression(selExpr.getTarget(), dataTypeDecl, nodeDecl, model));
}
// set record-projection's field id
vdmRecordProj.setFieldId(field.getName());
// also set the name of the field's type as the record-projection's record-type
if (field instanceof DataPortImpl) {
DataPort dport = (DataPort) field;
DataSubcomponentType dSubCompType = dport.getDataFeatureClassifier();
defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
vdmRecordProj.setRecordType(dSubCompType.getName());
} else if (field instanceof DataSubcomponentImpl) {
DataSubcomponent dSubComp = (DataSubcomponent) field;
DataSubcomponentType dSubCompType = dSubComp.getDataSubcomponentType();
defineDataTypeDataImplementationTypeInVDM(dSubCompType, dataTypeDecl, model);
vdmRecordProj.setRecordType(dSubCompType.getName());
} else if (field instanceof ArgImpl) {
Arg arg = (Arg) field;
Type argType = arg.getType();
defineDataTypeDataImplementationTypeInVDM(argType, dataTypeDecl, model);
if (argType instanceof PrimType) {
vdmRecordProj.setRecordType(getDataTypeName(argType));
} else {
System.out.println("Unresolved Arg Type so not setting record-type in record-projection.");
}
} else {
System.out.println("Unresolved type of field.");
}
}
vdmExpr.setRecordProjection(vdmRecordProj);
} else if (agreeExpr instanceof BinaryExpr) {
BinaryExpr binExpr = (BinaryExpr) agreeExpr;
// for vdm
BinaryOperation binoper = new BinaryOperation();
// set left operand
Expression leftOperand = getVdmExpressionFromAgreeExpression(binExpr.getLeft(), dataTypeDecl, nodeDecl, model);
binoper.setLhsOperand(leftOperand);
// set right operand
Expression rightOperand = getVdmExpressionFromAgreeExpression(binExpr.getRight(), dataTypeDecl, nodeDecl, model);
binoper.setRhsOperand(rightOperand);
// set appropriate operator
String operator = binExpr.getOp();
if (operator.equalsIgnoreCase("->")) {
vdmExpr.setArrow(binoper);
} else if (operator.equalsIgnoreCase("=>")) {
vdmExpr.setImplies(binoper);
} else if (operator.equalsIgnoreCase("and")) {
vdmExpr.setAnd(binoper);
} else if (operator.equalsIgnoreCase("or")) {
vdmExpr.setOr(binoper);
} else if (operator.equalsIgnoreCase("=")) {
vdmExpr.setEqual(binoper);
} else if (operator.equalsIgnoreCase(">")) {
vdmExpr.setGreaterThan(binoper);
} else if (operator.equalsIgnoreCase("<")) {
vdmExpr.setLessThan(binoper);
} else if (operator.equalsIgnoreCase(">=")) {
vdmExpr.setGreaterThanOrEqualTo(binoper);
} else if (operator.equalsIgnoreCase("<=")) {
vdmExpr.setLessThanOrEqualTo(binoper);
} else if (operator.equalsIgnoreCase("+")) {
vdmExpr.setPlus(binoper);
} else if (operator.equalsIgnoreCase("-")) {
vdmExpr.setMinus(binoper);
} else if (operator.equalsIgnoreCase("!=") || operator.equalsIgnoreCase("<>")) {
vdmExpr.setNotEqual(binoper);
} else if (operator.equalsIgnoreCase("/")) {
vdmExpr.setDiv(binoper);
} else if (operator.equalsIgnoreCase("*")) {
vdmExpr.setTimes(binoper);
} else {
System.out.println("Unmapped binary operator: " + operator);
}
} else if (agreeExpr instanceof UnaryExpr) {
UnaryExpr unExpr = (UnaryExpr) agreeExpr;
Expression singleOperand = getVdmExpressionFromAgreeExpression(unExpr.getExpr(), dataTypeDecl, nodeDecl, model);
String operator = unExpr.getOp();
if (operator.equalsIgnoreCase("this")) {
vdmExpr.setCurrent(singleOperand);
} else if (operator.equalsIgnoreCase("not")) {
vdmExpr.setNot(singleOperand);
} else if (operator.equalsIgnoreCase("-")) {
vdmExpr.setNegative(singleOperand);
} else {
System.out.println("Unmapped unary operator.");
}
} else if (agreeExpr instanceof BoolLitExpr) {
BoolLitExpr boolExpr = (BoolLitExpr) agreeExpr;
vdmExpr.setBoolLiteral(boolExpr.getVal().getValue());
} else if (agreeExpr instanceof EnumLitExpr) {
EnumLitExpr enumExpr = (EnumLitExpr) agreeExpr;
// check if elm is DataImplementationImpl or DataTypeImpl -- if yes add definition to type declarations if not already present
DoubleDotRef enumType = enumExpr.getEnumType();
if (enumType.getElm() instanceof DataTypeImpl) {
org.osate.aadl2.DataType aadlDType = (org.osate.aadl2.DataType) enumType.getElm();
resolveAADLDataType(aadlDType, dataTypeDecl, model);
} else {
System.out.println("Unexpected Elm type for EnumLitExpr");
}
vdmExpr.setIdentifier(enumExpr.getValue());
} else if (agreeExpr instanceof PreExpr) {
PreExpr preExpr = (PreExpr) agreeExpr;
Expression expr = getVdmExpressionFromAgreeExpression(preExpr.getExpr(), dataTypeDecl, nodeDecl, model);
vdmExpr.setPre(expr);
} else if (agreeExpr instanceof RecordLitExpr) {
RecordLiteral vdmRecordLiteral = new RecordLiteral();
RecordLitExpr recLitExpr = (RecordLitExpr) agreeExpr;
if (recLitExpr.getRecordType() instanceof DoubleDotRef) {
DoubleDotRef recType = (DoubleDotRef) recLitExpr.getRecordType();
if (recType.getElm().getName() != null) {
// check if elm is DataImplementationImpl -- if yes add definition to type declarations if not already present
if (recType.getElm() instanceof DataImplementation) {
org.osate.aadl2.DataImplementation aadlDImpl = (org.osate.aadl2.DataImplementation) recType.getElm();
resolveAADLDataImplementationType(aadlDImpl, dataTypeDecl, model);
} else {
System.out.println("Unexpected Elm type for EnumLitExpr");
}
// set name of the record literal in the vdm model
vdmRecordLiteral.setRecordType(recType.getElm().getName());
// get args and arg-expr and set them as field identifier and value in the vdm model
EList<NamedElement> recLitArgs = recLitExpr.getArgs();
EList<Expr> recLitArgsExpr = recLitExpr.getArgExpr();
// below are the values set to variable
for (int ind = 0; ind < recLitArgs.size(); ind++) {
FieldDefinition fieldDef = new FieldDefinition();
fieldDef.setFieldIdentifier(recLitArgs.get(ind).getName());
fieldDef.setFieldValue(getVdmExpressionFromAgreeExpression(recLitArgsExpr.get(ind), dataTypeDecl, nodeDecl, model));
// set field definitions in the record literal in the vdm model
vdmRecordLiteral.getFieldDefinition().add(fieldDef);
}
vdmExpr.setRecordLiteral(vdmRecordLiteral);
} else {
System.out.println("Unexpected Literal's Record Type that has null named elm.");
}
} else {
System.out.println("Unresolved or unmapped record literal expression.");
}
} else if (agreeExpr instanceof IntLitExpr) {
IntLitExpr intLitExpr = (IntLitExpr) agreeExpr;
BigInteger bigInt = new BigInteger(intLitExpr.getVal());
vdmExpr.setIntLiteral(bigInt);
} else if (agreeExpr instanceof RealLitExpr) {
RealLitExpr realLitExpr = (RealLitExpr) agreeExpr;
BigDecimal bigDecimal = new BigDecimal(realLitExpr.getVal());
vdmExpr.setRealLiteral(bigDecimal);
} else if (agreeExpr instanceof EventExpr) {
EventExpr eventExpr = (EventExpr) agreeExpr;
if (eventExpr.getPort() != null) {
vdmExpr.setEvent(getVdmExpressionFromAgreeExpression(eventExpr.getPort(), dataTypeDecl, nodeDecl, model));
} else {
System.out.println("EventExpr does not have port infornation.");
}
} else if (agreeExpr instanceof RealCast) {
RealCast realCastExpr = (RealCast) agreeExpr;
vdmExpr.setToReal(getVdmExpressionFromAgreeExpression(realCastExpr.getExpr(), dataTypeDecl, nodeDecl, model));
} else {
System.out.println("Unresolved/umapped agree expr" + agreeExpr.toString());
}
return vdmExpr;
}
use of org.osate.aadl2.DataType in project AGREE by loonwerks.
the class AgreeValidator method checkArg.
@Check(CheckType.FAST)
public void checkArg(Arg arg) {
Type type = arg.getType();
if (type instanceof PrimType) {
PrimType primType = (PrimType) type;
String strType = primType.getName();
String rangeLow = primType.getRangeLow();
String rangeHigh = primType.getRangeHigh();
if (rangeLow != null && rangeHigh != null) {
// this is a ranged argument. It can show up only in an equation statement
EObject container = arg.eContainer();
if (!(container instanceof EqStatement || container instanceof InputStatement)) {
error(arg, "Ranged arguments can appear only in equation statements or agree_input statements");
}
boolean rangeLowDot = rangeLow.contains(".");
boolean rangeHighDot = rangeHigh.contains(".");
if (rangeLowDot != rangeHighDot) {
error(arg, "The range intervals are of differing types");
}
if (strType.equals("int") && (rangeLowDot || rangeHighDot)) {
error(arg, "Ranged variable of type 'int' contains a 'real' value in its interval");
}
if (strType.equals("real") && (!rangeLowDot || !rangeHighDot)) {
error(arg, "Ranged variable of type 'real' contains an 'int' value in its interval");
}
float low = Float.valueOf(rangeLow);
float high = Float.valueOf(rangeHigh);
low *= primType.getLowNeg() == null ? 1.0 : -1.0;
high *= primType.getHighNeg() == null ? 1.0 : -1.0;
if (low >= high) {
error(arg, "The low value of the interval is greater than or equal to the high end");
}
}
} else if (type instanceof DoubleDotRef) {
DoubleDotRef recType = (DoubleDotRef) type;
NamedElement finalId = recType.getElm();
if (!(finalId instanceof DataImplementation) && !(finalId instanceof RecordDef) && !(finalId instanceof DataType) && !(finalId instanceof EnumStatement)) {
error(recType, "types must be record definition, array definition, data implementation, enumeration, or datatype");
}
if (finalId instanceof DataImplementation) {
if (AgreeTypeSystem.typesEqual(AgreeTypeSystem.typeDefFromType(recType), AgreeTypeSystem.Prim.ErrorTypeDef)) {
error(recType, "Data Implementations with no subcomponents must extend" + " a Base_Type that AGREE can reason about.");
return;
}
if (((DataImplementation) finalId).getAllSubcomponents().size() != 0) {
if (AgreeTypeSystem.typesEqual(AgreeTypeSystem.typeDefFromType(recType), AgreeTypeSystem.Prim.BoolTypeDef) || AgreeTypeSystem.typesEqual(AgreeTypeSystem.typeDefFromType(recType), AgreeTypeSystem.Prim.IntTypeDef) || AgreeTypeSystem.typesEqual(AgreeTypeSystem.typeDefFromType(recType), AgreeTypeSystem.Prim.RealTypeDef)) {
error(finalId, "Data implementations with subcomponents cannot be" + " interpreted by AGREE if they extend Base_Types");
}
}
// dataImplCycleCheck(recId);
return;
}
if (finalId instanceof DataType) {
if (AgreeTypeSystem.typesEqual(AgreeTypeSystem.typeDefFromType(recType), AgreeTypeSystem.Prim.ErrorTypeDef)) {
error(recType, "AADL Datatypes must extend" + " a Base_Type that AGREE can reason about.");
return;
}
}
}
}
use of org.osate.aadl2.DataType in project AGREE by loonwerks.
the class AgreeScopeProvider method getNamedElements.
private Map<String, NamedElement> getNamedElements(EObject ctx) {
Map<String, NamedElement> components = new HashMap<>();
if (ctx instanceof AadlPackage) {
components.put(((AadlPackage) ctx).getName(), (AadlPackage) ctx);
PublicPackageSection pubSec = ((AadlPackage) ctx).getPublicSection();
for (Element el : pubSec.getOwnedElements()) {
if (el instanceof DataImplementation || el instanceof DataType) {
components.put(((NamedElement) el).getName(), (NamedElement) el);
}
}
for (AnnexLibrary annex : AnnexUtil.getAllActualAnnexLibraries(((AadlPackage) ctx), AgreePackage.eINSTANCE.getAgreeContractLibrary())) {
AgreeContract contract = (AgreeContract) ((AgreeContractLibrary) annex).getContract();
components.putAll(getNamedElementsFromSpecs(contract.getSpecs()));
}
} else {
components.putAll(getNamedElementsFromClassifier((Classifier) ctx, false));
}
return components;
}
use of org.osate.aadl2.DataType in project ma-core-public by infiniteautomation.
the class SetPointEventHandlerDefinition method commonValidation.
private void commonValidation(ProcessResult response, SetPointEventHandlerVO vo) {
DataPointVO dp = DataPointDao.getInstance().get(vo.getTargetPointId());
DataType dataType = null;
if (dp == null)
response.addContextualMessage("targetPointId", "eventHandlers.noTargetPoint");
else {
dataType = dp.getPointLocator().getDataType();
if (!dp.getPointLocator().isSettable())
response.addContextualMessage("targetPointId", "event.setPoint.targetNotSettable");
}
if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE && vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE) {
response.addContextualMessage("activeAction", "eventHandlers.noSetPointAction");
response.addContextualMessage("inactiveAction", "eventHandlers.noSetPointAction");
}
MangoJavaScriptService javaScriptService = Common.getBean(MangoJavaScriptService.class);
// Active
if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.MULTISTATE) {
try {
Integer.parseInt(vo.getActiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("activeValueToSet", "eventHandlers.invalidActiveValue");
}
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.NUMERIC) {
try {
Double.parseDouble(vo.getActiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("activeValueToSet", "eventHandlers.invalidActiveValue");
}
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
DataPointVO dpActive = DataPointDao.getInstance().get(vo.getActivePointId());
if (dpActive == null)
response.addContextualMessage("activePointId", "eventHandlers.invalidActiveSource");
else if (dataType != dpActive.getPointLocator().getDataType())
response.addContextualMessage("activeDataPointId", "eventHandlers.invalidActiveSourceType");
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
if (StringUtils.isEmpty(vo.getActiveScript())) {
response.addContextualMessage("activeScript", "eventHandlers.invalidActiveScript");
} else {
try {
javaScriptService.compile(vo.getActiveScript(), true);
} catch (ScriptError e) {
response.addContextualMessage("activeScript", "eventHandlers.invalidActiveScriptError", e.getTranslatableMessage());
}
}
}
// Inactive
if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.MULTISTATE) {
try {
Integer.parseInt(vo.getInactiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("inactiveValueToSet", "eventHandlers.invalidInactiveValue");
}
} else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.NUMERIC) {
try {
Double.parseDouble(vo.getInactiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("inactiveValueToSet", "eventHandlers.invalidInactiveValue");
}
} else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
DataPointVO dpInactive = DataPointDao.getInstance().get(vo.getInactivePointId());
if (dpInactive == null)
response.addContextualMessage("inactivePointId", "eventHandlers.invalidInactiveSource");
else if (dataType != dpInactive.getPointLocator().getDataType())
response.addContextualMessage("inactivePointId", "eventHandlers.invalidInactiveSourceType");
} else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
if (StringUtils.isEmpty(vo.getInactiveScript())) {
response.addContextualMessage("inactiveScript", "eventHandlers.invalidInactiveScript");
} else {
try {
javaScriptService.compile(vo.getInactiveScript(), true);
} catch (ScriptError e) {
response.addContextualMessage("inactiveScript", "eventHandlers.invalidActiveScriptError", e.getTranslatableMessage());
}
}
}
if (vo.getAdditionalContext() != null)
validateScriptContext(vo.getAdditionalContext(), response);
else
vo.setAdditionalContext(new ArrayList<>());
}
use of org.osate.aadl2.DataType in project ma-core-public by infiniteautomation.
the class AbstractPointLocatorVO method readDataType.
protected DataType readDataType(JsonObject json) throws JsonException {
String text = json.getString("dataType");
if (text == null) {
throw new TranslatableJsonException("emport.error.missing", "dataType", DataType.formatNames());
}
DataType dataType;
try {
dataType = DataType.valueOf(text);
} catch (IllegalArgumentException e) {
throw new TranslatableJsonException("emport.error.invalid", "dataType", text, DataType.formatNames());
}
return dataType;
}
Aggregations