Search in sources :

Example 1 with BehaviorAction

use of org.osate.ba.aadlba.BehaviorAction in project osate2 by osate.

the class AadlBaLegalityRulesChecker method buildActionSetAssignedTargetLists.

/**
 * Recursively builds a list of assigned target contained
 * in a given BehaviorActions tree and checks for duplicated targets every time
 * it meet a Behavior Action Set node. It populates the given set with
 * duplicated targets.<BR><BR>
 *
 * A special attention is given to report legality rules D.6.(L3) and (L4)
 * failures : in order to help the user to correct his errors, the duplicates
 * list contains all instances of duplicated assigned targets.
 *
 * @param beActions The given BehaviorActions tree.
 * @param lActionSetDcr The list of assigned targets
 * @param lDuplicates The set of duplicated assigned targets.
 */
// As Java 1.6 can't create generic array.
@SuppressWarnings("unchecked")
private void buildActionSetAssignedTargetLists(BehaviorActions beActions, List<Target> lActionSetTar, Set<Target> lDuplicates) {
    // Basic action cases.
    if (beActions instanceof BasicAction) {
        if (beActions instanceof AssignmentAction) {
            lActionSetTar.add(((AssignmentAction) beActions).getTarget());
        }
        return;
    }
    // Behavior Action Block case.
    if (beActions instanceof BehaviorActionBlock) {
        BehaviorActionBlock tmp = (BehaviorActionBlock) beActions;
        buildActionSetAssignedTargetLists(tmp.getContent(), lActionSetTar, lDuplicates);
        return;
    }
    // If Statement case.
    if (beActions instanceof IfStatement) {
        IfStatement ifStat = (IfStatement) beActions;
        BehaviorActions tmp = ifStat.getBehaviorActions();
        buildActionSetAssignedTargetLists(tmp, lActionSetTar, lDuplicates);
        if (ifStat.getElseStatement() != null) {
            tmp = ifStat.getElseStatement();
            buildActionSetAssignedTargetLists(tmp, lActionSetTar, lDuplicates);
        }
        return;
    }
    if (beActions instanceof ElseStatement) {
        ElseStatement elseStat = (ElseStatement) beActions;
        BehaviorActions tmp = elseStat.getBehaviorActions();
        buildActionSetAssignedTargetLists(tmp, lActionSetTar, lDuplicates);
        return;
    }
    // Loop Statement case.
    if (beActions instanceof LoopStatement) {
        LoopStatement tmp = (LoopStatement) beActions;
        buildActionSetAssignedTargetLists(tmp.getBehaviorActions(), lActionSetTar, lDuplicates);
        return;
    }
    // ***** Processing containers:
    // List of BehaviorAction objects contained in the given BehaviorActions
    // tree.
    List<BehaviorAction> lbeActs = ((BehaviorActionCollection) beActions).getActions();
    // Behavior Action Sequence case:
    if (beActions instanceof BehaviorActionSequence) {
        for (BehaviorAction tmp : lbeActs) {
            buildActionSetAssignedTargetLists(tmp, lActionSetTar, lDuplicates);
        }
        return;
    }
    // Behavior Action Set case:
    // Current BehaviorAction object.
    BehaviorAction behAct;
    // Creates an array which will handle the lists of assigned targets. One
    // list for each behavior action of the action set.
    List<Target>[] llActionSetTar = null;
    llActionSetTar = new List[lbeActs.size()];
    // For each BehaviorAction of the given BehaviorActionCollection.
    for (int i = 0; i < lbeActs.size(); i++) {
        behAct = lbeActs.get(i);
        llActionSetTar[i] = new ArrayList<Target>();
        buildActionSetAssignedTargetLists(behAct, llActionSetTar[i], lDuplicates);
    }
    // Check for duplicated assigned targets in the lists of the
    // action set. Complexity is O(n^2) as the lists are not sorted.
    List<Target> lCurrent = null;
    List<Target> lOther = null;
    // Optimization flag to avoid adding the same target to the duplicates
    // set.
    boolean hasToAdd = true;
    // Compare the lists between them.
    for (int i = 0; i < llActionSetTar.length - 1; i++) {
        lCurrent = llActionSetTar[i];
        for (Target currentTar : lCurrent) {
            // Compare current list with the others.
            for (int j = i + 1; j < llActionSetTar.length; j++) {
                lOther = llActionSetTar[j];
                for (Target otherTar : lOther) {
                    // Case of duplicated assigned target.
                    if (AadlBaUtils.isSameTarget(currentTar, otherTar)) {
                        // Add the current target if it hasn't be done yet.
                        if (hasToAdd) {
                            lDuplicates.add(currentTar);
                            hasToAdd = false;
                        }
                        // Add the other target to help the user finding his errors.
                        lDuplicates.add(otherTar);
                    }
                }
            }
            // Reset the flag for the next target.
            hasToAdd = true;
        }
    }
    // higher level action set checking.
    for (List<Target> l : llActionSetTar) {
        lActionSetTar.addAll(l);
    }
}
Also used : BasicAction(org.osate.ba.aadlba.BasicAction) AssignmentAction(org.osate.ba.aadlba.AssignmentAction) LoopStatement(org.osate.ba.aadlba.LoopStatement) IfStatement(org.osate.ba.aadlba.IfStatement) Target(org.osate.ba.aadlba.Target) BehaviorAction(org.osate.ba.aadlba.BehaviorAction) BehaviorActionCollection(org.osate.ba.aadlba.BehaviorActionCollection) ElseStatement(org.osate.ba.aadlba.ElseStatement) BehaviorActionBlock(org.osate.ba.aadlba.BehaviorActionBlock) BehaviorActions(org.osate.ba.aadlba.BehaviorActions) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) BehaviorActionSequence(org.osate.ba.aadlba.BehaviorActionSequence)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 EList (org.eclipse.emf.common.util.EList)1 AssignmentAction (org.osate.ba.aadlba.AssignmentAction)1 BasicAction (org.osate.ba.aadlba.BasicAction)1 BehaviorAction (org.osate.ba.aadlba.BehaviorAction)1 BehaviorActionBlock (org.osate.ba.aadlba.BehaviorActionBlock)1 BehaviorActionCollection (org.osate.ba.aadlba.BehaviorActionCollection)1 BehaviorActionSequence (org.osate.ba.aadlba.BehaviorActionSequence)1 BehaviorActions (org.osate.ba.aadlba.BehaviorActions)1 ElseStatement (org.osate.ba.aadlba.ElseStatement)1 IfStatement (org.osate.ba.aadlba.IfStatement)1 LoopStatement (org.osate.ba.aadlba.LoopStatement)1 Target (org.osate.ba.aadlba.Target)1