Search in sources :

Example 66 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class OOPathCastTest method testInvalidCast.

@Test
public void testInvalidCast() {
    final String drl = "import org.drools.compiler.oopath.model.*;\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Man( $toy: /wife/children#Man/toys )\n" + "then\n" + "  list.add( $toy.getName() );\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    final Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertTrue(results.hasMessages(Message.Level.ERROR));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Example 67 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class GetKieContainerCommand method execute.

public KieContainer execute(Context context) {
    // use the new API to retrieve the session by ID
    KieServices kieServices = KieServices.Factory.get();
    KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    ((RegistryContext) context).register(KieContainer.class, kieContainer);
    return kieContainer;
}
Also used : KieServices(org.kie.api.KieServices) RegistryContext(org.drools.core.command.impl.RegistryContext) KieContainer(org.kie.api.runtime.KieContainer)

Example 68 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class Misc2Test method testNonSerializableInEvaluatorWrapper.

@Test
public void testNonSerializableInEvaluatorWrapper() throws Exception {
    // BZ-1315143
    String str = "package org.drools.compiler\n" + "rule B\n" + "  when\n" + "    $m1 : Message( $message1 : message, $date1 : birthday )\n" + "    $m2 : Message( this != $m1, message != $message1, birthday after $date1 )\n" + "  then\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newByteArrayResource(str.getBytes()).setTargetPath("org/drools/compiler/rules.drl"));
    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieSession ksession1 = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    Message message1 = new Message();
    message1.setMessage("Hello World");
    message1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-15"));
    Message message2 = new Message();
    message2.setMessage("Goodbye World");
    message2.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-16"));
    ksession1.insert(message1);
    ksession1.insert(message2);
    int fired1 = ksession1.fireAllRules();
    assertEquals(1, fired1);
    ksession1.dispose();
    // Force deepClone
    KieSession ksession2 = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieSession();
    Message message3 = new Message();
    message3.setMessage("Hello World");
    message3.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-15"));
    Message message4 = new Message();
    message4.setMessage("Goodbye World");
    message4.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2015-12-16"));
    ksession2.insert(message3);
    ksession2.insert(message4);
    int fired2 = ksession2.fireAllRules();
    assertEquals(1, fired2);
    ksession2.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 69 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class Misc2Test method testAutomaticallySwitchFromReteOOToPhreak.

@Test
public void testAutomaticallySwitchFromReteOOToPhreak() {
    String str = "rule R when then end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/rule.drl", str);
    KieBuilder kbuilder = ks.newKieBuilder(kfs);
    kbuilder.buildAll();
    assertEquals(0, kbuilder.getResults().getMessages().size());
    KieBase kbase = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()).getKieBase();
    KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 70 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class Misc2Test method testExtendingDate.

@Test
public void testExtendingDate() {
    // BZ-1072629
    String str = "import " + MyDate.class.getCanonicalName() + " \n" + "rule 'sample rule' \n" + "when \n" + "  $date: MyDate() \n" + "then \n" + "$date.setDescription(\"test\"); \n" + "end \n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", str);
    Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertEquals(0, results.getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Aggregations

KieServices (org.kie.api.KieServices)479 Test (org.junit.Test)385 KieContainer (org.kie.api.runtime.KieContainer)249 KieFileSystem (org.kie.api.builder.KieFileSystem)242 KieSession (org.kie.api.runtime.KieSession)239 ReleaseId (org.kie.api.builder.ReleaseId)214 KieBuilder (org.kie.api.builder.KieBuilder)146 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)119 KieModule (org.kie.api.builder.KieModule)104 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)99 KieModuleModel (org.kie.api.builder.model.KieModuleModel)78 ArrayList (java.util.ArrayList)76 Results (org.kie.api.builder.Results)73 KieBase (org.kie.api.KieBase)59 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)49 KieBaseModel (org.kie.api.builder.model.KieBaseModel)45 IncrementalResults (org.kie.internal.builder.IncrementalResults)44 Message (org.drools.compiler.Message)43 Resource (org.kie.api.io.Resource)42 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)28