Search in sources :

Example 1 with JavaDialect

use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project drools by kiegroup.

the class ObjectTypeNodeCompiler method compile.

/**
 * Creates a {@link CompiledNetwork} for the specified {@link ObjectTypeNode}. The {@link PackageBuilder} is used
 * to compile the generated source and load the class.
 *
 * @param kBuilder     builder used to compile and load class
 * @param objectTypeNode OTN we are generating a compiled network for
 * @return CompiledNetwork
 */
public static CompiledNetwork compile(KnowledgeBuilderImpl kBuilder, ObjectTypeNode objectTypeNode) {
    if (objectTypeNode == null) {
        throw new IllegalArgumentException("ObjectTypeNode cannot be null!");
    }
    if (kBuilder == null) {
        throw new IllegalArgumentException("PackageBuilder cannot be null!");
    }
    ObjectTypeNodeCompiler compiler = new ObjectTypeNodeCompiler(objectTypeNode);
    String packageName = compiler.getPackageName();
    PackageRegistry pkgReg = kBuilder.getPackageRegistry(packageName);
    if (pkgReg == null) {
        kBuilder.addPackage(new PackageDescr(packageName));
        pkgReg = kBuilder.getPackageRegistry(packageName);
    }
    String source = compiler.generateSource();
    String generatedSourceName = compiler.getName();
    JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
    dialect.addSrc(compiler.getBinaryName(), source.getBytes(IoUtils.UTF8_CHARSET));
    kBuilder.compileAll();
    kBuilder.updateResults();
    CompiledNetwork network;
    try {
        network = (CompiledNetwork) Class.forName(generatedSourceName, true, kBuilder.getRootClassLoader()).newInstance();
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("This is a bug. Please contact the development team", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("This is a bug. Please contact the development team", e);
    } catch (InstantiationException e) {
        throw new RuntimeException("This is a bug. Please contact the development team", e);
    }
    return network;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompiledNetwork(org.drools.core.reteoo.compiled.CompiledNetwork) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect)

Example 2 with JavaDialect

use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project jbpm by kiegroup.

the class JavaActionBuilderTest method testSimpleAction.

@Test
public void testSimpleAction() throws Exception {
    final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
    ActionDescr actionDescr = new ActionDescr();
    actionDescr.setText("list.add( \"hello world\" );");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    JavaDialect javaDialect = (JavaDialect) dialectRegistry.getDialect("java");
    ProcessDescr processDescr = new ProcessDescr();
    processDescr.setClassName("Process1");
    processDescr.setName("Process1");
    WorkflowProcessImpl process = new WorkflowProcessImpl();
    process.setName("Process1");
    process.setPackageName("pkg1");
    ProcessBuildContext context = new ProcessBuildContext(pkgBuilder, pkgBuilder.getPackage("pkg1"), null, processDescr, dialectRegistry, javaDialect);
    context.init(pkgBuilder, pkg, null, dialectRegistry, javaDialect, null);
    pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal java.util.List list;\n"));
    ActionNode actionNode = new ActionNode();
    DroolsAction action = new DroolsConsequenceAction("java", null);
    actionNode.setAction(action);
    ProcessDialect dialect = ProcessDialectRegistry.getDialect("java");
    dialect.getActionBuilder().build(context, action, actionDescr, actionNode);
    dialect.addProcess(context);
    javaDialect.compileAll();
    assertEquals(0, javaDialect.getResults().size());
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
    final KieSession wm = kbase.newKieSession();
    List<String> list = new ArrayList<String>();
    wm.setGlobal("list", list);
    ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
    ((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
    assertEquals("hello world", list.get(0));
}
Also used : DroolsAction(org.jbpm.workflow.core.DroolsAction) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DroolsAction(org.jbpm.workflow.core.DroolsAction) Action(org.jbpm.process.instance.impl.Action) DroolsConsequenceAction(org.jbpm.workflow.core.impl.DroolsConsequenceAction) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) ActionDescr(org.drools.compiler.lang.descr.ActionDescr) ActionNode(org.jbpm.workflow.core.node.ActionNode) ArrayList(java.util.ArrayList) ProcessDescr(org.drools.compiler.lang.descr.ProcessDescr) ProcessDialect(org.jbpm.process.builder.dialect.ProcessDialect) WorkflowProcessImpl(org.jbpm.workflow.core.impl.WorkflowProcessImpl) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) ProcessContext(org.drools.core.spi.ProcessContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 3 with JavaDialect

use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project jbpm by kiegroup.

the class JavaActionBuilder method getAnalysis.

protected AnalysisResult getAnalysis(final PackageBuildContext context, final ActionDescr actionDescr) {
    JavaDialect dialect = (JavaDialect) context.getDialect("java");
    Map<String, Class<?>> variables = new HashMap<String, Class<?>>();
    BoundIdentifiers boundIdentifiers = new BoundIdentifiers(variables, context);
    AnalysisResult analysis = dialect.analyzeBlock(context, actionDescr, actionDescr.getText(), boundIdentifiers);
    return analysis;
}
Also used : HashMap(java.util.HashMap) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 4 with JavaDialect

use of org.drools.compiler.rule.builder.dialect.java.JavaDialect in project jbpm by kiegroup.

the class JavaReturnValueEvaluatorBuilder method getAnalysis.

protected AnalysisResult getAnalysis(final PackageBuildContext context, final ReturnValueDescr descr) {
    JavaDialect dialect = (JavaDialect) context.getDialect("java");
    Map<String, Class<?>> variables = new HashMap<String, Class<?>>();
    BoundIdentifiers boundIdentifiers = new BoundIdentifiers(variables, context);
    AnalysisResult analysis = dialect.analyzeBlock(context, descr, descr.getText(), boundIdentifiers);
    return analysis;
}
Also used : HashMap(java.util.HashMap) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 5 with JavaDialect

use of org.drools.compiler.rule.builder.dialect.java.JavaDialect 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

JavaDialect (org.drools.compiler.rule.builder.dialect.java.JavaDialect)6 ProcessDescr (org.drools.compiler.lang.descr.ProcessDescr)3 StringReader (java.io.StringReader)2 HashMap (java.util.HashMap)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)2 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)2 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 WorkflowProcessImpl (org.jbpm.workflow.core.impl.WorkflowProcessImpl)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 ReturnValueDescr (org.drools.compiler.compiler.ReturnValueDescr)1 ActionDescr (org.drools.compiler.lang.descr.ActionDescr)1