コード例 #1
0
  /**
   * Returns true if it was cleaned, or false if the clause was unsatisfiable
   *
   * @param sample
   * @return
   */
  public static ValueSet<NodeValue> toValueSet(Sample<NodeValue> sample) {
    if (sample.getPositives().size() > 1
        || (!sample.getPositives().isEmpty()
            && sample.getNegatives().containsAll(sample.getPositives()))) {
      /**
       * If there are .) multiple positive constants in a clause, such as in [?x = a, ?x = b] .) or
       * the negatives contain the positive, such as in [?x = a, !(?x = a)] then the clause is
       * unsatisfiable and no value contraint can be derived
       */
      return ValueSet.create();
    }

    if (!sample.getPositives().isEmpty()) {
      /**
       * Just in case deal with the case [?x = a, !(?x = b)] ---> [?x = a]
       *
       * <p>So positive takes precedence over negative
       */
      sample.getNegatives().clear();
    }

    return sample.getPositives().isEmpty()
        ? ValueSet.create(sample.getNegatives(), false)
        : ValueSet.create(sample.getPositives(), true);
  }
コード例 #2
0
  public static Map<Var, ValueSet<NodeValue>> extractValueConstraintsDnfClause(
      Set<Expr> dnfClause) {
    Map<Var, Sample<NodeValue>> tmp = new HashMap<Var, Sample<NodeValue>>();

    for (Expr expr : dnfClause) {

      boolean isNegative = (expr instanceof E_LogicalNot);
      if (isNegative) {
        expr = ((E_LogicalNot) expr).getArg();
      }

      if (!(expr instanceof E_Equals)) {
        continue;
      }

      Pair<Var, NodeValue> constraint = extractValueConstraint(expr);

      if (constraint == null) {
        continue;
      }

      Sample<NodeValue> sample = tmp.get(constraint.getKey());
      if (sample == null) {
        sample = Sample.create();
        tmp.put(constraint.getKey(), sample);
      }

      if (isNegative) {
        sample.getNegatives().add(constraint.getValue());
      } else {
        sample.getPositives().add(constraint.getValue());
      }
    }

    Map<Var, ValueSet<NodeValue>> result = new HashMap<Var, ValueSet<NodeValue>>();

    // Phase 2: Create the value sets
    for (Map.Entry<Var, Sample<NodeValue>> entry : tmp.entrySet()) {
      ValueSet<NodeValue> valueSet = toValueSet(entry.getValue());

      if (valueSet.isUnknown()) {
        continue;
      }

      result.put(entry.getKey(), valueSet);
    }

    return result;
  }