Search in sources :

Example 86 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testLocalTransactionPerStatement.

@Test
public void testLocalTransactionPerStatement() {
    String str = "";
    str += "package org.kie.test\n";
    str += "global java.util.List list\n";
    str += "rule rule1\n";
    str += "when\n";
    str += "  Integer(intValue > 0)\n";
    str += "then\n";
    str += "  list.add( 1 );\n";
    str += "end\n";
    str += "\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert(2);
    ksession.insert(3);
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 87 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testGetFactHandles.

@Test
public void testGetFactHandles() {
    // DROOLS-1270
    String str = "package org.kie.test\n" + "rule rule1 when\n" + "  String(this == \"A\")\n" + "then\n" + "  insertLogical( \"B\" );\n" + "end\n" + "\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
    ksession.insert("A");
    ksession.fireAllRules();
    assertEquals(2, ksession.getFactCount());
    for (FactHandle fh : ksession.getFactHandles()) {
        System.out.println(fh);
        if (fh.toString().contains("String:A")) {
            ksession.delete(fh);
        }
    }
    ksession.fireAllRules();
    assertEquals(0, ksession.getFactCount());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 88 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testMergeConfig.

@Test
public void testMergeConfig() {
    // JBRULES-3155
    String str = "";
    str += "package org.kie.test\n";
    str += "global java.util.List list\n";
    str += "rule rule1\n";
    str += "when\n";
    str += "  $i : Integer(intValue > 0)\n";
    str += "then\n";
    str += "  list.add( $i );\n";
    str += "end\n";
    str += "\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    Properties properties = new Properties();
    properties.put("drools.processInstanceManagerFactory", "com.example.CustomJPAProcessInstanceManagerFactory");
    KieSessionConfiguration config = ks.newKieSessionConfiguration(properties);
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, config, env);
    SessionConfiguration sessionConfig = (SessionConfiguration) ksession.getSessionConfiguration();
    assertEquals("com.example.CustomJPAProcessInstanceManagerFactory", sessionConfig.getProcessInstanceManagerFactory());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Properties(java.util.Properties) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) Test(org.junit.Test)

Example 89 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class JpaPersistentStatefulSessionTest method testGetCount.

@Test
public void testGetCount() {
    // BZ-1022374
    String str = "";
    str += "package org.kie.test\n";
    str += "rule rule1\n";
    str += "when\n";
    str += "then\n";
    str += " insertLogical( new String(\"a\") );\n";
    str += "end\n";
    str += "\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
    assertEquals(1, ksession.fireAllRules());
    assertEquals(1, ksession.getFactCount());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 90 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class DroolsAbstractPMMLTest method readKnowledgeBase.

private static KieBase readKnowledgeBase(List<InputStream> theory) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int j = 0; j < theory.size(); j++) {
        Resource res = ks.getResources().newInputStreamResource(theory.get(j));
        kfs.write(RESOURCE_PATH + "source_" + j + ".drl", res);
    }
    KieModuleModel model = ks.newKieModuleModel();
    KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).setDefault(true).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.STREAM);
    kfs.writeKModuleXML(model.toXML());
    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();
    if (kb.getResults().hasMessages(Message.Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    return kContainer.getKieBase();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Aggregations

KieFileSystem (org.kie.api.builder.KieFileSystem)297 KieServices (org.kie.api.KieServices)241 Test (org.junit.Test)211 KieBuilder (org.kie.api.builder.KieBuilder)179 KieSession (org.kie.api.runtime.KieSession)101 KieModuleModel (org.kie.api.builder.model.KieModuleModel)79 KieContainer (org.kie.api.runtime.KieContainer)74 ReleaseId (org.kie.api.builder.ReleaseId)64 Results (org.kie.api.builder.Results)60 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)53 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)39 KieBase (org.kie.api.KieBase)38 IncrementalResults (org.kie.internal.builder.IncrementalResults)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)32 KieBaseModel (org.kie.api.builder.model.KieBaseModel)31 ArrayList (java.util.ArrayList)30 Message (org.kie.api.builder.Message)26 KieModule (org.kie.api.builder.KieModule)22 Message (org.drools.compiler.Message)20 Resource (org.kie.api.io.Resource)18