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;
}
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);
}
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;
}
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));
}
}
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);
}
Aggregations