Search in sources :

Example 31 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project kie-wb-common by kiegroup.

the class AbstractBpmnFileIndexer method buildProcessDefinition.

private List<AbstractBpmnProcessDataEventListener> buildProcessDefinition(String bpmn2Content, ClassLoader moduleClassLoader) throws IllegalArgumentException {
    if (StringUtils.isEmpty(bpmn2Content)) {
        return Collections.<AbstractBpmnProcessDataEventListener>emptyList();
    }
    // Set class loader
    KnowledgeBuilder kbuilder = null;
    if (moduleClassLoader != null) {
        KnowledgeBuilderConfigurationImpl pconf = new KnowledgeBuilderConfigurationImpl(moduleClassLoader);
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pconf);
    } else {
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    }
    // Build
    kbuilder.add(new ByteArrayResource(bpmn2Content.getBytes()), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            logger.error("Error: {}", error.getMessage());
        }
        logger.debug("Process Cannot be Parsed! \n {} \n", bpmn2Content);
        return Collections.<AbstractBpmnProcessDataEventListener>emptyList();
    }
    // Retrieve ProcessInfoHolder
    List<AbstractBpmnProcessDataEventListener> processDataList = new ArrayList<>();
    kbuilder.getKnowledgePackages().forEach(pkg -> pkg.getProcesses().forEach(p -> {
        AbstractBpmnProcessDataEventListener processData = (AbstractBpmnProcessDataEventListener) p.getMetaData().get(getProcessDescriptorName());
        processDataList.add(processData);
    }));
    return processDataList;
}
Also used : KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) ModuleClassLoaderHelper(org.kie.workbench.common.services.backend.project.ModuleClassLoaderHelper) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) Path(org.uberfire.java.nio.file.Path) ReaderResource(org.drools.core.io.impl.ReaderResource) LoggerFactory(org.slf4j.LoggerFactory) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Package(org.guvnor.common.services.project.model.Package) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) Paths(org.uberfire.backend.server.util.Paths) Inject(javax.inject.Inject) ProcessValidatorRegistry(org.jbpm.process.core.validation.ProcessValidatorRegistry) BPMNSemanticModule(org.jbpm.bpmn2.xml.BPMNSemanticModule) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Module(org.guvnor.common.services.project.model.Module) IndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.IndexBuilder) SemanticModules(org.drools.core.xml.SemanticModules) AbstractFileIndexer(org.kie.workbench.common.services.refactoring.backend.server.indexing.AbstractFileIndexer) Logger(org.slf4j.Logger) Process(org.kie.api.definition.process.Process) KieModule(org.kie.workbench.common.services.shared.project.KieModule) BPMNDISemanticModule(org.jbpm.bpmn2.xml.BPMNDISemanticModule) Resource(org.kie.api.io.Resource) BPMNExtensionsSemanticModule(org.jbpm.bpmn2.xml.BPMNExtensionsSemanticModule) List(java.util.List) StringReader(java.io.StringReader) Collections(java.util.Collections) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource)

Example 32 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class CompositePackageDescrTest method addPackageDescrDifferentPkgUUID.

@Test(expected = RuntimeException.class)
public void addPackageDescrDifferentPkgUUID() {
    String pkgUUID = generateUUID();
    PackageDescr first = new PackageDescr(NAMESPACE);
    first.setPreferredPkgUUID(pkgUUID);
    assertTrue(first.getPreferredPkgUUID().isPresent());
    compositePackageDescr.addPackageDescr(new ByteArrayResource(), first);
    assertTrue(compositePackageDescr.getPreferredPkgUUID().isPresent());
    assertEquals(pkgUUID, compositePackageDescr.getPreferredPkgUUID().get());
    pkgUUID = generateUUID();
    PackageDescr second = new PackageDescr(NAMESPACE);
    second.setPreferredPkgUUID(pkgUUID);
    assertTrue(second.getPreferredPkgUUID().isPresent());
    assertNotEquals(first.getPreferredPkgUUID().get(), second.getPreferredPkgUUID().get());
    compositePackageDescr.addPackageDescr(new ByteArrayResource(), second);
}
Also used : ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 33 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project kie-wb-common by kiegroup.

the class BpmnFileIndexer method buildProcessDefinition.

private List<BpmnProcessDataEventListener> buildProcessDefinition(String bpmn2Content, ClassLoader moduleClassLoader) throws IllegalArgumentException {
    if (StringUtils.isEmpty(bpmn2Content)) {
        return Collections.<BpmnProcessDataEventListener>emptyList();
    }
    // Set class loader
    KnowledgeBuilder kbuilder = null;
    if (moduleClassLoader != null) {
        KnowledgeBuilderConfigurationImpl pconf = new KnowledgeBuilderConfigurationImpl(moduleClassLoader);
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(pconf);
    } else {
        kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    }
    // Build
    kbuilder.add(new ByteArrayResource(bpmn2Content.getBytes()), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            logger.error("Error: {}", error.getMessage());
        }
        logger.debug("Process Cannot be Parsed! \n {} \n", bpmn2Content);
        return Collections.<BpmnProcessDataEventListener>emptyList();
    }
    // Retrieve ProcessInfoHolder
    List<BpmnProcessDataEventListener> processDataList = new ArrayList<>();
    kbuilder.getKnowledgePackages().forEach(pkg -> pkg.getProcesses().forEach(p -> {
        BpmnProcessDataEventListener processData = (BpmnProcessDataEventListener) p.getMetaData().get(BpmnProcessDataEventListener.NAME);
        processDataList.add(processData);
    }));
    return processDataList;
}
Also used : KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) XmlProcessReader(org.jbpm.compiler.xml.XmlProcessReader) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) ModuleClassLoaderHelper(org.kie.workbench.common.services.backend.project.ModuleClassLoaderHelper) DefaultIndexBuilder(org.kie.workbench.common.services.refactoring.backend.server.indexing.DefaultIndexBuilder) Path(org.uberfire.java.nio.file.Path) ReaderResource(org.drools.core.io.impl.ReaderResource) LoggerFactory(org.slf4j.LoggerFactory) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Package(org.guvnor.common.services.project.model.Package) ProcessValidationError(org.jbpm.process.core.validation.ProcessValidationError) Paths(org.uberfire.backend.server.util.Paths) Inject(javax.inject.Inject) ProcessValidatorRegistry(org.jbpm.process.core.validation.ProcessValidatorRegistry) BPMNSemanticModule(org.jbpm.bpmn2.xml.BPMNSemanticModule) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Module(org.guvnor.common.services.project.model.Module) SemanticModules(org.drools.core.xml.SemanticModules) AbstractFileIndexer(org.kie.workbench.common.services.refactoring.backend.server.indexing.AbstractFileIndexer) Logger(org.slf4j.Logger) Process(org.kie.api.definition.process.Process) KieModule(org.kie.workbench.common.services.shared.project.KieModule) BPMNDISemanticModule(org.jbpm.bpmn2.xml.BPMNDISemanticModule) Resource(org.kie.api.io.Resource) BPMNExtensionsSemanticModule(org.jbpm.bpmn2.xml.BPMNExtensionsSemanticModule) List(java.util.List) StringReader(java.io.StringReader) ApplicationScoped(javax.enterprise.context.ApplicationScoped) BPMNDefinitionSetResourceType(org.kie.workbench.common.stunner.bpmn.resource.BPMNDefinitionSetResourceType) Collections(java.util.Collections) ProcessValidator(org.jbpm.process.core.validation.ProcessValidator) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource)

Example 34 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class TraitTest method testTraitModifyCore3.

@Test(timeout = 10000)
public void testTraitModifyCore3() {
    String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "global java.util.List list; \n" + "" + "declare trait A id : int end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "declare trait F extends C end\n" + "declare trait G extends D end\n" + "declare trait H extends D end\n" + "declare trait I extends E end\n" + "declare trait J extends F end\n" + "declare trait K extends G, H end\n" + "declare trait L extends G, H end\n" + "declare trait M extends I, J end\n" + "declare trait N extends K, L end\n" + "" + "declare Core @Traitable id : int = 0 end \n" + "" + "rule \"Init\" when \n" + "then \n" + "   insert( new Core() );" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    $x : Core( id == 0 )\n" + "then\n" + "    don( $x, A.class );\n" + "    don( $x, B.class );\n" + "    don( $x, D.class );\n" + "    don( $x, F.class );\n" + "    don( $x, E.class );\n" + "    don( $x, I.class );\n" + "    don( $x, K.class );\n" + "    don( $x, J.class );\n" + "    don( $x, C.class );\n" + "    don( $x, H.class );\n" + "    don( $x, G.class );\n" + "    don( $x, L.class );\n" + "    don( $x, M.class );\n" + "    don( $x, N.class );\n" + "end\n" + "\n" + "\n" + "\n" + "rule \"Log A\" when $x : A( id == 1 ) then System.out.println( \"A >> \" +  $x ); list.add( 1 ); end \n" + "rule \"Log B\" when $x : B( id == 1 ) then System.out.println( \"B >> \" +  $x ); list.add( 2 ); end \n" + "rule \"Log C\" when $x : C( id == 1 ) then System.out.println( \"C >> \" +  $x ); list.add( 3 ); end \n" + "rule \"Log D\" when $x : D( id == 1 ) then System.out.println( \"D >> \" +  $x ); list.add( 4 ); end \n" + "rule \"Log E\" when $x : E( id == 1 ) then System.out.println( \"E >> \" +  $x ); list.add( 5 ); end \n" + "rule \"Log F\" when $x : F( id == 1 ) then System.out.println( \"F >> \" +  $x ); list.add( 6 ); end \n" + "rule \"Log G\" when $x : G( id == 1 ) then System.out.println( \"G >> \" +  $x ); list.add( 7 ); end \n" + "rule \"Log H\" when $x : H( id == 1 ) then System.out.println( \"H >> \" +  $x ); list.add( 8 ); end \n" + "rule \"Log I\" when $x : I( id == 1 ) then System.out.println( \"I >> \" +  $x ); list.add( 9 ); end \n" + "rule \"Log J\" when $x : J( id == 1 ) then System.out.println( \"J >> \" +  $x ); list.add( 10 ); end \n" + "rule \"Log K\" when $x : K( id == 1 ) then System.out.println( \"K >> \" +  $x ); list.add( 11 ); end \n" + "rule \"Log L\" when $x : L( id == 1 ) then System.out.println( \"L >> \" +  $x ); list.add( 12 ); end \n" + "rule \"Log M\" when $x : M( id == 1 ) then System.out.println( \"M >> \" +  $x ); list.add( 13 ); end \n" + "rule \"Log N\" when $x : N( id == 1 ) then System.out.println( \"N >> \" +  $x ); list.add( 14 ); end \n" + "" + "rule \"Log Core\" when $x : Core( $id : id ) then System.out.println( \"Core >>>>>> \" +  $x ); end \n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + "  String( this == \"go\" ) \n" + "  $x : Core( id == 0 ) \n" + "then \n" + "  System.out.println( \" ------------------------------------------------------------------------------ \" ); \n" + "  modify ( $x ) { setId( 1 ); }" + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // not relevant
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List list = new ArrayList();
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.insert("go");
    ksession.fireAllRules();
    assertEquals(14, list.size());
    for (int j = 1; j <= 14; j++) {
        assertTrue(list.contains(j));
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Test(org.junit.Test)

Example 35 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class TraitTest method testHierarchyEncodeOnPackageMerge.

@Test(timeout = 10000)
public void testHierarchyEncodeOnPackageMerge() {
    String drl0 = "package org.drools.test; " + "declare trait X end ";
    String drl1 = "package org.drools.test; " + "import org.drools.core.factmodel.traits.*; " + "global java.util.List list; " + "declare trait A end " + "declare trait B extends A end " + "declare trait C extends B end " + "";
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, knowledgeBase);
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl0.getBytes()), ResourceType.DRL);
    assertFalse(kb.hasErrors());
    knowledgeBase.addPackages(kb.getKnowledgePackages());
    KnowledgeBuilder kb2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb2.add(new ByteArrayResource(drl1.getBytes()), ResourceType.DRL);
    System.out.print(kb2.getErrors());
    assertFalse(kb2.hasErrors());
    knowledgeBase.addPackages(kb2.getKnowledgePackages());
    HierarchyEncoder<String> hier = ((KnowledgeBaseImpl) knowledgeBase).getConfiguration().getComponentFactory().getTraitRegistry().getHierarchy();
    BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone();
    BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone();
    c.and(b);
    assertEquals(b, c);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) BitSet(java.util.BitSet) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)79 Test (org.junit.Test)67 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)67 KieSession (org.kie.api.runtime.KieSession)33 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 ArrayList (java.util.ArrayList)27 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)25 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)20 List (java.util.List)19 FactType (org.kie.api.definition.type.FactType)16 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)5 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)5 BitSet (java.util.BitSet)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)4 KieBase (org.kie.api.KieBase)4 StringReader (java.io.StringReader)3 LinkedList (java.util.LinkedList)3