Пример #1
0
    private void generateSignatures(AnnotationInfo info) throws IOException {
      write("\\begin{description}");
      for (SignatureInfo sig : info.signatureInfos) {
        write("\n\\item [$" + info.name + ":$ ] $");
        String delim = "";
        int correction = 0;
        for (int i = 0; i < sig.signature.parameterTypes.length; i++) {
          if (sig.signature.parameterTypes[i] == InternalGreqlEvaluator.class) {
            correction++;
            continue;
          }
          write(delim);

          write(Types.getGreqlTypeName(sig.signature.parameterTypes[i]));
          write("\\; ");
          write(sig.params[i - correction]);
          delim = " \\times ";
        }
        write(" \\longrightarrow ");
        write(Types.getGreqlTypeName(sig.signature.evaluateMethod.getReturnType()));
        write("$");
        if (sig.description != null) {
          write("\\\\\n\t");
          write(sig.description);
        }
      }
      write("\n\\end{description}");
    }
Пример #2
0
 public static final Object apply(FunctionInfo fi, Object... args) {
   assert fi != null;
   if (!fi.acceptsUndefinedValues) {
     for (Object arg : args) {
       if ((arg == null) || (arg == Undefined.UNDEFINED)) {
         return Undefined.UNDEFINED;
       }
     }
   }
   for (Signature sig : fi.signatures) {
     if (sig.matches(args)) {
       try {
         Object result = sig.evaluateMethod.invoke(fi.function, args);
         assert Types.isValidGreqlValue(result);
         return result == null ? Undefined.UNDEFINED : result;
       } catch (IllegalArgumentException e) {
         if (e.getCause() instanceof GreqlException) {
           throw (GreqlException) e.getCause();
         } else {
           throw new GreqlException(
               "When applying function " + fi.name + ": " + e.getMessage(), e.getCause());
         }
       } catch (IllegalAccessException e) {
         if (e.getCause() instanceof GreqlException) {
           throw (GreqlException) e.getCause();
         } else {
           throw new GreqlException(
               "When applying function " + fi.name + ": " + e.getMessage(), e.getCause());
         }
       } catch (InvocationTargetException e) {
         if (e.getCause() instanceof GreqlException) {
           throw (GreqlException) e.getCause();
         } else {
           throw new GreqlException(
               "When applying function " + fi.name + ": " + e.getMessage(), e.getCause());
         }
       }
     }
   }
   StringBuilder sb = new StringBuilder();
   sb.append("Function '").append(fi.name).append("' not defined for argument types (");
   String delim = "";
   for (Object arg : args) {
     sb.append(delim).append(Types.getGreqlTypeName(arg));
     delim = ", ";
   }
   sb.append(")");
   throw new GreqlException(sb.toString());
 }
Пример #3
0
 private static final boolean validArgumentTypes(Object[] args) {
   for (Object arg : args) {
     if (!Types.isValidGreqlValue(arg)) {
       throw new GreqlException(
           "Type unknown to GReQL: " + arg.getClass().getName() + ", value: " + arg);
     }
   }
   return true;
 }
Пример #4
0
 public static final String getArgumentAsString(Object arg) {
   if (arg == null) {
     arg = Undefined.UNDEFINED;
   }
   StringBuilder sb = new StringBuilder();
   sb.append(Types.getGreqlTypeName(arg));
   if (arg instanceof String) {
     sb.append(": ").append('"').append(arg.toString().replace("\"", "\\\"")).append('"');
   } else if (!(arg instanceof Graph) && !(arg instanceof Undefined)) {
     sb.append(": ").append(arg);
   }
   return sb.toString();
 }
Пример #5
0
    public final String getHtmlDescription() {
      StringBuilder sb = new StringBuilder();
      sb.append("<html><body><p>GReQL function <font color=\"blue\"><strong>")
          .append(name)
          .append("</strong></font></p><dl>");
      assert (functionClass.getConstructors().length == 1);
      Constructor<?> cons = functionClass.getConstructors()[0];

      Description consDesc = cons.getAnnotation(Description.class);

      for (Signature sig : signatures) {
        Description funDesc = sig.evaluateMethod.getAnnotation(Description.class);
        if (funDesc == null) {
          funDesc = consDesc;
        }
        if (funDesc == null) {
          continue;
        }
        Class<?> ret = sig.evaluateMethod.getReturnType();
        String returnType = Types.getGreqlTypeName(ret);

        boolean acceptsType =
            sig.parameterTypes[sig.parameterTypes.length - 1] == TypeCollection.class;
        sb.append("<dt><strong><font color=\"purple\">")
            .append(returnType)
            .append(" <font color=\"blue\">")
            .append(name)
            .append("</font></strong>");
        if (acceptsType) {
          sb.append(" { <font color=\"#008000\">types...</font> } ");
        }
        sb.append("(");
        String delim = "";
        int i = 0;
        for (String p : funDesc.params()) {
          if ((i == 0) && needsGraphArgument) {
            // don't show Graph argument
            ++i;
            continue;
          }
          if ((i == 0) && needsEvaluatorArgument) {
            // don't show evaluator argument
            ++i;
            continue;
          }
          if ((i == (sig.parameterTypes.length - 1)) && acceptsType) {
            // don't show TypeCollection argument
            ++i;
            continue;
          }
          Class<?> cls = sig.parameterTypes[i++];
          String type = Types.getGreqlTypeName(cls);
          sb.append(delim)
              .append("<strong><font color=\"purple\">")
              .append(type)
              .append("</font></strong> ")
              .append(p);
          delim = ", ";
        }
        sb.append(")</dt><dd>").append(funDesc.description()).append("</dd>");
      }
      sb.append("</dl></body></html>");
      return sb.toString();
    }