Exemplo n.º 1
0
  @Predicate(
      Signature = "str_len/2",
      Template = {"+atom,?integer"},
      Reference = "Get string length.")
  @Determined
  public static final boolean predicateSTRLEN(final Goal goal, final TermStruct predicate) {
    final Term argLeft = Utils.getTermFromElement(predicate.getElement(0));
    final Term argRight = Utils.getTermFromElement(predicate.getElement(1));

    TermInteger result = new TermInteger(argLeft.getText().length());

    return argRight.Equ(result);
  }
Exemplo n.º 2
0
  @Predicate(
      Signature = "str_trim/2",
      Template = {"+atom,?atom"},
      Reference = "Trim string.")
  @Determined
  public static final boolean predicateSTRTRIM(final Goal goal, final TermStruct predicate) {
    final Term argLeft = Utils.getTermFromElement(predicate.getElement(0));
    final Term argRight = Utils.getTermFromElement(predicate.getElement(1));

    Term result = new Term(argLeft.getText().trim());

    return argRight.Equ(result);
  }
Exemplo n.º 3
0
  @Predicate(
      Signature = "upper_lower/2",
      Template = {"+atom,?atom", "?atom,+atom"},
      Reference = "Allows to make upper or lower case text version of an atom.")
  @Determined
  public static final boolean predicateUPPERLOWER(final Goal goal, final TermStruct predicate) {
    final Term argLeft = Utils.getTermFromElement(predicate.getElement(0));
    final Term argRight = Utils.getTermFromElement(predicate.getElement(1));

    if (argLeft.getTermType() == Term.TYPE_ATOM) {
      // the first case
      Term term = new Term(argLeft.getText().toLowerCase());
      return argRight.Equ(term);
    } else {
      // the second case
      Term term = new Term(argRight.getText().toUpperCase());
      return argLeft.Equ(term);
    }
  }
Exemplo n.º 4
0
  @Predicate(
      Signature = "concat/3",
      Template = {"+atom,+atom,?atom", "+atom,?atom,+atom", "?atom,+atom,+atom"},
      Reference = "Concat two strings.")
  @Determined
  public static final boolean predicateCONCAT(final Goal goal, final TermStruct predicate) {
    final Term argFIRST = Utils.getTermFromElement(predicate.getElement(0));
    final Term argSECOND = Utils.getTermFromElement(predicate.getElement(1));
    final Term argTHIRD = Utils.getTermFromElement(predicate.getElement(2));

    if (argFIRST.getTermType() == Term.TYPE_ATOM) {
      if (argSECOND.getTermType() == Term.TYPE_ATOM) {
        // the first case
        StringBuilder bldr = new StringBuilder(argFIRST.getText());
        bldr.append(argSECOND.getText());
        Term term = new Term(bldr.toString());
        return argTHIRD.Equ(term);
      } else {
        // the second case
        String startText = argFIRST.getText();
        String fullText = argTHIRD.getText();
        if (startText.length() > fullText.length()) return false;
        String endText = fullText.substring(startText.length());
        Term second = new Term(endText);
        return argSECOND.Equ(second);
      }
    } else {
      // the third case
      String endText = argSECOND.getText();
      String fullText = argTHIRD.getText();
      if (endText.length() > fullText.length()) return false;
      String startText = fullText.substring(0, fullText.length() - endText.length());
      Term first = new Term(startText);
      return argFIRST.Equ(first);
    }
  }
Exemplo n.º 5
0
  @Predicate(
      Signature = "str_real/2",
      Template = {"+atom,?number", "?atom,+number"},
      Reference = "Convert a text atom to a real numeric atom (or back).")
  @Determined
  public static final boolean predicateSTRREAL(final Goal goal, final TermStruct predicate) {
    final Term argLeft = Utils.getTermFromElement(predicate.getElement(0));
    final Term argRight = Utils.getTermFromElement(predicate.getElement(1));

    if (argLeft.getTermType() == Term.TYPE_ATOM) {
      // the first case
      TermFloat result = null;
      try {
        result = new TermFloat(argLeft.getText().trim());
      } catch (NumberFormatException ex) {
        return false;
      }
      return argRight.Equ(result);
    } else {
      // the second case
      Term result = new Term(argRight.getText());
      return argLeft.Equ(result);
    }
  }