org.kie.api.builder.KieBuilder Java Examples
The following examples show how to use
org.kie.api.builder.KieBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MessageImplTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test //See DROOLS-193 (KnowledgeBuilderResult does not always contain a Resource) public void testMessageFromInvalidDSL() throws Exception { //Some suitably duff DSL String dsl = "bananna\n"; //Some suitably valid DRL String drl = "import org.drools.compiler.Person;\n" + "rule R1\n" + "when\n" + "There is a Person\n" + "then\n" + "end\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem() .write( "src/main/resources/dsl.dsl", dsl ) .write( "src/main/resources/drl.dslr", drl ); KieBuilder kieBuilder = ks.newKieBuilder( kfs ).buildAll(); Results results = kieBuilder.getResults(); assertEquals( 3, results.getMessages().size() ); }
Example #2
Source File: ReloadDroolsRulesService.java From drools-examples with Apache License 2.0 | 6 votes |
private KieContainer loadContainerFromString(List<Rule> rules) { long startTime = System.currentTimeMillis(); KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); KieFileSystem kfs = ks.newKieFileSystem(); for (Rule rule:rules) { String drl=rule.getContent(); kfs.write("src/main/resources/" + drl.hashCode() + ".drl", drl); } KieBuilder kb = ks.newKieBuilder(kfs); kb.buildAll(); if (kb.getResults().hasMessages(Message.Level.ERROR)) { throw new RuntimeException("Build Errors:\n" + kb.getResults().toString()); } long endTime = System.currentTimeMillis(); System.out.println("Time to build rules : " + (endTime - startTime) + " ms" ); startTime = System.currentTimeMillis(); KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId()); endTime = System.currentTimeMillis(); System.out.println("Time to load container: " + (endTime - startTime) + " ms" ); return kContainer; }
Example #3
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public void createAndTestKieContainer(ReleaseId releaseId, KieBuilder kb, String kBaseName) throws IOException, ClassNotFoundException, InterruptedException { KieServices ks = KieServices.Factory.get(); kb.buildAll(); if ( kb.getResults().hasMessages(Level.ERROR) ) { fail("Unable to build KieModule\n" + kb.getResults( ).toString() ); } KieRepository kr = ks.getRepository(); KieModule kJar = kr.getKieModule(releaseId); assertNotNull( kJar ); KieContainer kContainer = ks.newKieContainer(releaseId); KieBase kBase = kBaseName != null ? kContainer.getKieBase( kBaseName ) : kContainer.getKieBase(); KieSession kSession = kBase.newKieSession(); List list = new ArrayList(); kSession.setGlobal( "list", list ); kSession.fireAllRules(); assertEquals( 1, list.size() ); assertEquals( "org.kie.test.Message", list.get(0).getClass().getName() ); }
Example #4
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testPreventReflectionAPIsOnJavaClasses() { String drl = "package org.test; " + // existing java class "declare org.drools.compiler.Person " + " @role(event) " + "end \n" + ""; KieBuilder kieBuilder = build(drl); assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase(); assertThrows(UnsupportedOperationException.class, () -> kieBase.getFactType( "org.drools.compiler", "Person" )); }
Example #5
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testUnknownField() throws InstantiationException, IllegalAccessException { // DROOLS-546 String drl = "package org.test; " + "declare Pet" + " " + "end \n"; KieBuilder kieBuilder = build(drl); assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase(); FactType factType = kieBase.getFactType("org.test", "Pet"); Object instance = factType.newInstance(); factType.get(instance, "unknownField"); factType.set(instance, "unknownField", "myValue"); }
Example #6
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testTooManyPositionalArguments() throws InstantiationException, IllegalAccessException { // DROOLS-559 String drl = "package org.test;\n" + "global java.util.List names;\n" + "declare Person\n" + " name : String\n" + " age : int\n" + "end\n" + "rule R when \n" + " $p : Person( \"Mark\", 37, 42; )\n" + "then\n" + " names.add( $p.getName() );\n" + "end\n"; KieBuilder kieBuilder = build(drl); assertTrue( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); }
Example #7
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testOutOfRangePositions() throws InstantiationException, IllegalAccessException { // DROOLS-559 String drl = "package org.test;\n" + "global java.util.List names;\n" + "declare Person\n" + " name : String @position(3)\n" + " age : int @position(1)\n" + "end\n" + "rule R when \n" + " $p : Person( 37, \"Mark\"; )\n" + "then\n" + " names.add( $p.getName() );\n" + "end\n"; KieBuilder kieBuilder = build(drl); assertTrue( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); }
Example #8
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testDuplicatedPositions() throws InstantiationException, IllegalAccessException { // DROOLS-559 String drl = "package org.test;\n" + "global java.util.List names;\n" + "declare Person\n" + " name : String @position(1)\n" + " age : int @position(1)\n" + "end\n" + "rule R when \n" + " $p : Person( 37, \"Mark\"; )\n" + "then\n" + " names.add( $p.getName() );\n" + "end\n"; KieBuilder kieBuilder = build(drl); assertTrue( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); }
Example #9
Source File: JbpmBpmn2TestCase.java From kogito-runtimes with Apache License 2.0 | 6 votes |
protected KieBase createKnowledgeBaseFromResources(Resource... process) throws Exception { KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); if (process.length > 0) { KieFileSystem kfs = ks.newKieFileSystem(); for (Resource p : process) { kfs.write(p); } KieBuilder kb = ks.newKieBuilder(kfs); kb.buildAll(); // kieModule is automatically deployed to KieRepository // if successfully built. if (kb.getResults().hasMessages(Level.ERROR)) { throw new RuntimeException("Build Errors:\n" + kb.getResults().toString()); } } KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId()); return kContainer.getKieBase(); }
Example #10
Source File: DroolsUtils.java From spring-boot-start-current with Apache License 2.0 | 6 votes |
private static KieContainer buildKieContainer ( List< String > ruleContents ) { // 获取的各种对象来完成规则构建、管理和执行等操作 KieServices kieServices = KieServices.Factory.get(); // 用于以编程方式定义组成KieModule的资源 KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); for ( String ruleContent : ruleContents ) { kieFileSystem.write( "src/main/resources/" + System.currentTimeMillis() + ".drl" , ruleContent ); } // KieBuilder是KieModule中包含的资源的构建者 KieBuilder kieBuilder = kieServices.newKieBuilder( kieFileSystem ).buildAll(); // 如果有错误 if ( kieBuilder.getResults().hasMessages() ) { LogUtils.getLogger().warn( kieBuilder.getResults().getMessages() ); if ( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ) { throw new RuntimeException( kieBuilder.getResults() .getMessages() .parallelStream() .map( Message::getText ).collect( Collectors.joining( "," ) ) ); } } // 用来访问KBase和KSession等信息 return kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() ); }
Example #11
Source File: KieHelloWorldTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testErrorReportingWithWrongKmodule() throws Exception { // RHDM-69 String kmodule = "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"ABC\" default=\"false\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\"/>\n" + " <kbase name=\"ABC\" default=\"false\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\"/>\n" + "</kmodule>\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", createDrl( "R1" ) ); kfs.writeKModuleXML(kmodule); KieBuilder kb = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 1, kb.getResults().getMessages().size() ); assertTrue( kb.getResults().getMessages().get(0).toString().contains( "ABC" ) ); }
Example #12
Source File: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public static byte[] createJar(KieServices ks, String kmoduleContent, Predicate<String> classFilter, ReleaseId releaseId, Resource... resources) { KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kmoduleContent); for (int i = 0; i < resources.length; i++) { if (resources[i] != null) { kfs.write(resources[i]); } } KieBuilder kieBuilder = ks.newKieBuilder(kfs); ((InternalKieBuilder) kieBuilder).buildAll(classFilter); Results results = kieBuilder.getResults(); if (results.hasMessages(Message.Level.ERROR)) { throw new IllegalStateException(results.getMessages(Message.Level.ERROR).toString()); } InternalKieModule kieModule = (InternalKieModule) ks.getRepository() .getKieModule(releaseId); byte[] jar = kieModule.getBytes(); return jar; }
Example #13
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testDeclareExtendsWithFullyQualifiedName() { String drl = "package org.drools.extends.test; \n" + "" + "declare org.drools.extends.test.Foo end \n" + "declare org.drools.extends.test.Bar extends org.drools.extends.test.Foo end \n" + ""; KieServices kieServices = KieServices.Factory.get(); KieFileSystem kfs = kieServices.newKieFileSystem(); kfs.write( kieServices.getResources().newByteArrayResource( drl.getBytes() ) .setSourcePath( "test.drl" ) .setResourceType( ResourceType.DRL ) ); KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ); kieBuilder.buildAll(); assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); }
Example #14
Source File: KieDefaultPackageTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testInTestPackage() throws Exception { String javaClass = "" + "package org.jbpm;\n" + "public class Test{}\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/test/java/org/jbpm/Test.java", javaClass ); KieBuilder builder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, builder.getResults().getMessages().size() ); }
Example #15
Source File: KieDefaultPackageTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testAllInDefaultPackage() throws Exception { //This Model will be in the "default package" String model_drl = "" + "declare Smurf\n" + "Field1 : String\n" + "end\n"; //This DRL is in the "default package" String drl = "" + "rule \"test\"\n" + "when\n" + "Smurf()\n" + "then\n" + "end"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/main/resources/model.drl", model_drl ); kfs.write( "src/main/resources/drl.drl", drl ); KieBuilder builder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, builder.getResults().getMessages().size() ); }
Example #16
Source File: SerializationSecurityPolicyTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testSerialization() throws IOException, ClassNotFoundException { final String rule = " rule R " + " when " + " then " + " System.out.println(\"consequence!\"); " + " end"; final KieServices kieServices = KieServices.get(); final Resource drlResource = kieServices.getResources().newByteArrayResource( rule.getBytes(StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name()); drlResource.setResourceType(ResourceType.DRL); drlResource.setTargetPath("test.drl"); final KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); kieFileSystem.write(drlResource); final KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem); kieBuilder.buildAll(); final KieBase kieBase = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase(); final Collection<KiePackage> kpkgs = kieBase.getKiePackages(); final Collection<KiePackage> newCollection = new ArrayList<>(); for (KiePackage kpkg : kpkgs) { kpkg = SerializationHelper.serializeObject(kpkg); newCollection.add(kpkg); } ((InternalKnowledgeBase) kieBase).addPackages(newCollection); final KieSession kieSession = kieBase.newKieSession(); assertThat(kieSession.fireAllRules()).isEqualTo(1); }
Example #17
Source File: DeleteTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@BeforeEach public void setUp() { KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem(); kfs.write(KieServices.Factory.get().getResources() .newClassPathResource(DELETE_TEST_DRL, DeleteTest.class)); KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs); kbuilder.buildAll(); List<Message> res = kbuilder.getResults().getMessages(Level.ERROR); assertThat(res).isEmpty(); KieBase kbase = KieServices.Factory.get() .newKieContainer(kbuilder.getKieModule().getReleaseId()) .getKieBase(); ksession = kbase.newKieSession(); }
Example #18
Source File: DslExpansionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testDSLExpansion_NoExpansion() throws Exception { final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId = ks.newReleaseId( "org.kie", "dsl-test", "1.0" ); final KieModuleModel kproj = ks.newKieModuleModel(); final KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML( kproj.toXML() ) .writePomXML( generatePomXml( releaseId ) ) .write( "src/main/resources/KBase1/test-dsl.dsl", createDSL() ) .write( "src/main/resources/KBase1/test-rule.drl", createDRL() ); final KieBuilder kieBuilder = ks.newKieBuilder( kfs ); final List<Message> messages = kieBuilder.buildAll().getResults().getMessages(); if ( !messages.isEmpty() ) { for ( final Message m : messages ) { System.out.println( m.getText() ); } } assertFalse( messages.isEmpty() ); }
Example #19
Source File: DslExpansionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testDSLExpansion_MessageImplNPE() throws Exception { final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId = ks.newReleaseId( "org.kie", "dsl-test", "1.0" ); final KieModuleModel kproj = ks.newKieModuleModel(); final KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML( kproj.toXML() ) .writePomXML( generatePomXml( releaseId ) ) .write( "src/main/resources/KBase1/test-dsl.dsl", createDSL() ) .write( "src/main/resources/KBase1/test-rule.dslr", createDRL() ); final KieBuilder kieBuilder = ks.newKieBuilder( kfs ); final List<Message> messages = kieBuilder.buildAll().getResults().getMessages(); if ( !messages.isEmpty() ) { for ( final Message m : messages ) { System.out.println( m.getText() ); } } assertTrue( messages.isEmpty() ); }
Example #20
Source File: DroolsAutoConfiguration.java From spring-boot-starter-drools with MIT License | 6 votes |
@Bean @ConditionalOnMissingBean(KieContainer.class) public KieContainer kieContainer() throws IOException { final KieRepository kieRepository = getKieServices().getRepository(); kieRepository.addKieModule(new KieModule() { public ReleaseId getReleaseId() { return kieRepository.getDefaultReleaseId(); } }); KieBuilder kieBuilder = getKieServices().newKieBuilder(kieFileSystem()); kieBuilder.buildAll(); return getKieServices().newKieContainer(kieRepository.getDefaultReleaseId()); }
Example #21
Source File: KieHelloWorldTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testFailingHelloWorld() throws Exception { String drl = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R1 when\n" + " $m : Message( nonExistentField == \"Hello World\" )\n" + "then\n" + "end\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl); KieBuilder kb = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 1, kb.getResults().getMessages().size() ); }
Example #22
Source File: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public static byte[] createJar(KieServices ks, ReleaseId releaseId, String... drls) { KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML( releaseId); for (int i = 0; i < drls.length; i++) { if (drls[i] != null) { kfs.write("src/main/resources/r" + i + ".drl", drls[i]); } } KieBuilder kb = ks.newKieBuilder(kfs).buildAll(); assertFalse(kb.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR), kb.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).toString()); InternalKieModule kieModule = (InternalKieModule) ks.getRepository() .getKieModule(releaseId); byte[] jar = kieModule.getBytes(); return jar; }
Example #23
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private KieBuilder build(String drl) { KieServices kieServices = KieServices.Factory.get(); KieFileSystem kfs = kieServices.newKieFileSystem(); kfs.write( kieServices.getResources().newByteArrayResource( drl.getBytes() ) .setSourcePath( "test.drl" ) .setResourceType( ResourceType.DRL ) ); KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ); kieBuilder.buildAll(); return kieBuilder; }
Example #24
Source File: SeveralKieSessionsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * Inserts a new KieModule containing single KieBase and two KieSession * instances into KieRepository. * * @return created module ReleaseId */ private ReleaseId prepareKieModule() { final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId = ks.newReleaseId("org.drools.compiler", "severalKieSessionsTest", "1.0.0"); KieModuleModel module = ks.newKieModuleModel(); KieBaseModel baseModel = module.newKieBaseModel("defaultKBase"); baseModel.setDefault(true); baseModel.addPackage("*"); baseModel.newKieSessionModel("defaultKSession").setDefault(true); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(module.toXML()); kfs.generateAndWritePomXML(releaseId); kfs.write("src/main/resources/" + PACKAGE_PATH + "/" + DRL_FILE_NAME, ResourceFactory.newClassPathResource(DRL_FILE_NAME, this.getClass())); KieBuilder builder = ks.newKieBuilder(kfs).buildAll(); assertEquals(0, builder.getResults().getMessages().size()); ks.getRepository().addKieModule(builder.getKieModule()); return releaseId; }
Example #25
Source File: KieLoggersTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private KieServices populateKieFileSystem(Resource dt) { KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem().write( dt ); KieBuilder kb = ks.newKieBuilder( kfs ).buildAll(); assertTrue( kb.getResults().getMessages().isEmpty() ); return ks; }
Example #26
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testNotExistingInclude() throws Exception { String drl = "package org.drools.compiler.integrationtests\n" + "declare CancelFact\n" + " cancel : boolean = true\n" + "end\n" + "rule R1 when\n" + " $m : CancelFact( cancel == true )\n" + "then\n" + "end\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", drl ); KieModuleModel module = ks.newKieModuleModel(); final String defaultBaseName = "defaultKBase"; KieBaseModel defaultBase = module.newKieBaseModel(defaultBaseName) .addInclude( "notExistingKB1" ) .addInclude( "notExistingKB2" ); defaultBase.setDefault(true); defaultBase.addPackage( "*" ); defaultBase.newKieSessionModel("defaultKSession").setDefault( true ); kfs.writeKModuleXML( module.toXML() ); KieBuilder kb = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 2, kb.getResults().getMessages().size() ); }
Example #27
Source File: DroolsConfig.java From fw-spring-cloud with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean public KieContainer kieContainer() throws IOException { KieRepository kieRepository = kieServices.getRepository(); kieRepository.addKieModule(kieRepository::getDefaultReleaseId); KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem()); kieBuilder.buildAll(); return kieServices.newKieContainer(kieRepository.getDefaultReleaseId()); }
Example #28
Source File: RuleEngine.java From support-rulesengine with Apache License 2.0 | 5 votes |
private void initKie() { KieServices ks = KieServices.Factory.get(); kfs = ks.newKieFileSystem(); uploadDroolFiles(); KieBuilder kbuilder = ks.newKieBuilder(kfs); kbuilder.buildAll(); if (kbuilder.getResults().hasMessages(Level.ERROR)) { throw new IllegalArgumentException(kbuilder.getResults().toString()); } KieContainer kcontainer = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()); KieBaseConfiguration kbConfig = KieServices.Factory.get().newKieBaseConfiguration(); kbConfig.setOption(ConstraintJittingThresholdOption.get(-1)); kbase = kcontainer.newKieBase(kbConfig); }
Example #29
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testPositionalArguments() throws InstantiationException, IllegalAccessException { String drl = "package org.test;\n" + "global java.util.List names;\n" + "declare Person\n" + " name : String\n" + " age : int\n" + "end\n" + "rule R when \n" + " $p : Person( \"Mark\", 37; )\n" + "then\n" + " names.add( $p.getName() );\n" + "end\n"; KieBuilder kieBuilder = build(drl); assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR)); KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase(); FactType factType = kieBase.getFactType( "org.test", "Person" ); Object instance = factType.newInstance(); factType.set(instance, "name", "Mark"); factType.set(instance, "age", 37); List<String> names = new ArrayList<String>(); KieSession ksession = kieBase.newKieSession(); ksession.setGlobal("names", names); ksession.insert(instance); ksession.fireAllRules(); assertEquals( 1, names.size() ); assertEquals( "Mark", names.get( 0 ) ); }
Example #30
Source File: ListenersTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * Inserts a new KieModule containing single KieBase and a stateful and stateless KieSessions with listeners * into KieRepository. * * @return created KIE module ReleaseId */ private ReleaseId prepareKieModule() { final KieServices ks = KieServices.Factory.get(); KieModuleModel module = ks.newKieModuleModel(); KieBaseModel baseModel = module.newKieBaseModel("defaultKBase"); baseModel.setDefault(true); baseModel.addPackage("*"); KieSessionModel sessionModel = baseModel.newKieSessionModel("defaultKSession"); sessionModel.setDefault(true); sessionModel.setType(KieSessionModel.KieSessionType.STATEFUL); sessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER); sessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER); KieSessionModel statelessSessionModel = baseModel.newKieSessionModel("defaultStatelessKSession"); statelessSessionModel.setDefault(true); statelessSessionModel.setType(KieSessionModel.KieSessionType.STATELESS); statelessSessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER); statelessSessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(module.toXML()); kfs.generateAndWritePomXML(RELEASE_ID); kfs.write("src/main/resources/" + PACKAGE_PATH + "/test.drl", ResourceFactory.newByteArrayResource(DRL.getBytes())); KieBuilder builder = ks.newKieBuilder(kfs).buildAll(); assertEquals(0, builder.getResults().getMessages().size(), "Unexpected compilation errors"); ks.getRepository().addKieModule(builder.getKieModule()); return RELEASE_ID; }