@Test
 public void test3() throws SymbolFormatException, GdlFormatException {
   Substitution s = null;
   Gdl a, b;
   a = GdlFactory.create("(role ?p)");
   b = GdlFactory.create("(role white)");
   s = Unification.unify((GdlSentence) a, (GdlSentence) b);
   assertEquals("test", s.toString(), "{?p=white}");
 }
 @Test
 public void test2() throws SymbolFormatException, GdlFormatException {
   Substitution s = null;
   Gdl a, b;
   a = GdlFactory.create("(fact ?x)");
   b = GdlFactory.create("(fact ?y)");
   s = Unification.unify((GdlSentence) a, (GdlSentence) b);
   assertEquals("test", s.toString(), "{?x=?y}");
 }
 @Test
 public void test4() throws SymbolFormatException, GdlFormatException {
   Substitution s = null;
   Gdl a, b;
   a = GdlFactory.create("(f1 ?x 3 ?y)");
   b = GdlFactory.create("(f1 (f2 ?y (f3 ?z (f4 (f5 ?y 42) 1 ?z) 7)) ?z a)");
   s = Unification.unify((GdlSentence) a, (GdlSentence) b);
   assertEquals(
       "test", s.toString(), "{?z=3, ?y=a, ?x=( f2 a ( f3 3 ( f4 ( f5 a 42 ) 1 3 ) 7 ) )}");
 }
 @Override
 public boolean equals(Object object) {
   if (this == object) {
     return true;
   }
   if (object instanceof Substitution) {
     Substitution substitution = (Substitution) object;
     return substitution.getKey().equals(this.getKey())
         && substitution.getValue().equals(this.getValue());
   }
   return false;
 }
 @Override
 protected String computeKey() {
   Fingerprint f = new Fingerprint();
   f.addString(GUID);
   f.addString(String.valueOf(makeExecutable));
   f.addString(template.getKey());
   f.addInt(substitutions.size());
   for (Substitution entry : substitutions) {
     f.addString(entry.getKey());
     f.addString(entry.getValue());
   }
   return f.hexDigestAndReset();
 }
Esempio n. 6
0
  private String transform(String input, List<Substitution> substitutions) {
    List<String> tokens = tokenizer.tokenize(input);
    outer:
    for (int i = 0; i < tokens.size(); ) {
      int offset = i;
      for (final Substitution substitution : substitutions) {
        i = substitution.substitute(offset, tokens);
        if (i > offset) continue outer;
      }

      // Only gets here if no substitution matches.
      i++;
    }

    return tokenizer.toString(tokens);
  }
Esempio n. 7
0
  public String substitute(Substitution substitution) {
    MatchResult matchResult = new JdkMatchResult(internalMatcher);

    StringBuffer buffer = new StringBuffer();
    while (internalMatcher.find()) {
      internalMatcher.appendReplacement(buffer, "");
      substitution.handleMatch(buffer, matchResult);
    }
    internalMatcher.appendTail(buffer);
    return buffer.toString();
  }
  private Clause resolve(Clause mainPremise, Clause sidePremise, int mainPremiseAtomIndex) {

    // Rename the variables of the side premise
    int numberOfVariablesMainPremise = mainPremise.getVariables().size();
    Clause sidePremiseRenamed =
        sidePremise.renameVariables(
            this.m_saturator.getTermFactory(), numberOfVariablesMainPremise);

    Term mainAtom = mainPremise.getBody()[mainPremiseAtomIndex];
    Term sideAtom = sidePremiseRenamed.getHead();

    Substitution unifier =
        Substitution.mostGeneralUnifier(mainAtom, sideAtom, this.m_saturator.getTermFactory());

    if (unifier == null) return null;
    Set<Term> newBody = new LinkedHashSet<Term>();
    // Copy the atoms from the main premise
    for (int index = 0; index < mainPremise.getBody().length; index++) {
      if (index != mainPremiseAtomIndex) {
        Term newBodyAtom =
            mainPremise.getBody()[index].apply(unifier, this.m_saturator.getTermFactory());
        ((FunctionalTerm) newBodyAtom).originIndex =
            ((FunctionalTerm) mainPremise.getBody()[index]).originIndex;
        newBody.add(newBodyAtom);
      }
    }
    // Copy the atoms from the side premise
    for (int index = 0; index < sidePremiseRenamed.getBody().length; index++) {
      Term newBodyAtom =
          sidePremiseRenamed.getBody()[index].apply(unifier, this.m_saturator.getTermFactory());
      ((FunctionalTerm) newBodyAtom).originIndex = ((FunctionalTerm) mainAtom).originIndex;
      newBody.add(newBodyAtom);
    }

    // New body and head
    Term[] body = new Term[newBody.size()];
    newBody.toArray(body);
    Term head = mainPremise.getHead().apply(unifier, this.m_saturator.getTermFactory());

    Clause resolvent = new Clause(body, head);

    // Rename variables in resolvent
    ArrayList<Variable> variablesResolvent = resolvent.getVariables();
    HashMap<Variable, Integer> variableMapping = new HashMap<Variable, Integer>();
    for (int i = 0; i < variablesResolvent.size(); i++) {
      variableMapping.put(variablesResolvent.get(i), i);
    }
    Clause resolventRenamed =
        resolvent.renameVariables(this.m_saturator.getTermFactory(), variableMapping);

    return resolventRenamed;
  }
Esempio n. 9
0
  /**
   * * Enforces all equalities in the query, that is, for every equivalence class (among variables)
   * defined by a set of equalities, it chooses one representative variable and replaces all other
   * variables in the equivalence class with the representative variable. For example, if the query
   * body is R(x,y,z), x=y, y=z. It will choose x and produce the following body R(x,x,x).
   *
   * <p>We ignore the equalities with disjunctions. For example R(x,y,z), x=y OR y=z Note the
   * process will also remove from the body all the equalities that are here processed.
   *
   * @param result
   */
  public static void enforceEqualities(CQIE result) {

    List<Function> body = result.getBody();
    Substitution mgu = new SubstitutionImpl();

    // collecting all equalities as substitutions

    for (int i = 0; i < body.size(); i++) {
      Function atom = body.get(i);
      SubstitutionUtilities.applySubstitution(atom, mgu);

      if (atom.getFunctionSymbol() == ExpressionOperation.EQ) {
        if (!mgu.composeTerms(atom.getTerm(0), atom.getTerm(1))) continue;

        body.remove(i);
        i--;
      }
      // search for nested equalities in AND function
      else if (atom.getFunctionSymbol() == ExpressionOperation.AND) {
        nestedEQSubstitutions(atom, mgu);

        // we remove the function if empty because all its terms were equalities
        if (atom.getTerms().isEmpty()) {
          body.remove(i);
          i--;
        } else {
          // if there is only a term left we remove the conjunction
          if (atom.getTerms().size() == 1) {
            body.set(i, (Function) atom.getTerm(0));
          }
        }
      }
    }

    SubstitutionUtilities.applySubstitution(result, mgu, false);
  }
Esempio n. 10
0
  /**
   * We search for equalities in conjunctions. This recursive methods explore AND functions and
   * removes EQ functions, substituting the values using the class {@link
   * it.unibz.krdb.obda.owlrefplatform.core.basicoperations.Substitution#composeTerms(it.unibz.krdb.obda.model.Term,
   * it.unibz.krdb.obda.model.Term)}
   *
   * @param atom the atom that can contain equalities
   * @param mgu mapping between a variable and a term
   */
  private static void nestedEQSubstitutions(Function atom, Substitution mgu) {

    List<Term> terms = atom.getTerms();
    for (int i = 0; i < terms.size(); i++) {
      Term t = terms.get(i);

      if (t instanceof Function) {
        Function t2 = (Function) t;
        SubstitutionUtilities.applySubstitution(t2, mgu);

        // in case of equalities do the substitution and remove the term
        if (t2.getFunctionSymbol() == ExpressionOperation.EQ) {
          if (!mgu.composeTerms(t2.getTerm(0), t2.getTerm(1))) continue;

          terms.remove(i);
          i -= 1;
        }
        // consider the case of  AND function. Calls recursive method to consider nested equalities
        else {
          if (t2.getFunctionSymbol() == ExpressionOperation.AND) {
            nestedEQSubstitutions(t2, mgu);

            // we remove the function if empty because all its terms were equalities
            if (t2.getTerms().isEmpty()) {
              terms.remove(i);
              i--;
            } else {
              // if there is only a term left we remove the conjunction
              // we remove and function and we set  atom equals to the term that remained
              if (t2.getTerms().size() == 1) {
                atom.setTerm(i, t2.getTerm(0));
              }
            }
          }
        }
      }
    }
  }
 /**
  * Expands the template by applying all substitutions.
  *
  * @param template
  * @return the expanded text.
  */
 private String expandTemplate(String template) {
   for (Substitution entry : substitutions) {
     template = StringUtilities.replaceAllLiteral(template, entry.getKey(), entry.getValue());
   }
   return template;
 }