Search in sources :

Example 16 with KieBaseModel

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

the class QueryCepFireUntilHaltTest method prepare.

@Before
public void prepare() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + TestEvent.class.getCanonicalName() + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "end\n" + "query EventsFromStream\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "end\n" + "query ZeroToNineteenSeconds\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "    $result : TestEvent ( this after [0s, 19s] $event) from entry-point SecondStream\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/compiler/integrationtests/queries.drl"));
    assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    firstEntryPoint = ksession.getEntryPoint("FirstStream");
    secondEntryPoint = ksession.getEntryPoint("SecondStream");
    clock = ksession.getSessionClock();
    this.startEngine();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) Before(org.junit.Before)

Example 17 with KieBaseModel

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

the class QueryInRHSCepTest method prepare1.

private void prepare1() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + SolicitFirePojo.class.getCanonicalName() + "\n" + "import " + QueryItemPojo.class.getCanonicalName() + "\n" + "global java.util.List myGlobal \n" + "declare SolicitFirePojo\n" + "    @role( event )\n" + "end\n" + "query \"myQuery\"\n" + "    $r : QueryItemPojo()\n" + "end\n" + "rule \"drools-usage/WLHxG8S\"\n" + " no-loop\n" + " when\n" + " SolicitFirePojo()\n" + " then\n" + " myGlobal.add(drools.getKieRuntime().getQueryResults(\"myQuery\"));\n" + " end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/compiler/integrationtests/" + this.getClass().getName() + ".drl"));
    assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    clock = ksession.getSessionClock();
    myGlobal = new ArrayList<>();
    ksession.setGlobal("myGlobal", myGlobal);
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices)

Example 18 with KieBaseModel

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

the class QueryCepTest method prepare.

@Before
public void prepare() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + TestEvent.class.getCanonicalName() + "\n" + "declare TestEvent\n" + "    @role( event )\n" + "end\n" + "query EventsFromStream\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "end\n" + "query ZeroToNineteenSeconds\n" + "    $event : TestEvent() from entry-point FirstStream\n" + "    $result : TestEvent ( this after [0s, 19s] $event) from entry-point SecondStream\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieModuleModel kmodule = ks.newKieModuleModel();
    KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    baseModel.newKieSessionModel("defaultKSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmodule.toXML());
    kfs.write(ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/compiler/integrationtests/queries.drl"));
    assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty());
    ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    firstEntryPoint = ksession.getEntryPoint("FirstStream");
    secondEntryPoint = ksession.getEntryPoint("SecondStream");
    clock = ksession.getSessionClock();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) Before(org.junit.Before)

Example 19 with KieBaseModel

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

the class KieHelloWorldTest method createKieProjectWithPackages.

private KieModuleModel createKieProjectWithPackages(KieServices ks, String pkg) {
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel().setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).addPackage(pkg);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("realtime")).setDefault(true);
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 20 with KieBaseModel

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

the class KnowledgeBuilderTest method testResourceMapping.

@Ignore
@Test
public void testResourceMapping() throws Exception {
    ServiceRegistryImpl serviceRegistry = (ServiceRegistryImpl) ServiceRegistry.getInstance();
    serviceRegistry.reset();
    ServiceDiscoveryImpl.getInstance().addService(PMMLCompiler.class.getCanonicalName(), new PMMLCompiler() {

        public String compile(InputStream stream, ClassLoader cl) {
            return "rule R2 when then end";
        }

        @Override
        public List<KnowledgeBuilderResult> getResults() {
            return Collections.emptyList();
        }

        @Override
        public void clearResults() {
        // To change body of implemented methods use File | Settings | File Templates.
        }

        public Resource[] transform(Resource input, ClassLoader classLoader) {
            return new Resource[0];
        }

        public String getCompilerVersion() {
            return "KnowledgeBuilderTest";
        }

        @Override
        public List<PMMLResource> precompile(InputStream stream, ClassLoader classLoader, KieBaseModel rootModel) {
            return Collections.emptyList();
        }

        @Override
        public List<PMMLResource> precompile(String fileName, ClassLoader classLoader, KieBaseModel rootModel) {
            return Collections.emptyList();
        }

        @Override
        public Map<String, String> getJavaClasses(InputStream stream) {
            return Collections.emptyMap();
        }

        @Override
        public Map<String, String> getJavaClasses(String fileName) {
            return Collections.emptyMap();
        }
    });
    serviceRegistry.reload();
    String rule = "package org.drools.compiler.test\n" + "rule R1 when\n" + " \n" + "then\n" + "end\n";
    Resource res1 = ResourceFactory.newByteArrayResource(rule.getBytes());
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(res1, ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KiePackage kp1 = kbuilder.getKnowledgePackages().iterator().next();
    assertEquals(1, kp1.getRules().size());
    Rule r = kp1.getRules().iterator().next();
    assertEquals(res1, ((RuleImpl) r).getResource());
    String pmml = "<PMML version=\"4.0\"><Header/></PMML>";
    Resource res2 = ResourceFactory.newByteArrayResource(pmml.getBytes());
    KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder2.add(res2, ResourceType.PMML);
    assertFalse(kbuilder2.getErrors().toString(), kbuilder2.hasErrors());
    KiePackage kp2 = kbuilder2.getKnowledgePackages().iterator().next();
    assertEquals(1, kp2.getRules().size());
    Rule r2 = kp2.getRules().iterator().next();
    assertEquals(res2, ((RuleImpl) r2).getResource());
}
Also used : ServiceRegistryImpl(org.kie.api.internal.utils.ServiceRegistryImpl) InputStream(java.io.InputStream) PMMLCompiler(org.drools.compiler.compiler.PMMLCompiler) PMMLResource(org.drools.compiler.compiler.PMMLResource) Resource(org.kie.api.io.Resource) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

KieBaseModel (org.kie.api.builder.model.KieBaseModel)74 KieModuleModel (org.kie.api.builder.model.KieModuleModel)55 KieServices (org.kie.api.KieServices)45 KieFileSystem (org.kie.api.builder.KieFileSystem)31 Test (org.junit.Test)26 KieSessionModel (org.kie.api.builder.model.KieSessionModel)24 KieBuilder (org.kie.api.builder.KieBuilder)22 KieContainer (org.kie.api.runtime.KieContainer)22 ReleaseId (org.kie.api.builder.ReleaseId)20 KieSession (org.kie.api.runtime.KieSession)13 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)10 ArrayList (java.util.ArrayList)7 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7 KieBase (org.kie.api.KieBase)7 KieModule (org.kie.api.builder.KieModule)7 KieRepository (org.kie.api.builder.KieRepository)6 Before (org.junit.Before)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4