Search in sources :

Example 1 with Wireable

use of org.drools.core.spi.Wireable 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 Wireable

use of org.drools.core.spi.Wireable in project drools by kiegroup.

the class MVELDialectRuntimeData method onBeforeExecute.

public void onBeforeExecute() {
    for (Wireable target : wireList) {
        for (MVELCompileable compileable : invokerLookups.get(target)) {
            compileable.compile(this);
            // now wire up the target
            target.wire(compileable);
        }
    }
    wireList.clear();
    for (MVELCompileable compileable : mvelReaders) {
        compileable.compile(this);
    }
    if (dirty) {
        rewireImportedMethods();
        dirty = false;
    }
}
Also used : MVELCompileable(org.drools.core.base.mvel.MVELCompileable) Wireable(org.drools.core.spi.Wireable)

Example 3 with Wireable

use of org.drools.core.spi.Wireable in project drools by kiegroup.

the class JavaDialectRuntimeData method writeExternal.

/**
 * Handles the write serialization of the PackageCompilationData. Patterns in Rules may reference generated data which cannot be serialized by
 * default methods. The PackageCompilationData holds a reference to the generated bytecode. The generated bytecode must be restored before any Rules.
 */
public void writeExternal(ObjectOutput stream) throws IOException {
    KeyStoreHelper helper = new KeyStoreHelper();
    stream.writeBoolean(helper.isSigned());
    if (helper.isSigned()) {
        stream.writeObject(helper.getPvtKeyAlias());
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(bos);
    out.writeInt(this.store.size());
    for (Entry<String, byte[]> entry : this.store.entrySet()) {
        out.writeObject(entry.getKey());
        out.writeObject(entry.getValue());
    }
    out.flush();
    out.close();
    byte[] buff = bos.toByteArray();
    stream.writeObject(buff);
    if (helper.isSigned()) {
        sign(stream, helper, buff);
    }
    stream.writeInt(this.invokerLookups.size());
    for (Entry<String, Wireable> entry : this.invokerLookups.entrySet()) {
        stream.writeObject(entry.getKey());
        stream.writeObject(entry.getValue());
    }
    stream.writeInt(this.classLookups.size());
    for (Entry<String, byte[]> entry : this.classLookups.entrySet()) {
        stream.writeObject(entry.getKey());
        stream.writeObject(entry.getValue());
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) KeyStoreHelper(org.drools.core.util.KeyStoreHelper) Wireable(org.drools.core.spi.Wireable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

Wireable (org.drools.core.spi.Wireable)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutput (java.io.ObjectOutput)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 RuleErrorHandler (org.drools.compiler.builder.impl.errors.RuleErrorHandler)1 RuleInvokerErrorHandler (org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler)1 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 RuleClassBuilder (org.drools.compiler.rule.builder.RuleClassBuilder)1 MVELCompileable (org.drools.core.base.mvel.MVELCompileable)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)1 LineMappings (org.drools.core.rule.LineMappings)1 KeyStoreHelper (org.drools.core.util.KeyStoreHelper)1