Beispiel #1
0
 /**
  * Checks a binary predicate for a type matching the type parameters.
  *
  * @param pred a predicate to check.
  * @param type1 the proposed type of parameter 1.
  * @param type2 the proposed type of parameter 2.
  * @return true if <code>pred</code> is a binary predicate that matches the types.
  */
 protected static String checkTypes(
     Predicate pred, Predicate.ParamType type1, Predicate.ParamType type2) {
   if (pred.getArity() != 2) return formatError(pred, "expected 2 arguments");
   else if (pred.getParamType(0) != type1)
     return formatError(pred, "argument 1 should be type " + type1);
   else if (pred.getParamType(1) != type2)
     return formatError(pred, "argument 2 should be type " + type2);
   return null;
 }
Beispiel #2
0
  /**
   * Attempts to evalutate or assert the predicate by searching for and executing a method named
   * "a_[PredicateName]" (where [PredicateName] is the name of the Predicate object <code>pred
   * </code>) and that has a parameter type list that matches the predicate's parameter type list.
   *
   * @param isAssert true if this is to be interpreted as an assertion, false if it's to be treated
   *     as an evaluation.
   * @param pred a predicate object to assert or evaluate.
   * @return the value returned by the method described above; false if something when wrong.
   */
  private boolean assert_or_eval(boolean isAssert, Predicate pred) {
    currentPred = pred;
    String predName = pred.getName();
    if (predName == null || predName.length() == 0) return false;
    String methodName = (isAssert ? "a_" : "e_") + predName.replaceAll("-", "_");

    int arity = pred.getArity();
    Class<?>[] paramTypes = new Class[arity];
    Object[] paramValues = new Object[arity];
    for (int i = 0; i < arity; i++) {
      switch ((Predicate.ParamType) pred.getParamType(i)) {
        case STRING:
          paramTypes[i] = String.class;
          paramValues[i] = pred.getStringParam(i);
          break;
        case SET:
          paramTypes[i] = TreeSet.class;
          paramValues[i] = pred.getSetParam(i);
          break;
        case LONG:
          paramTypes[i] = Long.class;
          paramValues[i] = pred.getLongParam(i);
          break;
        default:
          error(
              (isAssert ? "!" : "")
                  + pred.toString()
                  + ": Unrecognized argument type argument "
                  + i
                  + " in Predicate");
          return false;
      }
    }

    try {
      Method m = this.getClass().getMethod(methodName, paramTypes);
      Object ret = m.invoke(this, paramValues);
      if (ret instanceof Boolean) return ((Boolean) ret).booleanValue();
    } catch (InvocationTargetException e) {
      error(
          formatLineNumber()
              + (isAssert ? "!" : "")
              + pred.toString()
              + ": InvocationTargetException ("
              + e.getCause()
              + ")");
      e.getTargetException().printStackTrace();
    } catch (Exception e) {
      error(
          formatLineNumber()
              + (isAssert ? "!" : "")
              + pred.toString()
              + ": predicate not found or failed ("
              + e.toString()
              + ")");
    }

    return false;
  }