use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class DescrBuilderTest method testAccumulate.
@Test
public void testAccumulate() throws InstantiationException, IllegalAccessException {
PackageDescrBuilder packBuilder = DescrFactory.newPackage().newGlobal().identifier("list").type(List.class.getName()).end().name("org.drools.compiler").newRule().name("r1").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot );").end().newRule().name("r2").attribute("dialect", "mvel").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot * 2 );").end();
String drl = new DrlDumper().dump(packBuilder.getDescr());
System.out.println(drl);
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
List list = new ArrayList();
knowledgeSession.setGlobal("list", list);
knowledgeSession.insert(3);
knowledgeSession.insert(39);
knowledgeSession.insert(24.0);
knowledgeSession.insert(42.0);
knowledgeSession.fireAllRules();
assertEquals(Arrays.asList(42.0, 84.0), list);
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class ConstraintEvaluationExceptionTest method addRuleToConstraintTestField.
private void addRuleToConstraintTestField(String ruleName, String ruleFileName) {
if (testRunType.isExecutableModel()) {
predicateInformation.addRuleNames(ruleName, ruleFileName);
} else {
// in non-exec-model, node sharing triggers merging
MVELConstraint otherMvelConstraint = new MVELConstraint("com.example", mvelConstraint.getExpression(), null, null, null, null, null);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
RuleImpl ruleImpl = new RuleImpl(ruleName);
Resource resource = new ByteArrayResource();
resource.setSourcePath(ruleFileName);
ruleImpl.setResource(resource);
buildContext.setRule(ruleImpl);
otherMvelConstraint.registerEvaluationContext(buildContext);
mvelConstraint.mergeEvaluationContext(otherMvelConstraint);
}
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class DeclaredTypeDifferentKJarIncludesTest method childKieBase.
private void childKieBase(KieServices kieServices) {
KieModuleModel childKModule = kieServices.newKieModuleModel();
KieBaseModel childKbase = childKModule.newKieBaseModel(CHILD_KBASE_NAME).setDefault(true).addInclude(SUPER_KBASE_NAME);
childKbase.addPackage(CHILD_KBASE_PACKAGE);
KieFileSystem childFileSystem = kieServices.newKieFileSystem();
ByteArrayResource rule = new ByteArrayResource(CHILD_RULE.getBytes(UTF_8), UTF_8.name());
childFileSystem.write("src/main/resources/org/childkbase/childrules.drl", rule);
childFileSystem.writeKModuleXML(childKModule.toXML());
childFileSystem.write("pom.xml", generatePomXmlWithDependencies(CHILD_RELEASE_ID, SUPER_RELEASE_ID));
kieServices.newKieBuilder(childFileSystem).buildAll(buildProjectClass());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class DescrBuilderTest method testAccumulate.
@Test
public void testAccumulate() throws InstantiationException, IllegalAccessException {
PackageDescrBuilder packBuilder = DescrFactory.newPackage().newGlobal().identifier("list").type(List.class.getName()).end().name("org.drools.mvel.compiler").newRule().name("r1").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot );").end().newRule().name("r2").attribute("dialect", "mvel").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot * 2 );").end();
String drl = new DrlDumper().dump(packBuilder.getDescr());
System.out.println(drl);
KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
List list = new ArrayList();
knowledgeSession.setGlobal("list", list);
knowledgeSession.insert(3);
knowledgeSession.insert(39);
knowledgeSession.insert(24.0);
knowledgeSession.insert(42.0);
knowledgeSession.fireAllRules();
assertEquals(Arrays.asList(42.0, 84.0), list);
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class CompositePackageDescrTest method addPackageDescrSamePkgUUID.
@Test
public void addPackageDescrSamePkgUUID() {
String pkgUUID = generateUUID();
PackageDescr toAdd = new PackageDescr(NAMESPACE);
toAdd.setPreferredPkgUUID(pkgUUID);
compositePackageDescr.addPackageDescr(new ByteArrayResource(), toAdd);
assertTrue(compositePackageDescr.getPreferredPkgUUID().isPresent());
assertEquals(pkgUUID, compositePackageDescr.getPreferredPkgUUID().get());
toAdd = new PackageDescr(NAMESPACE);
toAdd.setPreferredPkgUUID(pkgUUID);
compositePackageDescr.addPackageDescr(new ByteArrayResource(), toAdd);
assertEquals(pkgUUID, compositePackageDescr.getPreferredPkgUUID().get());
}
Aggregations