Search in sources :

Example 61 with KieServices

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

the class KieBuilderSetImplTest method testBuild.

@Test
public void testBuild() throws Exception {
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/rule%201.drl", ruleContent());
    final KieBuilderSetImpl kieBuilderSet = new KieBuilderSetImpl(kieBuilder(ks, kfs));
    kieBuilderSet.setFiles(new String[] { "src/main/resources/rule%201.drl" });
    final IncrementalResults build = kieBuilderSet.build();
    assertEquals(0, build.getAddedMessages().size());
    assertEquals(0, build.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Example 62 with KieServices

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

the class KieModuleModelTest method testMarshallingUnmarshalling.

@Test
public void testMarshallingUnmarshalling() {
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel().setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString()).setConfigurationProperty(PropertySpecificOption.PROPERTY_NAME, PropertySpecificOption.ALWAYS.toString());
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).setDeclarativeAgenda(DeclarativeAgendaOption.ENABLED).addInclude("OtherKBase").addPackage("org.kie.pkg1").addPackage("org.kie.pkg2");
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("realtime")).setBeliefSystem(BeliefSystemTypeOption.get("jtms")).setFileLogger("drools.log", 10, true).setDefault(true);
    ksession1.newListenerModel("org.domain.FirstInterface", ListenerModel.Kind.AGENDA_EVENT_LISTENER);
    ksession1.newListenerModel("org.domain.SecondInterface", ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER).newQualifierModel("MyQualfier2");
    ksession1.newListenerModel("org.domain.ThirdInterface", ListenerModel.Kind.PROCESS_EVENT_LISTENER).newQualifierModel("MyQualfier3").setValue("v1");
    ksession1.newListenerModel("org.domain.FourthInterface", ListenerModel.Kind.AGENDA_EVENT_LISTENER).newQualifierModel("MyQualfier4").addArgument("name1", "xxxx").addArgument("name2", "yyyy");
    ksession1.newWorkItemHandlerModel("name", "org.domain.FifthInterface").newQualifierModel("MyQualfier5").addArgument("name1", "aaa").addArgument("name2", "bbb");
    // KieBaseModel kieBaseModel2 = kproj.newKieBaseModel("KBase2")
    // .setEqualsBehavior( EqualityBehaviorOption.EQUALITY )
    // .setEventProcessingMode( EventProcessingOption.STREAM );
    String xml = kproj.toXML();
    // System.out.println( xml );
    KieModuleModel kprojXml = fromXML(xml);
    assertEquals(LanguageLevelOption.DRL6_STRICT.toString(), kprojXml.getConfigurationProperty(LanguageLevelOption.PROPERTY_NAME));
    assertEquals(PropertySpecificOption.ALWAYS.toString(), kprojXml.getConfigurationProperty(PropertySpecificOption.PROPERTY_NAME));
    KieBaseModel kieBaseModelXML = kprojXml.getKieBaseModels().get("KBase1");
    assertSame(kprojXml, ((KieBaseModelImpl) kieBaseModelXML).getKModule());
    assertEquals(EqualityBehaviorOption.EQUALITY, kieBaseModelXML.getEqualsBehavior());
    assertEquals(EventProcessingOption.STREAM, kieBaseModelXML.getEventProcessingMode());
    assertEquals(DeclarativeAgendaOption.ENABLED, kieBaseModelXML.getDeclarativeAgenda());
    assertFalse(kieBaseModelXML.isDefault());
    assertEquals("org.kie.pkg1", kieBaseModelXML.getPackages().get(0));
    assertEquals("org.kie.pkg2", kieBaseModelXML.getPackages().get(1));
    assertEquals("OtherKBase", ((KieBaseModelImpl) kieBaseModelXML).getIncludes().iterator().next());
    KieSessionModel kieSessionModelXML = kieBaseModelXML.getKieSessionModels().get("KSession1");
    assertSame(kieBaseModelXML, ((KieSessionModelImpl) kieSessionModelXML).getKieBaseModel());
    assertEquals(KieSessionType.STATEFUL, kieSessionModelXML.getType());
    assertEquals(ClockTypeOption.get("realtime"), kieSessionModelXML.getClockType());
    assertEquals(BeliefSystemTypeOption.get("jtms"), kieSessionModelXML.getBeliefSystem());
    FileLoggerModel fileLogger = kieSessionModelXML.getFileLogger();
    assertEquals("drools.log", fileLogger.getFile());
    assertEquals(10, fileLogger.getInterval());
    assertEquals(true, fileLogger.isThreaded());
    assertTrue(kieSessionModelXML.isDefault());
    List<ListenerModel> listeners = kieSessionModelXML.getListenerModels();
    ListenerModel listener2 = listeners.get(0);
    assertEquals("org.domain.SecondInterface", listener2.getType());
    assertEquals(ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER, listener2.getKind());
    // QualifierModel qualifier2 = listener2.getQualifierModel();
    // assertEquals("MyQualfier2", qualifier2.getType());
    ListenerModel listener1 = listeners.get(1);
    assertEquals("org.domain.FirstInterface", listener1.getType());
    assertEquals(ListenerModel.Kind.AGENDA_EVENT_LISTENER, listener1.getKind());
    // assertNull(listener1.getQualifierModel());
    ListenerModel listener4 = listeners.get(2);
    assertEquals("org.domain.FourthInterface", listener4.getType());
    assertEquals(ListenerModel.Kind.AGENDA_EVENT_LISTENER, listener4.getKind());
    // QualifierModel qualifier4 = listener4.getQualifierModel();
    // assertEquals("MyQualfier4", qualifier4.getType());
    // assertEquals("xxxx", qualifier4.getArguments().get("name1"));
    // assertEquals("yyyy", qualifier4.getArguments().get("name2"));
    ListenerModel listener3 = listeners.get(3);
    assertEquals("org.domain.ThirdInterface", listener3.getType());
    assertEquals(ListenerModel.Kind.PROCESS_EVENT_LISTENER, listener3.getKind());
    // QualifierModel qualifier3 = listener3.getQualifierModel();
    // assertEquals("MyQualfier3", qualifier3.getType());
    // assertEquals("v1", qualifier3.getValue());
    WorkItemHandlerModel wihm = kieSessionModelXML.getWorkItemHandlerModels().get(0);
    assertEquals("org.domain.FifthInterface", wihm.getType());
// QualifierModel qualifier5 = wihm.getQualifierModel();
// assertEquals("MyQualfier5", qualifier5.getType());
// assertEquals("aaa", qualifier5.getArguments().get("name1"));
// assertEquals("bbb", qualifier5.getArguments().get("name2"));
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) ListenerModel(org.kie.api.builder.model.ListenerModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) WorkItemHandlerModel(org.kie.api.builder.model.WorkItemHandlerModel) KieServices(org.kie.api.KieServices) FileLoggerModel(org.kie.api.builder.model.FileLoggerModel) KieSessionModel(org.kie.api.builder.model.KieSessionModel) Test(org.junit.Test)

Example 63 with KieServices

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

the class KieBuilderImpl method init.

private PomModel init() {
    KieServices ks = KieServices.Factory.get();
    results = new ResultsImpl();
    // if pomXML is null it will generate a default, using default ReleaseId
    // if pomXml is invalid, it assign pomModel to null
    PomModel pomModel = getPomModel();
    // if kModuleModelXML is null or invalid it will generate a default kModule, with a default kbase name
    buildKieModuleModel();
    if (pomModel != null) {
        // creates ReleaseId from build pom
        // If the pom was generated, it will be the same as teh default ReleaseId
        releaseId = pomModel.getReleaseId();
        // add all the pom dependencies to this builder ... not sure this is a good idea (?)
        KieRepositoryImpl repository = (KieRepositoryImpl) ks.getRepository();
        for (AFReleaseId dep : pomModel.getDependencies(DependencyFilter.COMPILE_FILTER)) {
            KieModule depModule = repository.getKieModule(adapt(dep), pomModel);
            if (depModule != null) {
                addKieDependency(depModule);
            }
        }
    } else {
        // if the pomModel is null it means that the provided pom.xml is invalid so use the default releaseId
        releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    }
    return pomModel;
}
Also used : AFReleaseId(org.appformer.maven.support.AFReleaseId) PomModel(org.appformer.maven.support.PomModel) KieServices(org.kie.api.KieServices) KieModule(org.kie.api.builder.KieModule)

Example 64 with KieServices

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

the class AbstractKnowledgeTest method createKieModule.

public KieModuleModel createKieModule(String namespace, boolean createJar, String version) throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kproj = new KieModuleModelImpl();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(namespace + ".KBase1").setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).addPackage(namespace + ".KBase1").setDefault(true);
    kieBaseModel1.newKieSessionModel(namespace + ".KSession1").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("realtime")).setDefault(true);
    kieBaseModel1.newKieSessionModel(namespace + ".KSession2").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    kieBaseModel1.newKieSessionModel(namespace + ".KSessionDefault").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
    KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(namespace + ".KBase2").setEqualsBehavior(EqualityBehaviorOption.IDENTITY).addPackage(namespace + ".KBase2").setEventProcessingMode(EventProcessingOption.CLOUD);
    kieBaseModel2.newKieSessionModel(namespace + ".KSession3").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    KieBaseModel kieBaseModel3 = kproj.newKieBaseModel(namespace + ".KBase3").addInclude(kieBaseModel1.getName()).addInclude(kieBaseModel2.getName()).setEqualsBehavior(EqualityBehaviorOption.IDENTITY).setEventProcessingMode(EventProcessingOption.CLOUD);
    kieBaseModel3.newKieSessionModel(namespace + ".KSession4").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("pseudo"));
    KieServices ks = KieServices.Factory.get();
    KieFileSystemImpl kfs = (KieFileSystemImpl) ks.newKieFileSystem();
    kfs.write("src/main/resources/META-INF/beans.xml", generateBeansXML());
    kfs.writeKModuleXML(((KieModuleModelImpl) kproj).toXML());
    ReleaseId releaseId = ks.newReleaseId(namespace, "art1", version);
    kfs.generateAndWritePomXML(releaseId);
    String kBase1R1 = getRule(namespace + ".test1", "rule1", version);
    String kBase1R2 = getRule(namespace + ".test1", "rule2", version);
    String kbase2R1 = getRule(namespace + ".test2", "rule1", version);
    String kbase2R2 = getRule(namespace + ".test2", "rule2", version);
    String fldKB1 = "src/main/resources/" + kieBaseModel1.getName().replace('.', '/');
    String fldKB2 = "src/main/resources/" + kieBaseModel2.getName().replace('.', '/');
    kfs.write(fldKB1 + "/rule1.drl", kBase1R1.getBytes());
    kfs.write(fldKB1 + "/rule2.drl", kBase1R2.getBytes());
    kfs.write(fldKB2 + "/rule1.drl", kbase2R1.getBytes());
    kfs.write(fldKB2 + "/rule2.drl", kbase2R2.getBytes());
    kfs.write("src/main/java/org/drools/compiler/cdi/test/KProjectTestClass" + namespace + ".java", generateKProjectTestClass(kproj, namespace));
    KieBuilder kBuilder = ks.newKieBuilder(kfs);
    kBuilder.buildAll();
    if (kBuilder.getResults().hasMessages(Level.ERROR)) {
        fail("should not have errors" + kBuilder.getResults());
    }
    MemoryKieModule kieModule = (MemoryKieModule) kBuilder.getKieModule();
    MemoryFileSystem trgMfs = kieModule.getMemoryFileSystem();
    if (createJar) {
        trgMfs.writeAsJar(fileManager.getRootDirectory(), namespace + "-" + version);
    } else {
        java.io.File file = fileManager.newFile(namespace + "-" + version);
        trgMfs.writeAsFs(file);
    }
    return kproj;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl)

Example 65 with KieServices

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

the class SimulateTestBase method createKJarWithMultipleResources.

public static ReleaseId createKJarWithMultipleResources(String id, String[] resources, ResourceType[] types) {
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int i = 0; i < resources.length; i++) {
        String res = resources[i];
        String type = types[i].getDefaultExtension();
        kfs.write("src/main/resources/" + id.replaceAll("\\.", "/") + "/org/test/res" + i + "." + type, res);
    }
    KieBaseModel kBase1 = kproj.newKieBaseModel(id).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kBase1.newKieSessionModel(id + ".KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertTrue(kieBuilder.getResults().getMessages().isEmpty());
    KieModule kieModule = kieBuilder.getKieModule();
    return kieModule.getReleaseId();
}
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) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

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