use of org.osate.aadl2.instance.SystemInstance in project osate2 by osate.
the class InstantiateModel method createSystemOperationModes.
// --------------------------------------------------------------------------------------------
// Methods related to system operation modes
// --------------------------------------------------------------------------------------------
/*
* Create the system operation mode objects for the instance model.
*/
protected void createSystemOperationModes(final SystemInstance root, final int limit) throws InterruptedException {
class SOMBuilder {
class Node {
ComponentInstance ci;
Node parentNode;
State state;
Node(ComponentInstance ci, Node parentNode) {
this.ci = ci;
this.parentNode = parentNode;
}
}
class State {
boolean active;
// mode is ignored if !active
ModeInstance mode;
State(boolean active) {
this.active = active;
}
}
ArrayList<Node> workState = new ArrayList<>();
int modalCount;
int createSoms() throws InterruptedException {
Node rootNode = new Node(null, null);
rootNode.state = new State(true);
initWorkState(root, rootNode);
modalCount = workState.size();
if (modalCount == 0) {
/*
* We have no modal components, but we need to create a special SOM to
* represent our single normal operating state.
*/
final SystemOperationMode som = InstanceFactory.eINSTANCE.createSystemOperationMode();
som.setName(NORMAL_SOM_NAME);
root.getSystemOperationModes().add(som);
return 0;
} else {
return enumerateSoms(0, 0);
}
}
protected int enumerateSoms(int depth, int index) throws InterruptedException {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
Node node = workState.get(depth);
State parentState = node.parentNode.state;
Iterator<ModeInstance> modes = parentState.active ? getActiveModes(node.ci, parentState.mode) : Collections.emptyIterator();
boolean active = parentState.active && modes.hasNext();
State state = new State(active);
node.state = state;
if (depth + 1 == modalCount) {
// here we add one or more SOMs
if (active) {
while (modes.hasNext()) {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
state.mode = modes.next();
root.getSystemOperationModes().add(createSOM(index + 1));
if (index < 0 || ++index >= limit) {
return -1;
}
}
} else {
root.getSystemOperationModes().add(createSOM(index + 1));
if (index < 0 || ++index >= limit) {
return -1;
}
}
} else {
if (active) {
while (modes.hasNext()) {
state.mode = modes.next();
index = enumerateSoms(depth + 1, index);
if (index < 0) {
return -1;
}
}
} else {
index = enumerateSoms(depth + 1, index);
}
}
node.state = null;
return index;
}
protected Iterator<ModeInstance> getActiveModes(ComponentInstance ci, ModeInstance parentMode) {
List<ModeInstance> modes = ci.getModeInstances();
if (parentMode == null) {
// system instance
return modes.iterator();
} else if (!ci.getInModes().isEmpty() && !ci.getInModes().contains(parentMode)) {
// component not active in parent mode
return Collections.emptyIterator();
} else {
// limit derived modes to mapping
return modes.stream().filter(mi -> {
return !mi.isDerived() || mi.getParents().contains(parentMode);
}).iterator();
}
}
protected void initWorkState(ComponentInstance ci, Node parentNode) throws InterruptedException {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
if (!ci.getModeInstances().isEmpty()) {
parentNode = new Node(ci, parentNode);
workState.add(parentNode);
}
for (ComponentInstance sub : ci.getComponentInstances()) {
initWorkState(sub, parentNode);
}
}
protected SystemOperationMode createSOM(int somNo) throws InterruptedException {
final SystemOperationMode som;
som = InstanceFactory.eINSTANCE.createSystemOperationMode();
for (Node node : workState) {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
if (!node.state.active) {
continue;
}
ModeInstance mi = node.state.mode;
List<SystemOperationMode> soms = mode2som.get(mi);
if (soms == null) {
soms = new ArrayList<SystemOperationMode>();
mode2som.put(mi, soms);
}
soms.add(som);
som.getCurrentModes().add(mi);
}
som.setName("som_" + somNo);
return som;
}
}
int index = new SOMBuilder().createSoms();
if (index < 0) {
errManager.warning(root, "List of system operation modes is incomplete (see project property 'Instantiation')");
}
}
use of org.osate.aadl2.instance.SystemInstance in project osate2 by osate.
the class CreateConnectionsSwitch method addConnectionInstance.
protected ConnectionInstance addConnectionInstance(final SystemInstance systemInstance, final ConnectionInfo connInfo, final ConnectionInstanceEnd dstI) {
// with aggregate data ports will be sources/destinations missing
int numConns = connInfo.connections.size();
if (connInfo.sources.size() != numConns || connInfo.destinations.size() != numConns) {
// happens if conn leaves system to aggregate data port
warning(connInfo.container, "Connection from " + connInfo.sources.get(0).getInstanceObjectPath() + " to " + dstI.getInstanceObjectPath() + " could not be instantiated.");
return null;
}
// check for duplicate connection instance
// with arrays we can get duplicates that we don't need
ComponentInstance container = connInfo.container;
List<Connection> conns = connInfo.connections;
if (container == null) {
container = systemInstance;
}
for (ConnectionInstance test : container.getConnectionInstances()) {
// check for duplicates and do not create
if (connInfo.src == test.getSource() && dstI == test.getDestination() && conns.size() == test.getConnectionReferences().size()) {
ListIterator<Connection> i = conns.listIterator();
boolean isDuplicate = true;
for (ConnectionReference ref : test.getConnectionReferences()) {
if (ref.getConnection() != i.next()) {
isDuplicate = false;
break;
}
}
if (!isDuplicate) {
// also test reverse direction
isDuplicate = true;
i = conns.listIterator(conns.size());
for (ConnectionReference ref : test.getConnectionReferences()) {
if (ref.getConnection() != i.previous()) {
isDuplicate = false;
break;
}
}
}
if (isDuplicate) {
return null;
}
}
}
boolean duplicate = false;
// Generate a name for the connection
String containerPath = (connInfo.container != null) ? container.getInstanceObjectPath() : systemInstance.getName();
int len = containerPath.length() + 1;
String srcPath = connInfo.src.getInstanceObjectPath();
StringBuffer sb = new StringBuffer();
String dstPath = "xxx";
int i = (srcPath.startsWith(containerPath)) ? len : 0;
srcPath = srcPath.substring(i);
sb.append(srcPath);
sb.append(" -> ");
if (dstI != null) {
dstPath = dstI.getInstanceObjectPath();
i = (dstPath.startsWith(containerPath)) ? len : 0;
dstPath = dstPath.substring(i);
sb.append(dstPath);
}
ConnectionInstance conni = null;
if (!duplicate) {
conni = connInfo.createConnectionInstance(sb.toString(), dstI);
if (conni == null) {
warning(container, "Connection sequence from " + srcPath + " to " + dstPath + " is only outgoing. No connection instance created.");
return null;
} else {
container.getConnectionInstances().add(conni);
}
fillInModes(conni);
fillInModeTransitions(conni);
}
return conni;
}
use of org.osate.aadl2.instance.SystemInstance in project osate2 by osate.
the class CreateEndToEndFlowsSwitch method testConnection.
/**
* @param conni
* @param etei
* @param result
*/
private boolean testConnection(ConnectionInstance conni, EndToEndFlowInstance etei) {
Iterator<ConnectionReference> refIter = conni.getConnectionReferences().iterator();
boolean match = false;
while (refIter.hasNext()) {
String name1 = refIter.next().getConnection().getName();
String name2 = connections.get(0).getName();
if (name1.equalsIgnoreCase(name2)) {
Iterator<Connection> connIter = connections.iterator();
connIter.next();
match = true;
while (match && refIter.hasNext() && connIter.hasNext()) {
match &= refIter.next().getConnection().getName().equalsIgnoreCase(connIter.next().getName());
}
if (!refIter.hasNext() && connIter.hasNext()) {
match = false;
}
}
}
if (match && connections.size() == 1) {
// make sure connection instance goes in the same direction as the flow
ComponentInstance connci = conni.getSource().getComponentInstance();
FlowElementInstance fei = etei;
while (fei instanceof EndToEndFlowInstance) {
fei = ((EndToEndFlowInstance) fei).getFlowElements().get(((EndToEndFlowInstance) fei).getFlowElements().size() - 1);
}
if (fei instanceof FlowSpecificationInstance) {
fei = fei.getComponentInstance();
}
ComponentInstance flowci = (ComponentInstance) fei;
match = false;
ComponentInstance ci = connci;
while (!(ci instanceof SystemInstance)) {
if (ci == flowci) {
match = true;
break;
}
ci = ci.getContainingComponentInstance();
}
}
if (match) {
// test if the connection instance is connected to the end of the ete instance
// relevant if the flow goes through a port of a feature group and the connection
// instance comes from an expanded fg connection
ConnectionInstanceEnd src = conni.getSource();
if (src instanceof FeatureInstance) {
FeatureInstance firstFeature = (FeatureInstance) src;
FeatureInstance lastFeature = getLastFeature(etei);
if (lastFeature != null) {
match = isSameorContains(lastFeature, firstFeature);
}
}
}
return match;
}
use of org.osate.aadl2.instance.SystemInstance in project osate2 by osate.
the class InstantiateModel method rebuildInstanceModelFile.
/**
* This method will construct an instance model, save it on disk and return
* its root object The method will make sure the declarative models are up
* to date.
*
* @param si system implementation
*
* @return SystemInstance or <code>null</code> if cancelled.
* @since 1.1
*/
public static SystemInstance rebuildInstanceModelFile(final IResource ires, final IProgressMonitor monitor) throws Exception {
ires.deleteMarkers(null, true, IResource.DEPTH_INFINITE);
ResourceSet rset = new ResourceSetImpl();
Resource res = rset.getResource(OsateResourceUtil.toResourceURI(ires), true);
SystemInstance target = (SystemInstance) res.getContents().get(0);
ComponentImplementation ci = target.getComponentImplementation();
URI uri = EcoreUtil.getURI(ci);
res.getContents().clear();
res.save(null);
res.unload();
ci = (ComponentImplementation) rset.getEObject(uri, true);
if (ci == null) {
// The root component instance doesn't exist anymore
throw new RootMissingException();
}
final InstantiateModel instantiateModel = new InstantiateModel(monitor, new AnalysisErrorReporterManager(new MarkerAnalysisErrorReporter.Factory(AadlConstants.INSTANTIATION_OBJECT_MARKER)));
SystemInstance root = instantiateModel.createSystemInstance(ci, res);
return root;
}
use of org.osate.aadl2.instance.SystemInstance in project osate2 by osate.
the class InstantiateModel method hasSubcomponentInstance.
/*
* check to see if the specified subcomponent already exists as component
* instance in the ancestry
*/
private boolean hasSubcomponentInstance(ComponentInstance ci, Subcomponent sub) {
ComponentInstance parent = ci;
while (parent != null && !(parent instanceof SystemInstance)) {
Subcomponent psc = parent.getSubcomponent();
if (psc == sub) {
return true;
}
parent = (ComponentInstance) parent.eContainer();
}
return false;
}
Aggregations