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