private void check(ITerm listOne, ITerm expectedResult) throws EvaluationException {
    builtin = new DistinctValuesBuiltin(listOne);

    ITuple arguments = BASIC.createTuple(X, Y, Z);

    ITuple expectedTuple = BASIC.createTuple(expectedResult);

    ITuple actualTuple = builtin.evaluate(arguments);

    assertEquals(expectedTuple, actualTuple);
  }
/**
 * Represents a string substring operation, but restricts the endIndex to <code>string.length - 1
 * </code>.
 *
 * @author gigi
 * @author Adrian Marte
 */
public class StringSubstringUntilEndBuiltin extends FunctionalBuiltin {

  private static final IPredicate PREDICATE = BASIC.createPredicate("STRING_SUBSTRING2", 3);

  /**
   * Constructor.
   *
   * @param terms The terms, where the first term is the string, the second term is the begin index
   *     and the third term represents the result.
   * @throws IllegalArgumentException if one of the terms is {@code null}
   */
  public StringSubstringUntilEndBuiltin(ITerm... terms) {
    super(PREDICATE, terms);
  }

  protected ITerm computeResult(ITerm[] terms) throws EvaluationException {
    if (terms[0] instanceof IStringTerm && terms[1] instanceof IIntegerTerm) {
      IStringTerm string = (IStringTerm) terms[0];
      IIntegerTerm beginIndex = (IIntegerTerm) terms[1];

      String substring = string.getValue().substring(beginIndex.getValue().intValue());

      return Factory.TERM.createString(substring);
    }

    return null;
  }
}
/**
 * Represents the RIF built-in func:contains as described in
 * http://www.w3.org/TR/xpath-functions/#func-contains. Restricts the value of collation to Unicode
 * code point collation (http://www.w3.org/2005/xpath-functions/collation/codepoint).
 */
public class StringContainsWithoutCollationBuiltin extends BooleanBuiltin {

  private static final IPredicate PREDICATE = BASIC.createPredicate("STRING_CONTAINS2", 2);

  /**
   * Constructor.
   *
   * @param terms The terms, where the term at the first position is the <code>haystack</code> and
   *     the term at the second position is the <code>needle</code>. The <code>haystack</code> is
   *     the string being searched for the occurrence of the <code>needle</code>. The <code>needle
   *     </code> is the string to be searched for in the <code>haystack</code>.
   * @throws IllegalArgumentException if one of the terms is {@code null}
   */
  public StringContainsWithoutCollationBuiltin(ITerm... terms) {
    super(PREDICATE, terms);
  }

  @Override
  protected boolean computeResult(ITerm[] terms) {
    String haystack = null;
    String needle = null;

    if (terms[0] instanceof IStringTerm && terms[1] instanceof StringTerm) {
      haystack = ((IStringTerm) terms[0]).getValue();
      needle = ((IStringTerm) terms[1]).getValue();
    } else {
      return false;
    }

    return StringContainsBuiltin.contains(haystack, needle, null);
  }
}
  ILiteral createLiteral(boolean positive, String predicateName, Object... termObjects) {
    List<ITerm> terms = new ArrayList<ITerm>();

    for (Object o : termObjects) {
      if (o instanceof Integer) terms.add(CONCRETE.createInteger((Integer) o));
      else if (o instanceof String) terms.add(TERM.createVariable((String) o));
    }

    if (predicateName.equals("="))
      return BASIC.createLiteral(positive, new EqualBuiltin(terms.toArray(new ITerm[2])));

    return BASIC.createLiteral(
        positive,
        BASIC.createAtom(
            BASIC.createPredicate(predicateName, terms.size()), BASIC.createTuple(terms)));
  }
/** Represents a string compare operation. */
public class StringCompareBuiltin extends FunctionalBuiltin {

  /** The predicate defining this built-in. */
  private static final IPredicate PREDICATE = BASIC.createPredicate("STRING_COMPARE", 3);

  /**
   * Constructor. Three terms must be passed to the constructor, otherwise an exception will be
   * thrown.
   *
   * @param terms The terms.
   * @throws IllegalArgumentException If one of the terms is {@code null}.
   * @throws IllegalArgumentException If the number of terms submitted is not 3.
   * @throws IllegalArgumentException If terms is <code>null</code>.
   */
  public StringCompareBuiltin(ITerm... terms) {
    super(PREDICATE, terms);
  }

  protected ITerm computeResult(ITerm[] terms) throws EvaluationException {
    if (terms[0] instanceof IStringTerm && terms[1] instanceof IStringTerm) {
      int result = BuiltinHelper.compare(terms[0], terms[1]);
      return Factory.CONCRETE.createInteger(result);
    }

    return null;
  }
}
  private ITuple compute(ITerm term) throws EvaluationException {
    ToDayTimeDurationBuiltin builtin = new ToDayTimeDurationBuiltin(term, Y);

    ITuple arguments = BASIC.createTuple(X, Y);
    ITuple actualTuple = builtin.evaluate(arguments);

    return actualTuple;
  }
  IRule createRule(ILiteral head, ILiteral... body) {
    List<ILiteral> h = new ArrayList<ILiteral>();
    h.add(head);

    List<ILiteral> b = new ArrayList<ILiteral>();
    for (ILiteral l : body) b.add(l);

    return BASIC.createRule(h, b);
  }
/** Represents the RIF built-in predicate pred:date-less-than. */
public class DateLessBuiltin extends LessBuiltin {

  /** The predicate defining this built-in. */
  private static final IPredicate PREDICATE = BASIC.createPredicate("DATE_LESS", 2);

  public DateLessBuiltin(ITerm... terms) {
    super(PREDICATE, terms);
  }

  @Override
  protected boolean computeResult(ITerm[] terms) {
    if (terms[0] instanceof IDateTerm && terms[1] instanceof IDateTerm) {
      return super.computeResult(terms);
    }

    return false;
  }
}
/**
 * Checks if a term is not of type 'Name'.
 *
 * @author Adrian Marte
 */
public class IsNotNameBuiltin extends BooleanBuiltin {

  /** The predicate defining this built-in. */
  public static final IPredicate PREDICATE = BASIC.createPredicate("IS_NOT_NAME", 1);

  /**
   * Constructor.
   *
   * @param terms The list of terms. Must always be of length 1 in this case.
   */
  public IsNotNameBuiltin(ITerm... terms) {
    super(PREDICATE, terms);
  }

  protected boolean computeResult(ITerm[] terms) {
    return !IsNameBuiltin.isName(terms[0]);
  }
}
/**
 * Checks if a term is of type 'NegativeInteger'.
 *
 * @author Adrian Marte
 */
public class IsNegativeIntegerBuiltin extends BooleanBuiltin {

  /** The predicate defining this built-in. */
  public static final IPredicate PREDICATE = BASIC.createPredicate("IS_NEGATIVEINTEGER", 1);

  /**
   * Constructor.
   *
   * @param terms The list of terms. Must always be of length 1 in this case.
   */
  public IsNegativeIntegerBuiltin(ITerm... terms) {
    super(PREDICATE, terms);
  }

  protected boolean computeResult(ITerm[] terms) {
    return isNegativeInteger(terms[0]);
  }

  public static boolean isNegativeInteger(ITerm term) {
    return term instanceof INegativeInteger;
  }
}
示例#11
0
/**
 * Represents a data type conversion function, which converts supported data type instances to
 * String instances. According to the XPath casting table, all data types are supported to be
 * converted to String.
 */
public class ToStringBuiltin extends ConversionBuiltin {

  private static final IPredicate PREDICATE = BASIC.createPredicate("TO_STRING", 2);

  /**
   * Creates a new instance of this builtin.
   *
   * @param terms The term representing the data type instance to be converted.
   */
  public ToStringBuiltin(ITerm... terms) {
    super(PREDICATE, terms);
  }

  @Override
  protected ITerm convert(ITerm term) {
    if (term instanceof IConcreteTerm) {
      return toString(term);
    }

    return null;
  }

  /**
   * Converts a XMLLiteral term to a String term. The language tag of the XMLLiteral term is
   * omitted.
   *
   * @param term The XMLLiteral term to be converted.
   * @return A new String term representing the result of the conversion.
   */
  public static IStringTerm toString(IXMLLiteral term) {
    String string = term.getString();
    return TERM.createString(string);
  }

  /**
   * Converts a PlainLiteral term to a String term. The language tag of the PlainLiteral term is
   * omitted.
   *
   * @param term The PlainLiteral term to be converted.
   * @return A new String term representing the result of the conversion.
   */
  public static IStringTerm toString(IPlainLiteral term) {
    String string = term.getString();
    return TERM.createString(string);
  }

  /**
   * Converts a constant term to a String term. The <code>toCanonicalString</code> method of the
   * given term is used to convert to String term.
   *
   * @param term The term to be converted.
   * @return A new String term representing the result of the conversion, or <code>null</code> if
   *     the data type represented by the given term is not supported.
   */
  public static IStringTerm toString(ITerm term) {
    if (term instanceof IConcreteTerm) {
      if (term instanceof IPlainLiteral) {
        return toString((IPlainLiteral) term);
      } else if (term instanceof IStringTerm) {
        return (IStringTerm) term;
      } else if (term instanceof IXMLLiteral) {
        return toString((IXMLLiteral) term);
      }

      String string = ((IConcreteTerm) term).toCanonicalString();
      return TERM.createString(string);
    }

    return null;
  }
}
  private void equals(IDayTimeDuration expected, ITerm term) throws EvaluationException {
    ITuple expectedTuple = BASIC.createTuple(expected);
    ITuple actualTuple = compute(term);

    assertEquals(expectedTuple, actualTuple);
  }