Search in sources :

Example 61 with ByteArrayResource

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);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 62 with ByteArrayResource

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);
    }
}
Also used : MVELConstraint(org.drools.mvel.MVELConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 63 with ByteArrayResource

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());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource)

Example 64 with ByteArrayResource

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);
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 65 with ByteArrayResource

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());
}
Also used : ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) PackageDescr(org.drools.drl.ast.descr.PackageDescr) 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