Search in sources :

Example 26 with JavaDialectRuntimeData

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

the class PackageCompilationDataTest method testCodeSourceUrl.

@Test
public void testCodeSourceUrl() throws Exception {
    final String className = TestEvalExpression.class.getName();
    KnowledgeBaseImpl kBase = new KnowledgeBaseImpl("xxx", null);
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools");
    pkg.setClassLoader(Thread.currentThread().getContextClassLoader());
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    kBase.addPackage(pkg);
    final JavaDialectRuntimeData pcData = (JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java");
    final EvalCondition invoker = new EvalCondition(null);
    pcData.putInvoker(className, invoker);
    final InputStream is = getClass().getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");
    try {
        pcData.write(className.replace('.', '/') + ".class", read(is));
    } finally {
        is.close();
    }
    pcData.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pcData.onBeforeExecute();
    Class cls = kBase.getRootClassLoader().loadClass("org.drools.mvel.rule.PackageCompilationDataTest$TestEvalExpression");
    final CodeSource codeSource = invoker.getEvalExpression().getClass().getProtectionDomain().getCodeSource();
    assertNotNull(codeSource.getLocation());
}
Also used : InputStream(java.io.InputStream) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) CodeSource(java.security.CodeSource) EvalCondition(org.drools.core.rule.EvalCondition) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 27 with JavaDialectRuntimeData

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

the class KnowledgePackageImpl method deepCloneIfAlreadyInUse.

public KnowledgePackageImpl deepCloneIfAlreadyInUse(ClassLoader classLoader) {
    if (inUse.compareAndSet(false, true)) {
        return this;
    }
    if (classLoader instanceof ProjectClassLoader) {
        JavaDialectRuntimeData javaDialectRuntimeData = (JavaDialectRuntimeData) dialectRuntimeRegistry.getDialectData("java");
        if (javaDialectRuntimeData == null) {
            // using the canonical model there's no runtime registry and no need for any clone
            return this;
        }
        ClassLoader originalClassLoader = javaDialectRuntimeData.getRootClassLoader();
        if (classLoader == originalClassLoader) {
            // if the classloader isn't changed there's no need for a clone
            return this;
        }
        if (originalClassLoader instanceof ProjectClassLoader) {
            ((ProjectClassLoader) classLoader).initFrom((ProjectClassLoader) originalClassLoader);
        }
    }
    KnowledgePackageImpl clonedPkg = ClassUtils.deepClone(this, classLoader, cloningResources);
    clonedPkg.setClassLoader(classLoader);
    if (ruleUnitDescriptionLoader != null) {
        for (String ruleUnit : ruleUnitDescriptionLoader.getDescriptions().keySet()) {
            clonedPkg.getRuleUnitDescriptionLoader().getDescription(ruleUnit);
        }
    }
    return clonedPkg;
}
Also used : ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData)

Example 28 with JavaDialectRuntimeData

use of org.drools.core.rule.JavaDialectRuntimeData in project jbpm by kiegroup.

the class JavaProcessDialect method addProcess.

public void addProcess(final ProcessBuildContext context) {
    JavaDialect javaDialect = (JavaDialect) context.getDialectRegistry().getDialect("java");
    String processClass = processClassBuilder.buildRule(context);
    if (processClass == null) {
        // nothing to compile.
        return;
    }
    final Process process = context.getProcess();
    final ProcessDescr processDescr = context.getProcessDescr();
    // The compilation result is for the entire rule, so difficult to
    // associate with any descr
    javaDialect.addClassCompileTask(context.getPkg().getName() + "." + processDescr.getClassName(), processDescr, processClass, null, new ProcessErrorHandler(processDescr, process, "Process Compilation error"));
    JavaDialectRuntimeData data = (JavaDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData(javaDialect.getId());
    for (final Iterator it = context.getInvokers().keySet().iterator(); it.hasNext(); ) {
        final String className = (String) it.next();
        // Check if an invoker - Action 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 != null && invoker instanceof Wireable) {
            data.putInvoker(className, (Wireable) invoker);
        }
        final String text = (String) context.getInvokers().get(className);
        final BaseDescr descr = (BaseDescr) context.getDescrLookup(className);
        javaDialect.addClassCompileTask(className, descr, text, null, new ProcessInvokerErrorHandler(processDescr, process, "Unable to generate action invoker."));
    }
// setup the line mappins for this rule
// TODO @TODO must setup mappings
// 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().getPackageCompilationData().getLineMappings().put(
// name,
// mapping );
}
Also used : ProcessErrorHandler(org.jbpm.process.builder.ProcessErrorHandler) Wireable(org.drools.core.spi.Wireable) Iterator(java.util.Iterator) Process(org.kie.api.definition.process.Process) ProcessDescr(org.drools.compiler.lang.descr.ProcessDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ProcessInvokerErrorHandler(org.jbpm.process.builder.ProcessInvokerErrorHandler) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect)

Aggregations

JavaDialectRuntimeData (org.drools.core.rule.JavaDialectRuntimeData)28 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)11 HashMap (java.util.HashMap)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)6 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)5 Test (org.junit.Test)5 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)4 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)4 Consequence (org.drools.core.spi.Consequence)4 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)4 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)3 TypeDeclaration (org.drools.core.rule.TypeDeclaration)3 Wireable (org.drools.core.spi.Wireable)3 Process (org.kie.api.definition.process.Process)3 KieSession (org.kie.api.runtime.KieSession)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2