예제 #1
0
  public void add(LinkedListNode node) {
    DefeasibleLogicalDependency newDep =
        (DefeasibleLogicalDependency) ((LinkedListEntry) node).getObject();
    newDep.setStatus(resolveStatus(newDep));

    Rule rule = newDep.getJustifier().getRule();

    // first iterate to see if this new dep is defeated. If it's defeated, it can no longer impacts
    // any deps
    // if we checked what it defeats, and later this was defeated, we would have undo action. So we
    // do the cheaper work first.
    boolean wasDefeated = false;
    for (LinkedListEntry<DefeasibleLogicalDependency> existingNode = rootUndefeated;
        existingNode != null;
        existingNode = existingNode.getNext()) {
      DefeasibleLogicalDependency existingDep = existingNode.getObject();
      wasDefeated = checkAndApplyIsDefeated(newDep, rule, existingDep);
      if (wasDefeated) {
        break;
      }
    }

    if (!wasDefeated) {
      LinkedListEntry<DefeasibleLogicalDependency> stagedDeps = null;
      // for (DefeasibleLogicalDependency existingDep = rootUndefeated; existingDep != null; ) {
      for (LinkedListEntry<DefeasibleLogicalDependency> existingNode = rootUndefeated;
          existingNode != null; ) {
        LinkedListEntry<DefeasibleLogicalDependency> next = existingNode.getNext();
        DefeasibleLogicalDependency existingDep = existingNode.getObject();

        if (checkAndApplyIsDefeated(existingDep, existingDep.getJustifier().getRule(), newDep)) {
          // fist remove it from the undefeated list
          removeUndefeated(existingDep, existingNode);
          if (existingDep.getRootDefeated() != null) {
            // build up the list of staged deps, that will need to be reprocessed
            if (stagedDeps == null) {
              stagedDeps = existingDep.getRootDefeated();
            } else {
              stagedDeps.setPrevious(existingDep.getTailDefeated());
              stagedDeps = existingDep.getRootDefeated();
            }
          }
          existingDep.clearDefeated();
        }
        existingNode = next;
      }
      addUndefeated(newDep, (LinkedListEntry<DefeasibleLogicalDependency>) node);
      // now process the staged
      reprocessDefeated(stagedDeps);
    }
    updateStatus();
  }