use of org.kie.api.io.KieResources in project drools by kiegroup.
the class GuidedDecisionTableTest method initKieSession.
private void initKieSession(String... resourceNames) {
KieResources kieResources = KieServices.get().getResources();
final Resource[] resources = Stream.of(resourceNames).map(resource -> kieResources.newClassPathResource(resource, GuidedDecisionTableTest.class)).toArray(Resource[]::new);
final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resources);
kSession = kBase.newKieSession();
rulesFired = new TrackingAgendaEventListener();
kSession.addEventListener(rulesFired);
rulesFired.clear();
}
use of org.kie.api.io.KieResources in project drools by kiegroup.
the class RuleTemplateTest method testGuidedRuleTemplate.
@Test
public void testGuidedRuleTemplate() throws Exception {
final String resourceName = "cheese.template";
final KieResources kieResources = KieServices.get().getResources();
final Resource resource = kieResources.newClassPathResource(resourceName, RuleTemplateTest.class);
resource.setResourceType(ResourceType.TEMPLATE);
final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
final KieSession kSession = kBase.newKieSession();
final Cheese cheese = new Cheese();
cheese.setPrice(90);
final Customer petr = new Customer(0, "Peter");
final Customer john = new Customer(1, "John");
kSession.insert(cheese);
kSession.insert(petr);
kSession.insert(john);
Assertions.assertThat(kSession.fireAllRules()).as("One rule should be fired").isEqualTo(1);
final Collection messages = kSession.getObjects(object -> object instanceof Message);
Assertions.assertThat(messages).hasSize(1);
Assertions.assertThat(messages).hasOnlyOneElementSatisfying(message -> ((Message) message).getMessage().compareTo("Peter satisfied"));
}
use of org.kie.api.io.KieResources in project drools by kiegroup.
the class TypeDeclarationTest method testCrossPackageDeclares.
@Test
public void testCrossPackageDeclares() {
String pkg1 = "package org.drools.compiler.test1; " + "import org.drools.compiler.test2.GrandChild; " + "import org.drools.compiler.test2.Child; " + "import org.drools.compiler.test2.BarFuu; " + "declare FuBaz foo : String end " + "declare Parent " + " unknown : BarFuu " + "end " + "declare GreatChild extends GrandChild " + " father : Child " + "end ";
String pkg2 = "package org.drools.compiler.test2; " + "import org.drools.compiler.test1.Parent; " + "import org.drools.compiler.test1.FuBaz; " + "declare BarFuu " + " baz : FuBaz " + "end " + "declare Child extends Parent " + "end " + "declare GrandChild extends Child " + " notknown : FuBaz " + "end ";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.generateAndWritePomXML(ks.newReleaseId("test", "foo", "1.0"));
KieModuleModel km = ks.newKieModuleModel();
km.newKieBaseModel("rules").addPackage("org.drools.compiler.test2").addPackage("org.drools.compiler.test1");
kfs.writeKModuleXML(km.toXML());
KieResources kr = ks.getResources();
Resource r1 = kr.newByteArrayResource(pkg1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("org/drools/compiler/test1/p1.drl");
Resource r2 = kr.newByteArrayResource(pkg2.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("org/drools/compiler/test2/p2.drl");
kfs.write(r1);
kfs.write(r2);
KieBuilder builder = ks.newKieBuilder(kfs);
builder.buildAll();
assertEquals(Collections.emptyList(), builder.getResults().getMessages(Message.Level.ERROR));
KieContainer kc = ks.newKieContainer(builder.getKieModule().getReleaseId());
FactType ft = kc.getKieBase("rules").getFactType("org.drools.compiler.test2", "Child");
assertNotNull(ft);
assertNotNull(ft.getFactClass());
assertEquals("org.drools.compiler.test1.Parent", ft.getFactClass().getSuperclass().getName());
}
Aggregations