Exemplo n.º 1
0
  public HashSet calculateNextStates(
      PDMState state,
      PDMStateSpace statespace,
      boolean root,
      boolean failure,
      int numStates,
      int breadth) {
    HashSet result = new HashSet();
    HashSet data = state.dataElements;
    HashSet exec1 = state.executedOperations;
    HashSet failed = state.failedOperations;

    HashSet execOps = calculateExecutableOperations(data, exec1, failed, root);
    Iterator it = execOps.iterator();
    int b = 0;
    int bLimit = 0;
    if (!failure) {
      bLimit = breadth;
    } else {
      bLimit = breadth / 2;
    }
    // for each executable operation a new state is created
    while (it.hasNext() && i < numStates && b < bLimit) {
      if (!failure) {
        PDMOperation op = (PDMOperation) it.next();
        PDMDataElement d = op.getOutputElement();
        // First, add the state with the operation 'op' succesfully
        // executed
        HashSet ins2 = (HashSet) data.clone(); // NB: is it necessary to
        // clear this clone
        // again?
        ins2.add(d);
        HashSet exec2 = (HashSet) exec1.clone();
        exec2.add(op);
        PDMState s = checkIfStateExists(statespace, ins2, exec2, failed);
        // Check whether the new state already exists
        // If so, then another link to this state is made
        if (!(s == null)) {
          PDMState st = s;
          PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), 1.0);
          statespace.addEdge(edge);
        }
        // If not, a new state is created and linked to the current
        // state
        else {
          String name = "state" + i;
          // int num = checkStatusOfState(statespace, )
          PDMState st = new PDMState(statespace, name, ins2, exec2, failed);
          statespace.addState(st);
          result.add(st);
          PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), 1.0);
          statespace.addEdge(edge);
          i++;
          b++;
        }
      }

      // Then, if failure of operations is considered, add the state with
      // the failed operations 'op'.
      if (failure) {
        PDMOperation op = (PDMOperation) it.next();
        PDMDataElement d = op.getOutputElement();
        // First, add the state with the operation 'op' succesfully
        // executed
        HashSet ins2 = (HashSet) data.clone(); // NB: is it necessary to
        // clear this clone
        // again?
        ins2.add(d);
        HashSet exec2 = (HashSet) exec1.clone();
        exec2.add(op);
        PDMState s = checkIfStateExists(statespace, ins2, exec2, failed);
        // Check whether the new state already exists
        // If so, then another link to this state is made
        if (!(s == null)) {
          PDMState st = s;
          double prob = 1.0 - (op.getFailureProbability());
          PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), prob);
          statespace.addEdge(edge);
        }
        // If not, a new state is created and linked to the current
        // state
        else {
          String name = "state" + i;
          // int num = checkStatusOfState(statespace, )
          PDMState st = new PDMState(statespace, name, ins2, exec2, failed);
          statespace.addState(st);
          result.add(st);
          double prob = 1.0 - (op.getFailureProbability());
          PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), prob);
          statespace.addEdge(edge);
          i++;
          b++;
        }
        HashSet failed2 = (HashSet) failed.clone();
        failed2.add(op);
        PDMState s2 = checkIfStateExists(statespace, data, exec1, failed2);
        if (!(s2 == null)) {
          PDMState st = s2;
          PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), op.getFailureProbability());
          statespace.addEdge(edge);
        }
        // If not, a new state is created and linked to the current
        // state
        else {
          String name = "state" + i;
          PDMState st = new PDMState(statespace, name, data, exec1, failed2);
          statespace.addState(st);
          result.add(st);
          PDMStateEdge edge = new PDMStateEdge(state, st, op.getID(), op.getFailureProbability());
          statespace.addEdge(edge);
          i++;
          b++;
        }
        // failed2.clear();
      }
    }
    return result;
  }