Search in sources :

Example 1 with LineMappings

use of org.drools.core.rule.LineMappings in project drools by kiegroup.

the class JavaDialect method addRule.

/**
 * This will add the rule for compiling later on.
 * It will not actually call the compiler
 */
public void addRule(final RuleBuildContext context) {
    final RuleImpl rule = context.getRule();
    final RuleDescr ruleDescr = context.getRuleDescr();
    RuleClassBuilder classBuilder = context.getDialect().getRuleClassBuilder();
    String ruleClass = classBuilder.buildRule(context);
    // return if there is no ruleclass name;
    if (ruleClass == null) {
        return;
    }
    // The compilation result is for the entire rule, so difficult to associate with any descr
    addClassCompileTask(this.pkg.getName() + "." + ruleDescr.getClassName(), ruleDescr, ruleClass, this.src, new RuleErrorHandler(ruleDescr, rule, "Rule Compilation error"));
    JavaDialectRuntimeData data = (JavaDialectRuntimeData) this.pkg.getDialectRuntimeRegistry().getDialectData(ID);
    for (Map.Entry<String, String> invokers : context.getInvokers().entrySet()) {
        final String className = invokers.getKey();
        // Check if an invoker - returnvalue, predicate, eval or consequence has been associated
        // If so we add it to the PackageCompilationData as it will get wired up on compilation
        final Object invoker = context.getInvokerLookup(className);
        if (invoker instanceof Wireable) {
            data.putInvoker(className, (Wireable) invoker);
        }
        final String text = invokers.getValue();
        final BaseDescr descr = context.getDescrLookup(className);
        addClassCompileTask(className, descr, text, this.src, new RuleInvokerErrorHandler(descr, rule, "Unable to generate rule invoker."));
    }
    // setup the line mappins for this rule
    final String name = this.pkg.getName() + "." + StringUtils.ucFirst(ruleDescr.getClassName());
    final LineMappings mapping = new LineMappings(name);
    mapping.setStartLine(ruleDescr.getConsequenceLine());
    mapping.setOffset(ruleDescr.getConsequenceOffset());
    this.pkg.getDialectRuntimeRegistry().getLineMappings().put(name, mapping);
}
Also used : RuleInvokerErrorHandler(org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler) Wireable(org.drools.core.spi.Wireable) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) RuleClassBuilder(org.drools.compiler.rule.builder.RuleClassBuilder) LineMappings(org.drools.core.rule.LineMappings) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleErrorHandler(org.drools.compiler.builder.impl.errors.RuleErrorHandler) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with LineMappings

use of org.drools.core.rule.LineMappings in project drools by kiegroup.

the class JavaDialect method addFunction.

public void addFunction(final FunctionDescr functionDescr, final TypeResolver typeResolver, final Resource resource) {
    // logger.info( functionDescr + " : " + typeResolver );
    final String functionClassName = this.pkg.getName() + "." + StringUtils.ucFirst(functionDescr.getName());
    functionDescr.setClassName(functionClassName);
    this.pkg.addStaticImport(functionClassName + "." + functionDescr.getName());
    Function function = new Function(functionDescr.getNamespace(), functionDescr.getName(), ID);
    if (resource != null && ((InternalResource) resource).hasURL()) {
        function.setResource(resource);
    }
    this.pkg.addFunction(function);
    final String functionSrc = getFunctionBuilder().build(this.pkg, functionDescr, typeResolver, this.pkg.getDialectRuntimeRegistry().getLineMappings(), this.results);
    addClassCompileTask(functionClassName, functionDescr, functionSrc, this.src, new FunctionErrorHandler(functionDescr, "Function Compilation error"));
    final LineMappings mapping = new LineMappings(functionClassName);
    mapping.setStartLine(functionDescr.getLine());
    mapping.setOffset(functionDescr.getOffset());
    this.pkg.getDialectRuntimeRegistry().getLineMappings().put(functionClassName, mapping);
}
Also used : Function(org.drools.core.rule.Function) LineMappings(org.drools.core.rule.LineMappings) FunctionErrorHandler(org.drools.compiler.builder.impl.errors.FunctionErrorHandler)

Example 3 with LineMappings

use of org.drools.core.rule.LineMappings 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 4 with LineMappings

use of org.drools.core.rule.LineMappings in project drools by kiegroup.

the class MVELDialect method addRule.

public void addRule(RuleBuildContext context) {
    // MVEL: Compiler change
    final RuleDescr ruleDescr = context.getRuleDescr();
    // setup the line mappins for this rule
    final String name = this.pkg.getName() + "." + StringUtils.ucFirst(ruleDescr.getClassName());
    final LineMappings mapping = new LineMappings(name);
    mapping.setStartLine(ruleDescr.getConsequenceLine());
    mapping.setOffset(ruleDescr.getConsequenceOffset());
    context.getPkg().getDialectRuntimeRegistry().getLineMappings().put(name, mapping);
}
Also used : LineMappings(org.drools.core.rule.LineMappings) RuleDescr(org.drools.compiler.lang.descr.RuleDescr)

Aggregations

LineMappings (org.drools.core.rule.LineMappings)4 HashMap (java.util.HashMap)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 FunctionErrorHandler (org.drools.compiler.builder.impl.errors.FunctionErrorHandler)1 RuleErrorHandler (org.drools.compiler.builder.impl.errors.RuleErrorHandler)1 RuleInvokerErrorHandler (org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler)1 FunctionError (org.drools.compiler.compiler.FunctionError)1 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)1 RuleClassBuilder (org.drools.compiler.rule.builder.RuleClassBuilder)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 Function (org.drools.core.rule.Function)1 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)1 Wireable (org.drools.core.spi.Wireable)1 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)1