Program: 74479
Project: 74479
Buggy source:
/** 
 * This is an experimental function which tries to analyze a ModalModel and flatten it into a single FSMActor. The purpose of implementation is  to understand the underlying structure of the ModalModel and Refinement, so that in the future we may have better implementation. Also it would  be suitable for the exhibition of BEAR 2008.  In our current implementation we only allow one additional layer for  the refinement; an arbitrary layer of refinement would lead to state  explosion of the system. Also the additional layer must be a finite  state refinement so that the conversion is possible. But it is easy to expand this functionality into multiple layer.  Note that in the current context of ModalModel, when state machine refinement is used, it is not possible to generate further refinement, meaning that the current implementation is powerful enough to deal with state refinement.
 * @param model Whole System under analysis.
 * @return Equivalent FSMActor for later analysis.
 */
private static FSMActor _rewriteModalModelWithStateRefinementToFSMActor(ModalModel modelmodel) throws IllegalActionException, NameDuplicationException, CloneNotSupportedException {
  FSMActor model=modelmodel.getController();
  FSMActor returnFSMActor=new FSMActor(model.workspace());
  returnFSMActor.setName(modelmodel.getName());
  Iterator states=model.entityList().iterator();
  while (states.hasNext()) {
    NamedObj state=(NamedObj)states.next();
    if (state instanceof State) {
      String refinementList=((State)state).refinementName.getExpression();
      if (refinementList == null) {
        State newState=(State)state.clone();
        newState.setName(state.getName());
        (newState).setContainer(returnFSMActor);
        if ((model.getInitialState() == state)) {
          newState.isInitialState.setToken("String_Node_Str");
        }
        newState.moveToFirst();
      }
 else       if (refinementList.equalsIgnoreCase("String_Node_Str")) {
        State newState=(State)state.clone();
        newState.setName(state.getName());
        (newState).setContainer(returnFSMActor);
        if ((model.getInitialState() == state)) {
          newState.isInitialState.setToken("String_Node_Str");
        }
        newState.moveToFirst();
      }
 else {
        TypedActor[] actors=((State)state).getRefinement();
        if (actors != null) {
          if (actors.length > 1) {
            System.out.println("String_Node_Str");
          }
 else {
            TypedActor innerActor=actors[0];
            if (innerActor instanceof FSMActor) {
              Iterator innerStates=((FSMActor)innerActor).entityList().iterator();
              while (innerStates.hasNext()) {
                NamedObj innerState=(NamedObj)innerStates.next();
                if (innerState instanceof State) {
                  State newState=(State)innerState.clone();
                  newState.setName(state.getName().trim() + "String_Node_Str" + innerState.getName().trim());
                  newState.setContainer(returnFSMActor);
                  if ((model.getInitialState() == state) && ((FSMActor)innerActor).getInitialState() == innerState) {
                    newState.isInitialState.setToken("String_Node_Str");
                  }
                  newState.moveToFirst();
                }
              }
              Iterator innerTransitions=((FSMActor)innerActor).relationList().iterator();
              while (innerTransitions.hasNext()) {
                Relation innerTransition=(Relation)innerTransitions.next();
                if (!(innerTransition instanceof Transition)) {
                  continue;
                }
                State source=((Transition)innerTransition).sourceState();
                State destination=((Transition)innerTransition).destinationState();
                Transition newTransition=(Transition)innerTransition.clone();
                newTransition.setName(((State)state).getName() + "String_Node_Str" + innerTransition.getName());
                Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
                State sCorresponding=null;
                State dCorresponding=null;
                while (returnFSMActorStates.hasNext()) {
                  NamedObj cState=(NamedObj)returnFSMActorStates.next();
                  if (cState instanceof State) {
                    if (((State)cState).getName().equalsIgnoreCase(((State)state).getName().trim() + "String_Node_Str" + source.getName().trim())) {
                      sCorresponding=(State)cState;
                    }
                  }
                }
                returnFSMActorStates=returnFSMActor.entityList().iterator();
                while (returnFSMActorStates.hasNext()) {
                  NamedObj cState=(NamedObj)returnFSMActorStates.next();
                  if (cState instanceof State) {
                    if (((State)cState).getName().equalsIgnoreCase(((State)state).getName().trim() + "String_Node_Str" + destination.getName().trim())) {
                      dCorresponding=(State)cState;
                    }
                  }
                }
                Port s=sCorresponding.outgoingPort;
                Port d=dCorresponding.incomingPort;
                newTransition.unlinkAll();
                newTransition.setContainer(returnFSMActor);
                newTransition.moveToFirst();
                s.link(newTransition);
                d.link(newTransition);
              }
            }
 else {
              throw new IllegalActionException("String_Node_Str");
            }
          }
        }
 else {
        }
      }
    }
  }
  Iterator Transitions=model.relationList().iterator();
  while (Transitions.hasNext()) {
    Relation transition=(Relation)Transitions.next();
    if (!(transition instanceof Transition)) {
      continue;
    }
    State source=((Transition)transition).sourceState();
    State destination=((Transition)transition).destinationState();
    TypedActor[] sActors=((State)source).getRefinement();
    TypedActor[] dActors=((State)destination).getRefinement();
    if ((sActors == null) && (dActors == null)) {
      Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
      State sCorresponding=null;
      State dCorresponding=null;
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(source.getName().trim())) {
            sCorresponding=(State)cState;
          }
        }
      }
      returnFSMActorStates=returnFSMActor.entityList().iterator();
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(destination.getName().trim())) {
            dCorresponding=(State)cState;
          }
        }
      }
      Port s=sCorresponding.outgoingPort;
      Port d=dCorresponding.incomingPort;
      Transition newTransition=(Transition)transition.clone();
      newTransition.unlinkAll();
      newTransition.setContainer(returnFSMActor);
      newTransition.moveToFirst();
      s.link(newTransition);
      d.link(newTransition);
      newTransition.setName(source.getName().trim() + "String_Node_Str" + destination.getName().trim());
    }
 else     if ((sActors == null) && (dActors != null)) {
      TypedActor dInnerActor=dActors[0];
      if (!(dInnerActor instanceof FSMActor)) {
      }
      Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
      State sCorresponding=null;
      State dCorresponding=null;
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(source.getName().trim())) {
            sCorresponding=(State)cState;
          }
        }
      }
      returnFSMActorStates=returnFSMActor.entityList().iterator();
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(destination.getName().trim() + "String_Node_Str" + ((FSMActor)dInnerActor).getInitialState().getName().trim())) {
            dCorresponding=(State)cState;
          }
        }
      }
      Port s=sCorresponding.outgoingPort;
      Port d=dCorresponding.incomingPort;
      Transition newTransition=(Transition)transition.clone();
      newTransition.unlinkAll();
      newTransition.setContainer(returnFSMActor);
      newTransition.moveToFirst();
      s.link(newTransition);
      d.link(newTransition);
      newTransition.setName(source.getName().trim() + "String_Node_Str" + destination.getName().trim()+ "String_Node_Str"+ ((FSMActor)dInnerActor).getInitialState().getName().trim());
    }
 else     if ((sActors != null) && (dActors == null)) {
      TypedActor innerActor=sActors[0];
      if (innerActor instanceof FSMActor) {
        Iterator innerStates=((FSMActor)innerActor).entityList().iterator();
        while (innerStates.hasNext()) {
          NamedObj innerState=(NamedObj)innerStates.next();
          if (innerState instanceof State) {
            Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
            State sCorresponding=null;
            State dCorresponding=null;
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(source.getName().trim() + "String_Node_Str" + innerState.getName().trim())) {
                  sCorresponding=(State)cState;
                }
              }
            }
            returnFSMActorStates=returnFSMActor.entityList().iterator();
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(destination.getName().trim())) {
                  dCorresponding=(State)cState;
                }
              }
            }
            Port s=sCorresponding.outgoingPort;
            Port d=dCorresponding.incomingPort;
            Transition newTransition=(Transition)transition.clone();
            newTransition.unlinkAll();
            newTransition.setContainer(returnFSMActor);
            newTransition.moveToFirst();
            s.link(newTransition);
            d.link(newTransition);
            newTransition.setName(source.getName().trim() + "String_Node_Str" + innerState.getName().trim()+ "String_Node_Str"+ destination.getName().trim());
          }
        }
      }
    }
 else {
      TypedActor sInnerActor=sActors[0];
      TypedActor dInnerActor=dActors[0];
      String newDestName="String_Node_Str";
      if (dInnerActor instanceof FSMActor) {
        newDestName=destination.getName().trim() + "String_Node_Str" + ((FSMActor)dInnerActor).getInitialState().getName().trim();
      }
      if (sInnerActor instanceof FSMActor) {
        Iterator innerStates=((FSMActor)sInnerActor).entityList().iterator();
        while (innerStates.hasNext()) {
          NamedObj innerState=(NamedObj)innerStates.next();
          if (innerState instanceof State) {
            Transition newTransition=(Transition)transition.clone(model.workspace());
            newTransition.unlinkAll();
            Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
            State sCorresponding=null;
            State dCorresponding=null;
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(source.getName().trim() + "String_Node_Str" + innerState.getName().trim())) {
                  sCorresponding=(State)cState;
                }
              }
            }
            returnFSMActorStates=returnFSMActor.entityList().iterator();
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(newDestName)) {
                  dCorresponding=(State)cState;
                }
              }
            }
            Port s=sCorresponding.outgoingPort;
            Port d=dCorresponding.incomingPort;
            newTransition.unlinkAll();
            newTransition.setContainer(returnFSMActor);
            newTransition.moveToFirst();
            s.link(newTransition);
            d.link(newTransition);
            newTransition.setName(source.getName().trim() + "String_Node_Str" + innerState.getName().trim()+ "String_Node_Str"+ newDestName);
          }
        }
      }
    }
  }
  return returnFSMActor;
}

Fixed version (ground truth):
/** 
 * This is an experimental function which tries to analyze a ModalModel and flatten it into a single FSMActor. The purpose of implementation is  to understand the underlying structure of the ModalModel and Refinement, so that in the future we may have better implementation. Also it would  be suitable for the exhibition of BEAR 2008.  In our current implementation we only allow one additional layer for  the refinement; an arbitrary layer of refinement would lead to state  explosion of the system. Also the additional layer must be a finite  state refinement so that the conversion is possible. But it is easy to expand this functionality into multiple layer.  Note that in the current context of ModalModel, when state machine refinement is used, it is not possible to generate further refinement, meaning that the current implementation is powerful enough to deal with state refinement.
 * @param model Whole System under analysis.
 * @return Equivalent FSMActor for later analysis.
 */
private static FSMActor _rewriteModalModelWithStateRefinementToFSMActor(ModalModel modelmodel) throws IllegalActionException, NameDuplicationException, CloneNotSupportedException {
  FSMActor model=modelmodel.getController();
  FSMActor returnFSMActor=new FSMActor(model.workspace());
  returnFSMActor.setName(modelmodel.getName());
  Iterator states=model.entityList().iterator();
  while (states.hasNext()) {
    NamedObj state=(NamedObj)states.next();
    if (state instanceof State) {
      String refinementList=((State)state).refinementName.getExpression();
      if (refinementList == null) {
        State newState=(State)state.clone();
        newState.setName(state.getName());
        (newState).setContainer(returnFSMActor);
        if ((model.getInitialState() == state)) {
          newState.isInitialState.setToken("String_Node_Str");
        }
        newState.moveToFirst();
      }
 else       if (refinementList.equalsIgnoreCase("String_Node_Str")) {
        State newState=(State)state.clone();
        newState.setName(state.getName());
        (newState).setContainer(returnFSMActor);
        if ((model.getInitialState() == state)) {
          newState.isInitialState.setToken("String_Node_Str");
        }
        newState.moveToFirst();
      }
 else {
        TypedActor[] actors=((State)state).getRefinement();
        if (actors != null) {
          if (actors.length > 1) {
            System.out.println("String_Node_Str");
          }
 else {
            TypedActor innerActor=actors[0];
            if (innerActor instanceof FSMActor) {
              Iterator innerStates=((FSMActor)innerActor).entityList().iterator();
              while (innerStates.hasNext()) {
                NamedObj innerState=(NamedObj)innerStates.next();
                if (innerState instanceof State) {
                  State newState=(State)innerState.clone();
                  newState.setName(state.getName().trim() + "String_Node_Str" + innerState.getName().trim());
                  newState.setContainer(returnFSMActor);
                  if ((model.getInitialState() == state) && ((FSMActor)innerActor).getInitialState() == innerState) {
                    newState.isInitialState.setToken("String_Node_Str");
                  }
                  newState.moveToFirst();
                }
              }
              Iterator innerTransitions=((FSMActor)innerActor).relationList().iterator();
              while (innerTransitions.hasNext()) {
                Relation innerTransition=(Relation)innerTransitions.next();
                if (!(innerTransition instanceof Transition)) {
                  continue;
                }
                State source=((Transition)innerTransition).sourceState();
                State destination=((Transition)innerTransition).destinationState();
                Transition newTransition=(Transition)innerTransition.clone();
                newTransition.setName(((State)state).getName() + "String_Node_Str" + innerTransition.getName());
                Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
                State sCorresponding=null;
                State dCorresponding=null;
                while (returnFSMActorStates.hasNext()) {
                  NamedObj cState=(NamedObj)returnFSMActorStates.next();
                  if (cState instanceof State) {
                    if (((State)cState).getName().equalsIgnoreCase(((State)state).getName().trim() + "String_Node_Str" + source.getName().trim())) {
                      sCorresponding=(State)cState;
                    }
                  }
                }
                returnFSMActorStates=returnFSMActor.entityList().iterator();
                while (returnFSMActorStates.hasNext()) {
                  NamedObj cState=(NamedObj)returnFSMActorStates.next();
                  if (cState instanceof State) {
                    if (((State)cState).getName().equalsIgnoreCase(((State)state).getName().trim() + "String_Node_Str" + destination.getName().trim())) {
                      dCorresponding=(State)cState;
                    }
                  }
                }
                Port s=sCorresponding.outgoingPort;
                Port d=dCorresponding.incomingPort;
                newTransition.unlinkAll();
                newTransition.setContainer(returnFSMActor);
                newTransition.moveToFirst();
                s.link(newTransition);
                d.link(newTransition);
              }
            }
 else {
              throw new IllegalActionException("String_Node_Str");
            }
          }
        }
 else {
        }
      }
    }
  }
  Iterator Transitions=model.relationList().iterator();
  while (Transitions.hasNext()) {
    Relation transition=(Relation)Transitions.next();
    if (!(transition instanceof Transition)) {
      continue;
    }
    State source=((Transition)transition).sourceState();
    State destination=((Transition)transition).destinationState();
    TypedActor[] sActors=((State)source).getRefinement();
    TypedActor[] dActors=((State)destination).getRefinement();
    if ((sActors == null) && (dActors == null)) {
      Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
      State sCorresponding=null;
      State dCorresponding=null;
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(source.getName().trim())) {
            sCorresponding=(State)cState;
          }
        }
      }
      returnFSMActorStates=returnFSMActor.entityList().iterator();
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(destination.getName().trim())) {
            dCorresponding=(State)cState;
          }
        }
      }
      Port s=sCorresponding.outgoingPort;
      Port d=dCorresponding.incomingPort;
      Transition newTransition=(Transition)transition.clone();
      newTransition.unlinkAll();
      newTransition.setContainer(returnFSMActor);
      newTransition.moveToFirst();
      s.link(newTransition);
      d.link(newTransition);
      newTransition.setName(source.getName().trim() + "String_Node_Str" + destination.getName().trim());
    }
 else     if ((sActors == null) && (dActors != null)) {
      TypedActor dInnerActor=dActors[0];
      if (!(dInnerActor instanceof FSMActor)) {
      }
      Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
      State sCorresponding=null;
      State dCorresponding=null;
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(source.getName().trim())) {
            sCorresponding=(State)cState;
          }
        }
      }
      returnFSMActorStates=returnFSMActor.entityList().iterator();
      while (returnFSMActorStates.hasNext()) {
        NamedObj cState=(NamedObj)returnFSMActorStates.next();
        if (cState instanceof State) {
          if (((State)cState).getName().equalsIgnoreCase(destination.getName().trim() + "String_Node_Str" + ((FSMActor)dInnerActor).getInitialState().getName().trim())) {
            dCorresponding=(State)cState;
          }
        }
      }
      Port s=sCorresponding.outgoingPort;
      Port d=dCorresponding.incomingPort;
      Transition newTransition=(Transition)transition.clone();
      newTransition.unlinkAll();
      newTransition.setContainer(returnFSMActor);
      newTransition.moveToFirst();
      s.link(newTransition);
      d.link(newTransition);
      newTransition.setName(source.getName().trim() + "String_Node_Str" + destination.getName().trim()+ "String_Node_Str"+ ((FSMActor)dInnerActor).getInitialState().getName().trim());
    }
 else     if ((sActors != null) && (dActors == null)) {
      TypedActor innerActor=sActors[0];
      if (innerActor instanceof FSMActor) {
        Iterator innerStates=((FSMActor)innerActor).entityList().iterator();
        while (innerStates.hasNext()) {
          NamedObj innerState=(NamedObj)innerStates.next();
          if (innerState instanceof State) {
            Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
            State sCorresponding=null;
            State dCorresponding=null;
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(source.getName().trim() + "String_Node_Str" + innerState.getName().trim())) {
                  sCorresponding=(State)cState;
                }
              }
            }
            returnFSMActorStates=returnFSMActor.entityList().iterator();
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(destination.getName().trim())) {
                  dCorresponding=(State)cState;
                }
              }
            }
            Port s=sCorresponding.outgoingPort;
            Port d=dCorresponding.incomingPort;
            Transition newTransition=(Transition)transition.clone();
            newTransition.unlinkAll();
            newTransition.setContainer(returnFSMActor);
            newTransition.moveToFirst();
            s.link(newTransition);
            d.link(newTransition);
            newTransition.setName(source.getName().trim() + "String_Node_Str" + innerState.getName().trim()+ "String_Node_Str"+ destination.getName().trim());
          }
        }
      }
    }
 else {
      TypedActor sInnerActor=null;
      TypedActor dInnerActor=null;
      if (sActors[0] != null) {
        sInnerActor=sActors[0];
      }
      if (dActors[0] != null) {
        dInnerActor=dActors[0];
      }
      String newDestName="String_Node_Str";
      if (dInnerActor instanceof FSMActor) {
        newDestName=destination.getName().trim() + "String_Node_Str" + ((FSMActor)dInnerActor).getInitialState().getName().trim();
      }
      if (sInnerActor instanceof FSMActor) {
        Iterator innerStates=((FSMActor)sInnerActor).entityList().iterator();
        while (innerStates.hasNext()) {
          NamedObj innerState=(NamedObj)innerStates.next();
          if (innerState instanceof State) {
            Transition newTransition=(Transition)transition.clone(model.workspace());
            newTransition.unlinkAll();
            Iterator returnFSMActorStates=returnFSMActor.entityList().iterator();
            State sCorresponding=null;
            State dCorresponding=null;
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(source.getName().trim() + "String_Node_Str" + innerState.getName().trim())) {
                  sCorresponding=(State)cState;
                }
              }
            }
            returnFSMActorStates=returnFSMActor.entityList().iterator();
            while (returnFSMActorStates.hasNext()) {
              NamedObj cState=(NamedObj)returnFSMActorStates.next();
              if (cState instanceof State) {
                if (((State)cState).getName().equalsIgnoreCase(newDestName)) {
                  dCorresponding=(State)cState;
                }
              }
            }
            Port s=sCorresponding.outgoingPort;
            Port d=dCorresponding.incomingPort;
            newTransition.unlinkAll();
            newTransition.setContainer(returnFSMActor);
            newTransition.moveToFirst();
            s.link(newTransition);
            d.link(newTransition);
            newTransition.setName(source.getName().trim() + "String_Node_Str" + innerState.getName().trim()+ "String_Node_Str"+ newDestName);
          }
        }
      }
    }
  }
  return returnFSMActor;
}