use of org.osate.aadl2.ComponentType in project AGREE by loonwerks.
the class MATLABFunctionHandler method runJob.
@Override
protected IStatus runJob(Element root, IProgressMonitor monitor) {
Classifier classifier = getOutermostClassifier(root);
if (!(classifier instanceof ComponentType)) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Must select an AADL Component Type");
}
ComponentType ct = (ComponentType) classifier;
ComponentImplementation ci = null;
EphemeralImplementationUtil implUtil = new EphemeralImplementationUtil(monitor);
try {
SystemInstance si = implUtil.generateEphemeralCompInstanceFromType(ct);
ComponentType sysType = AgreeUtils.getInstanceType(si);
EList<AnnexSubclause> annexSubClauses = AnnexUtil.getAllAnnexSubclauses(sysType, AgreePackage.eINSTANCE.getAgreeContractSubclause());
if (annexSubClauses.size() == 0) {
throw new AgreeException("There is not an AGREE annex in the '" + sysType.getName() + "' system type.");
}
// Get Agree program
AgreeProgram agreeProgram = new AgreeASTBuilder().getAgreeProgram(si, false);
if (agreeProgram.containsRealTimePatterns) {
throw new AgreeException("'" + sysType.getName() + "' system type contains AGREE Real Time Patterns." + " Export of AGREE Real Time Patterns NOT Supported - they are considered scheduling properties" + " of components and can be decomposed further.");
}
// Translate Agree Node to Lustre Node with pre-statement flatten, helper nodes inlined,
// and variable declarations sorted so they are declared before use
Node lustreNode = AgreeNodeToLustreContract.translate(agreeProgram.topNode, agreeProgram);
// Translate Lustre Node to MATLAB Function AST
MATLABPrimaryFunction matlabFunction = LustreToMATLABTranslator.translate(lustreNode, agreeProgram);
ModelInfo info = getModelInfo(ct);
if (info == null) {
// return;
return Status.CANCEL_STATUS;
}
String dirStr = info.outputDirPath;
if (dirStr == null || dirStr.isEmpty()) {
// return;
return Status.CANCEL_STATUS;
}
boolean exportContractsPressed = info.exportPressed;
boolean genImplPressed = info.generatePressed;
boolean genVerificationPressed = info.updatePressed;
boolean verifySubsysPressed = info.verifyPressed;
String matlabFuncScriptName = matlabFunction.name + ".m";
if (genImplPressed) {
// Write MATLAB script to generate subsystem in the selected
// output folder
String subsysName = "";
if (info.subsystemName.equals("")) {
subsysName = sysType.getName();
} else {
subsysName = info.subsystemName;
}
MdlScriptCreator implMdlScript = new MdlScriptCreator(dirStr, info.implMdlPath, info.verifyMdlName, subsysName, matlabFunction.ports, matlabFuncScriptName, true, info.verifyPressed);
String implMdlScriptName = "generate_" + subsysName + ".m";
// generate the script to create the impl model file into the path specified for the model
File implMdlFile = new File(info.implMdlPath);
String implMdlDir = implMdlFile.getParent();
if (implMdlDir != null) {
Path implMdlScriptPath = Paths.get(implMdlDir, implMdlScriptName);
writeToFile(implMdlScriptPath, implMdlScript.toString());
}
}
if (exportContractsPressed || genVerificationPressed || verifySubsysPressed) {
Path matlabFuncScriptPath = Paths.get(dirStr, matlabFuncScriptName);
// Write MATLAB function code into the specified file in the
// selected output folder
writeToFile(matlabFuncScriptPath, matlabFunction.toString());
if (genVerificationPressed || verifySubsysPressed) {
// Create Simulink Model Update script into the output
// folder
MdlScriptCreator verifMdlScript = new MdlScriptCreator(dirStr, info.implMdlPath, info.verifyMdlName, info.subsystemName, matlabFunction.ports, matlabFuncScriptName, false, info.verifyPressed);
String verifMdlScriptName = matlabFunction.name + "_Observer.m";
Path verifMdlScriptPath = Paths.get(dirStr, verifMdlScriptName);
writeToFile(verifMdlScriptPath, verifMdlScript.toString());
}
}
// return;
return Status.OK_STATUS;
} catch (Throwable e) {
String messages = getNestedMessages(e);
e.printStackTrace();
Dialog.showError("AGREE Error", e.toString());
// return;
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, messages, e);
} finally {
if (ci != null) {
ci.eResource().getContents().remove(ci);
}
implUtil.cleanup();
}
}
use of org.osate.aadl2.ComponentType in project AGREE by loonwerks.
the class AgreeValidator method checkAssume.
@Check(CheckType.FAST)
public void checkAssume(AssumeStatement assume) {
Classifier comp = assume.getContainingClassifier();
if (!(comp instanceof ComponentType)) {
error(assume, "Assume statements are allowed only in component types");
}
// the expression could be null if a pattern is used
Expr expr = assume.getExpr();
if (expr != null) {
TypeDef exprType = AgreeTypeSystem.infer(expr);
if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, exprType)) {
error(assume, "Expression for assume statement is of type '" + nameOfTypeDef(exprType) + "' but must be of type 'bool'");
}
}
if (assume.getName() == null) {
info(assume, "It is recommended that assume statements be named." + " (Hint: an identifier may be placed between the \"assume\" keyword and the quoted description.)");
}
}
use of org.osate.aadl2.ComponentType in project AGREE by loonwerks.
the class AgreeValidator method checkNamedElement.
@Check(CheckType.FAST)
public void checkNamedElement(NamedElement namedEl) {
// check for namespace collision in component types of component
// implementations
// and for collisions between subcomponent and feature names
EObject container = namedEl.eContainer();
if (container == null) {
return;
}
if (container instanceof RecordDef || container instanceof NodeDef) {
// TODO: perhaps we can ignore all arguments?
return;
}
while (!(container instanceof AadlPackage || container instanceof ComponentImplementation || container instanceof ComponentType)) {
container = container.eContainer();
}
ComponentImplementation compImpl = null;
ComponentType type = null;
if (container instanceof ComponentImplementation) {
compImpl = (ComponentImplementation) container;
type = compImpl.getType();
checkDupNames(namedEl, type, compImpl);
} else if (container instanceof ComponentType) {
type = (ComponentType) container;
}
if (type != null && (namedEl.getName() != null)) {
for (Feature feat : type.getAllFeatures()) {
if (namedEl.getName().equals(feat.getName())) {
error(feat, "Element of the same name ('" + namedEl.getName() + "') in AGREE Annex in '" + (compImpl == null ? type.getName() : compImpl.getName()) + "'");
error(namedEl, "Feature of the same name ('" + namedEl.getName() + "') in component type");
}
}
}
// check name space collision with enumerated types
}
use of org.osate.aadl2.ComponentType in project AGREE by loonwerks.
the class AgreeValidator method checkLiftContract.
@Check(CheckType.FAST)
public void checkLiftContract(LiftContractStatement lcst) {
Classifier comp = lcst.getContainingClassifier();
if (comp instanceof ComponentImplementation) {
ComponentType ct = ((ComponentImplementation) comp).getType();
List<AnnexSubclause> agreeAnnexes = AnnexUtil.getAllAnnexSubclauses(ct, AgreePackage.eINSTANCE.getAgreeContractSubclause());
if (agreeAnnexes.size() > 0) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whose type has an AGREE annex.");
}
List<Subcomponent> subcomps = ((ComponentImplementation) comp).getAllSubcomponents();
if (subcomps.size() == 1) {
Subcomponent subcomp = subcomps.get(0);
Classifier subCls = subcomp.getClassifier();
ComponentType subCt = null;
if (subCls instanceof ComponentImplementation) {
subCt = ((ComponentImplementation) subCls).getType();
} else if (subCls instanceof ComponentType) {
subCt = (ComponentType) subCls;
} else {
throw new RuntimeException();
}
{
Set<String> usedParentInPorts = new HashSet<>();
Set<String> usedParentOutPorts = new HashSet<>();
Set<String> usedChildInPorts = new HashSet<>();
Set<String> usedChildOutPorts = new HashSet<>();
EList<Classifier> ctPlusAllExtended = ct.getSelfPlusAllExtended();
EList<Classifier> subCtPlusAllExtended = subCt.getSelfPlusAllExtended();
for (Connection conn : ((ComponentImplementation) comp).getAllConnections()) {
{
NamedElement sourceNe = conn.getSource().getConnectionEnd();
if (subCtPlusAllExtended.contains(sourceNe.getContainingClassifier())) {
if (usedChildOutPorts.contains(sourceNe.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection out of same output " + sourceNe.getQualifiedName() + ".");
}
usedChildOutPorts.add(sourceNe.getName());
}
if (ctPlusAllExtended.contains(sourceNe.getContainingClassifier())) {
if (usedParentInPorts.contains(sourceNe.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection out of same input " + sourceNe.getQualifiedName() + ".");
}
usedParentInPorts.add(sourceNe.getName());
}
}
{
NamedElement destNe = conn.getDestination().getConnectionEnd();
if (subCtPlusAllExtended.contains(destNe.getContainingClassifier())) {
if (usedChildInPorts.contains(destNe.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection into same input " + destNe.getQualifiedName() + ".");
}
usedChildInPorts.add(destNe.getName());
}
if (ctPlusAllExtended.contains(destNe.getContainingClassifier())) {
if (usedParentOutPorts.contains(destNe.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whith more than one connection into same output " + destNe.getQualifiedName() + ".");
}
usedParentOutPorts.add(destNe.getName());
}
}
}
for (Feature feat : comp.getAllFeatures()) {
boolean isIn = false;
if (feat instanceof DataPort) {
isIn = ((DataPort) feat).isIn();
} else if (feat instanceof EventDataPort) {
isIn = ((EventDataPort) feat).isIn();
} else if (feat instanceof EventPort) {
isIn = ((EventPort) feat).isIn();
}
if (isIn) {
if (!usedParentInPorts.contains(feat.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection from input " + feat.getQualifiedName() + ".");
}
} else {
if (!usedParentOutPorts.contains(feat.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection to output " + feat.getQualifiedName() + ".");
}
}
}
for (Feature feat : subCt.getAllFeatures()) {
boolean isIn = false;
if (feat instanceof DataPort) {
isIn = ((DataPort) feat).isIn();
} else if (feat instanceof EventDataPort) {
isIn = ((EventDataPort) feat).isIn();
} else if (feat instanceof EventPort) {
isIn = ((EventPort) feat).isIn();
}
if (isIn) {
if (!usedChildInPorts.contains(feat.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection into " + feat.getQualifiedName() + ".");
}
} else {
if (!usedChildOutPorts.contains(feat.getName())) {
error(lcst, "'lift contract;' statement is not allowed in component implementation whithout connection out of " + feat.getQualifiedName() + ".");
}
}
}
}
} else {
error(lcst, "'lift contract;' statement is not allowed in component implementation whithout exactly one subcomponent.");
}
} else {
error(lcst, "'lift contract;' statement is not allowed in component interface.");
}
}
use of org.osate.aadl2.ComponentType in project AGREE by loonwerks.
the class AgreeValidator method checkGuarantee.
@Check(CheckType.FAST)
public void checkGuarantee(GuaranteeStatement guar) {
Classifier comp = guar.getContainingClassifier();
if (!(comp instanceof ComponentType)) {
error(guar, "Guarantee statements are allowed only in component types");
}
// the expression could be null if a pattern is used
Expr expr = guar.getExpr();
if (expr != null) {
TypeDef exprType = AgreeTypeSystem.infer(expr);
if (!AgreeTypeSystem.typesEqual(AgreeTypeSystem.Prim.BoolTypeDef, exprType)) {
error(guar, "Expression for guarantee statement is of type '" + nameOfTypeDef(exprType) + "' but must be of type 'bool'");
}
}
if (guar.getName() == null) {
info(guar, "It is recommended that guarantee statements be named." + " (Hint: an identifier may be placed between the \"guarantee\" keyword and the quoted description.)");
}
}
Aggregations