Ejemplo n.º 1
0
  public void removeUndefeated(
      DefeasibleLogicalDependency dep, LinkedListEntry<DefeasibleLogicalDependency> node) {
    boolean pos = dep.getValue() == null || MODE.POSITIVE.getId().equals(dep.getValue().toString());
    switch (dep.getStatus()) {
      case DEFINITELY:
        if (pos) {
          definitelyPosCount--;
          if (definitelyPosCount == 0) {
            statusMask = statusMask ^ DEFINITELY_POS_BIT;
          }
        } else {
          definitelyNegCount--;
          if (definitelyNegCount == 0) {
            statusMask = statusMask ^ DEFINITELY_NEG_BIT;
          }
        }
        break;
      case DEFEASIBLY:
        if (pos) {
          defeasiblyPosCount--;
          if (defeasiblyPosCount == 0) {
            statusMask = statusMask ^ DEFEASIBLY_POS_BIT;
          }
        } else {
          defeasiblyNegCount--;
          if (defeasiblyNegCount == 0) {
            statusMask = statusMask ^ DEFEASIBLY_NEG_BIT;
          }
        }
        break;
      case DEFEATEDLY:
        if (pos) {
          defeatedlyPosCount--;
          if (defeatedlyPosCount == 0) {
            statusMask = statusMask ^ DEFEATEDLY_POS_BIT;
          }
        } else {
          defeatedlyNegCount--;
          if (defeatedlyNegCount == 0) {
            statusMask = statusMask ^ DEFEATEDLY_NEG_BIT;
          }
        }
        break;
      case UNDECIDABLY:
        throw new IllegalStateException("Individual logical dependencies cannot be undecidably");
    }

    if (this.rootUndefeated == node) {
      removeFirst();
    } else if (this.tailUndefeated == node) {
      removeLast();
    } else {
      node.getPrevious().setNext(node.getNext());
      ((LinkedListNode) node.getNext()).setPrevious(node.getPrevious());
      node.nullPrevNext();
    }
  }
Ejemplo n.º 2
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();
  }
Ejemplo n.º 3
0
  public void addUndefeated(
      DefeasibleLogicalDependency dep, LinkedListEntry<DefeasibleLogicalDependency> node) {
    boolean pos = dep.getValue() == null || MODE.POSITIVE.getId().equals(dep.getValue().toString());
    switch (dep.getStatus()) {
      case DEFINITELY:
        if (pos) {
          definitelyPosCount++;
          statusMask = statusMask | DEFINITELY_POS_BIT;
        } else {
          definitelyNegCount++;
          statusMask = statusMask | DEFINITELY_NEG_BIT;
        }
        break;
      case DEFEASIBLY:
        if (pos) {
          defeasiblyPosCount++;
          statusMask = statusMask | DEFEASIBLY_POS_BIT;
        } else {
          defeasiblyNegCount++;
          statusMask = statusMask | DEFEASIBLY_NEG_BIT;
        }
        break;
      case DEFEATEDLY:
        if (pos) {
          defeatedlyPosCount++;
          statusMask = statusMask | DEFEATEDLY_POS_BIT;
        } else {
          defeatedlyNegCount++;
          statusMask = statusMask | DEFEATEDLY_NEG_BIT;
        }
        break;
      case UNDECIDABLY:
        throw new IllegalStateException("Individual logical dependencies cannot be undecidably");
    }

    if (rootUndefeated == null) {
      rootUndefeated = node;
      tailUndefeated = node;
    } else {
      if (dep.getStatus() == DefeasibilityStatus.DEFINITELY) {
        // Strict dependencies at to the front
        rootUndefeated.setPrevious(node);
        node.setNext(rootUndefeated);
        rootUndefeated = node;
      } else {
        // add to end
        tailUndefeated.setNext(node);
        node.setPrevious(tailUndefeated);
        tailUndefeated = node;
      }
    }
  }
Ejemplo n.º 4
0
  public void cancel(PropagationContext propagationContext) {
    // get all but last, as that we'll do via the BeliefSystem, for cleanup
    // note we don't update negative, conflict counters. It's needed for the last cleanup operation
    FastIterator it = iterator();
    for (LinkedListEntry<DefeasibleLogicalDependency> node = getFirst(); node != tailUndefeated; ) {
      LinkedListEntry<DefeasibleLogicalDependency> temp =
          (LinkedListEntry<DefeasibleLogicalDependency>)
              it.next(node); // get next, as we are about to remove it
      final DefeasibleLogicalDependency dep = node.getObject();
      dep.getJustifier().getLogicalDependencies().remove(dep);
      remove(dep);
      node = temp;
    }

    LinkedListEntry last = (LinkedListEntry) getFirst();
    final LogicalDependency node = (LogicalDependency) last.getObject();
    node.getJustifier().getLogicalDependencies().remove(node);
    // beliefSystem.delete( node, this, context );
    positiveFactHandle = null;
    negativeFactHandle = null;
  }
Ejemplo n.º 5
0
  public void remove(LinkedListNode node) {
    DefeasibleLogicalDependency dep =
        ((LinkedListEntry<DefeasibleLogicalDependency>) node).getObject();

    if (dep.getDefeatedBy() != null) {
      // Defeated deps do not have defeated defeated lists of their own, so just remove.
      DefeasibleLogicalDependency defeater = dep.getDefeatedBy();
      defeater.removeDefeated(dep);
    } else {
      // ins undefeated, process it's defeated list if they exist
      removeUndefeated(dep, (LinkedListEntry<DefeasibleLogicalDependency>) node);
      if (dep.getRootDefeated() != null) {
        reprocessDefeated(dep.getRootDefeated());
      }
    }
    updateStatus();
  }
Ejemplo n.º 6
0
    public Entry next(Entry object) {
      DefeasibleLogicalDependency dep = (DefeasibleLogicalDependency) object;
      if (dep.getRootDefeated() != null) {
        // try going down the list of defeated first
        return dep.getRootDefeated();
      }

      if (dep.getNext() != null) {
        return dep.getNext();
      }

      if (dep.getDefeatedBy() != null) {
        // go back up to the parent undefeated, and try the next undefeated
        return dep.getDefeatedBy().getNext();
      }

      return null; // nothing more to iterate too.
    }
Ejemplo n.º 7
0
 private boolean checkAndApplyIsDefeated(
     DefeasibleLogicalDependency potentialInferior,
     Rule rule,
     DefeasibleLogicalDependency potentialSuperior) {
   if (potentialSuperior.getDefeats() == null) {
     return false;
   }
   if (potentialSuperior.getStatus() == DefeasibilityStatus.DEFINITELY
       && potentialInferior.getStatus() != DefeasibilityStatus.DEFINITELY) {
     potentialSuperior.addDefeated(potentialInferior);
     return true;
   }
   // adds the references that defeat the current node
   if (Arrays.binarySearch(potentialSuperior.getDefeats(), rule.getName()) >= 0
       || Arrays.binarySearch(
               potentialSuperior.getDefeats(), rule.getPackage() + "." + rule.getName())
           >= 0) {
     potentialSuperior.addDefeated(potentialInferior);
     return true;
   }
   return false;
 }