Search in sources :

Example 26 with ByteArrayResource

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

the class TraitTest method testTraitEncoding.

@Test
public void testTraitEncoding() {
    String s1 = "package org.drools.core.factmodel.traits;\n" + "import " + Entity.class.getCanonicalName() + ";\n" + "declare trait A 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" + "" + "rule \"donOneThing\"\n" + "when\n" + "    $x : Entity()\n" + "then\n" + "    don( $x, A.class );\n" + "end\n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    String( this == \"y\" ) \n" + "    $x : Entity()\n" + "then\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";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactoryImpl.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    TraitRegistryImpl tr = (TraitRegistryImpl) ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(kbase);
    System.out.println(tr.getHierarchy());
    Entity ent = new Entity("x");
    KieSession ksession = kbase.newKieSession();
    ksession.insert(ent);
    ksession.fireAllRules();
    assertEquals(1, ent.getMostSpecificTraits().size());
    ksession.insert("y");
    ksession.fireAllRules();
    System.out.println(ent.getMostSpecificTraits());
    assertEquals(2, ent.getMostSpecificTraits().size());
}
Also used : Entity(org.drools.traits.core.factmodel.Entity) TraitRegistryImpl(org.drools.traits.core.factmodel.TraitRegistryImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 27 with ByteArrayResource

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

the class DeclaredTypeDifferentKJarIncludesTest method superKieBase.

private void superKieBase(KieServices kieServices) {
    KieModuleModel superKModule = kieServices.newKieModuleModel();
    KieBaseModel superKieBase = superKModule.newKieBaseModel(SUPER_KBASE_NAME);
    superKieBase.addPackage(SUPER_KBASE_PACKAGE);
    KieFileSystem superFileSystem = kieServices.newKieFileSystem();
    ByteArrayResource rule = new ByteArrayResource(SUPER_RULE.getBytes(UTF_8), UTF_8.name());
    superFileSystem.write("src/main/resources/org/superkbase/superrules.drl", rule);
    superFileSystem.writeKModuleXML(superKModule.toXML());
    superFileSystem.write("pom.xml", generatePomXmlWithDependencies(SUPER_RELEASE_ID));
    kieServices.newKieBuilder(superFileSystem).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 28 with ByteArrayResource

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

the class ConstraintEvaluationExceptionTest method initConstraintTestField.

private void initConstraintTestField(String constraint, String ruleName, String ruleFileName) {
    if (testRunType.isExecutableModel()) {
        predicateInformation = new PredicateInformation(constraint, ruleName, ruleFileName);
    } else {
        mvelConstraint = new MVELConstraint("com.example", constraint, 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);
        mvelConstraint.registerEvaluationContext(buildContext);
    }
}
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) PredicateInformation(org.drools.model.functions.PredicateInformation) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 29 with ByteArrayResource

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

the class DescrBuilderTest method testDumperFromPkg.

@Test
public void testDumperFromPkg() {
    // DROOLS-109
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    System.out.println(drl);
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) 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();
    KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
    org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
    RuleImpl r = ((RuleImpl) rule);
    assertEquals(2, r.getLhs().getChildren().size());
    Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
    RuleConditionElement arg1 = iter.next();
    assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
    assertEquals(2, ((GroupElement) arg1).getChildren().size());
    RuleConditionElement arg2 = iter.next();
    assertTrue(arg2 instanceof Pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleConditionElement(org.drools.core.rule.RuleConditionElement) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 30 with ByteArrayResource

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

the class BigRuleSetCompilationTest method generateResourcesToBeCompiled.

private Collection<Resource> generateResourcesToBeCompiled(int numberOfResources, int rulesPerResource) {
    Collection<Resource> resources = new ArrayList<>();
    for (int i = 0; i < numberOfResources; i++) {
        Resource resource = new ByteArrayResource(generateRules("org.kie.kogito.codegen.test" + i, rulesPerResource).getBytes());
        resource.setResourceType(ResourceType.DRL);
        resource.setSourcePath("org/kie/kogito/codegen/test" + i + "/rules.drl");
        resources.add(resource);
    }
    return resources;
}
Also used : Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource)

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