Search in sources :

Example 1 with FunctionError

use of org.drools.compiler.compiler.FunctionError in project drools by kiegroup.

the class JavaFunctionBuilder method build.

/* (non-Javadoc)
     * @see org.kie.rule.builder.dialect.java.JavaFunctionBuilder#build(org.kie.rule.Package, org.kie.lang.descr.FunctionDescr, org.codehaus.jfdi.interpreter.TypeResolver, java.util.Map)
     */
public String build(final InternalKnowledgePackage pkg, final FunctionDescr functionDescr, final TypeResolver typeResolver, final Map<String, LineMappings> lineMappings, final List<KnowledgeBuilderResult> errors) {
    final Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("package", pkg.getName());
    vars.put("imports", pkg.getImports().keySet());
    final List<String> staticImports = new LinkedList<String>();
    for (String staticImport : pkg.getStaticImports()) {
        if (!staticImport.endsWith(functionDescr.getName())) {
            staticImports.add(staticImport);
        }
    }
    vars.put("staticImports", staticImports);
    vars.put("className", StringUtils.ucFirst(functionDescr.getName()));
    vars.put("methodName", functionDescr.getName());
    vars.put("returnType", functionDescr.getReturnType());
    vars.put("parameterTypes", functionDescr.getParameterTypes());
    vars.put("parameterNames", functionDescr.getParameterNames());
    vars.put("hashCode", functionDescr.getText().hashCode());
    // Check that all the parameters are resolvable
    final List<String> names = functionDescr.getParameterNames();
    final List<String> types = functionDescr.getParameterTypes();
    for (int i = 0, size = names.size(); i < size; i++) {
        try {
            typeResolver.resolveType(types.get(i));
        } catch (final ClassNotFoundException e) {
            errors.add(new FunctionError(functionDescr, e, "Unable to resolve type " + types.get(i) + " while building function."));
            break;
        }
    }
    vars.put("text", functionDescr.getText());
    final String text = String.valueOf(TemplateRuntime.eval(template, null, new MapVariableResolverFactory(vars)));
    final BufferedReader reader = new BufferedReader(new StringReader(text));
    final String lineStartsWith = "    public static " + functionDescr.getReturnType() + " " + functionDescr.getName();
    try {
        String line;
        int offset = 0;
        while ((line = reader.readLine()) != null) {
            offset++;
            if (line.startsWith(lineStartsWith)) {
                break;
            }
        }
        functionDescr.setOffset(offset);
    } catch (final IOException e) {
        // won't ever happen, it's just reading over a string.
        throw new RuntimeException("Error determining start offset with function");
    }
    final String name = pkg.getName() + "." + StringUtils.ucFirst(functionDescr.getName());
    final LineMappings mapping = new LineMappings(name);
    mapping.setStartLine(functionDescr.getLine());
    mapping.setOffset(functionDescr.getOffset());
    lineMappings.put(name, mapping);
    return text;
}
Also used : HashMap(java.util.HashMap) FunctionError(org.drools.compiler.compiler.FunctionError) IOException(java.io.IOException) LinkedList(java.util.LinkedList) LineMappings(org.drools.core.rule.LineMappings) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Example 2 with FunctionError

use of org.drools.compiler.compiler.FunctionError in project drools by kiegroup.

the class JavaFunctionBuilder method build.

/* (non-Javadoc)
     * @see org.kie.rule.builder.dialect.java.JavaFunctionBuilder#build(org.kie.rule.Package, org.kie.lang.descr.FunctionDescr, org.codehaus.jfdi.interpreter.TypeResolver, java.util.Map)
     */
public String build(final InternalKnowledgePackage pkg, final FunctionDescr functionDescr, final TypeResolver typeResolver, final Map<String, LineMappings> lineMappings, final List<KnowledgeBuilderResult> errors) {
    final Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("package", pkg.getName());
    vars.put("imports", pkg.getImports().keySet());
    final List<String> staticImports = new ArrayList<String>();
    for (String staticImport : pkg.getStaticImports()) {
        if (!staticImport.endsWith(functionDescr.getName())) {
            staticImports.add(staticImport);
        }
    }
    vars.put("staticImports", staticImports);
    vars.put("className", StringUtils.ucFirst(functionDescr.getName()));
    vars.put("methodName", functionDescr.getName());
    vars.put("returnType", functionDescr.getReturnType());
    vars.put("parameterTypes", functionDescr.getParameterTypes());
    vars.put("parameterNames", functionDescr.getParameterNames());
    vars.put("hashCode", functionDescr.getText().hashCode());
    // Check that all the parameters are resolvable
    final List<String> names = functionDescr.getParameterNames();
    final List<String> types = functionDescr.getParameterTypes();
    for (int i = 0, size = names.size(); i < size; i++) {
        try {
            typeResolver.resolveType(types.get(i));
        } catch (final ClassNotFoundException e) {
            errors.add(new FunctionError(functionDescr, e, "Unable to resolve type " + types.get(i) + " while building function."));
            break;
        }
    }
    vars.put("text", functionDescr.getText());
    final String text = String.valueOf(TemplateRuntime.eval(template, null, new MapVariableResolverFactory(vars)));
    final BufferedReader reader = new BufferedReader(new StringReader(text));
    final String lineStartsWith = "    public static " + functionDescr.getReturnType() + " " + functionDescr.getName();
    try {
        String line;
        int offset = 0;
        while ((line = reader.readLine()) != null) {
            offset++;
            if (line.startsWith(lineStartsWith)) {
                break;
            }
        }
        functionDescr.setOffset(offset);
    } catch (final IOException e) {
        // won't ever happen, it's just reading over a string.
        throw new RuntimeException("Error determining start offset with function");
    }
    final String name = pkg.getName() + "." + StringUtils.ucFirst(functionDescr.getName());
    final LineMappings mapping = new LineMappings(name);
    mapping.setStartLine(functionDescr.getLine());
    mapping.setOffset(functionDescr.getOffset());
    lineMappings.put(name, mapping);
    return text;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FunctionError(org.drools.compiler.compiler.FunctionError) IOException(java.io.IOException) LineMappings(org.drools.core.rule.LineMappings) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

Aggregations

BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 FunctionError (org.drools.compiler.compiler.FunctionError)2 LineMappings (org.drools.core.rule.LineMappings)2 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1