Search in sources :

Example 6 with KieBaseModel

use of org.kie.api.builder.model.KieBaseModel 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 7 with KieBaseModel

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

the class KieBuilderSetImpl method registerInitialErrors.

private void registerInitialErrors(KieBuilderImpl kieBuilder) {
    previousErrors = new HashMap<String, Collection<KnowledgeBuilderError>>();
    InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModuleIgnoringErrors();
    for (KieBaseModel kBaseModel : kieModule.getKieModuleModel().getKieBaseModels().values()) {
        KnowledgeBuilder kBuilder = kieModule.getKnowledgeBuilderForKieBase(kBaseModel.getName());
        if (kBuilder != null) {
            previousErrors.put(kBaseModel.getName(), kBuilder.getErrors());
            resourcesWithErrors.put(kBaseModel.getName(), findResourcesWithErrors(kBuilder));
        }
    }
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Collection(java.util.Collection)

Example 8 with KieBaseModel

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

the class KieContainerImpl method update.

private Results update(final InternalKieModule currentKM, final ReleaseId newReleaseId) {
    final InternalKieModule newKM = (InternalKieModule) kr.getKieModule(newReleaseId);
    if (newKM == null) {
        return null;
    }
    final KieJarChangeSet cs = currentKM.getChanges(newKM);
    List<String> modifiedClassNames = getModifiedClasses(cs);
    final boolean modifyingUsedClass = isModifyingUsedClass(modifiedClassNames, getClassLoader());
    final List<Class<?>> modifiedClasses = reinitModifiedClasses(newKM, modifiedClassNames, getClassLoader());
    final List<String> unchangedResources = getUnchangedResources(newKM, cs);
    Map<String, KieBaseModel> currentKieBaseModels = ((KieModuleKieProject) kProject).updateToModule(newKM);
    final ResultsImpl results = new ResultsImpl();
    List<String> kbasesToRemove = new ArrayList<String>();
    for (Entry<String, KieBase> kBaseEntry : kBases.entrySet()) {
        String kbaseName = kBaseEntry.getKey();
        KieBaseModelImpl newKieBaseModel = (KieBaseModelImpl) kProject.getKieBaseModel(kbaseName);
        KieBaseModelImpl currentKieBaseModel = (KieBaseModelImpl) currentKieBaseModels.get(kbaseName);
        // if a kbase no longer exists, just remove it from the cache
        if (newKieBaseModel == null) {
            // have to save for later removal to avoid iteration errors
            kbasesToRemove.add(kbaseName);
        } else {
            final InternalKnowledgeBase kBase = (InternalKnowledgeBase) kBaseEntry.getValue();
            KieBaseUpdateContext context = new KieBaseUpdateContext(kProject, kBase, currentKM, newReleaseId, newKM, cs, modifiedClasses, modifyingUsedClass, unchangedResources, results, newKieBaseModel, currentKieBaseModel);
            kBase.enqueueModification(currentKM.createKieBaseUpdater(context));
        }
    }
    for (String kbaseToRemove : kbasesToRemove) {
        kBases.remove(kbaseToRemove);
    }
    // remove sessions that no longer exist
    this.kSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    this.statelessKSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    return results;
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ArrayList(java.util.ArrayList) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieBase(org.kie.api.KieBase) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 9 with KieBaseModel

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

the class KieModuleKieProject method updateToModule.

public Map<String, KieBaseModel> updateToModule(InternalKieModule updatedKieModule) {
    Map<String, KieBaseModel> oldKieBaseModels = new HashMap<String, KieBaseModel>();
    oldKieBaseModels.putAll(kBaseModels);
    this.kieModules = null;
    this.kJarFromKBaseName.clear();
    ReleaseId currentReleaseId = this.kieModule.getReleaseId();
    ReleaseId updatingReleaseId = updatedKieModule.getReleaseId();
    if (currentReleaseId.getGroupId().equals(updatingReleaseId.getGroupId()) && currentReleaseId.getArtifactId().equals(updatingReleaseId.getArtifactId())) {
        this.kieModule = updatedKieModule;
    } else if (this.kieModule.getKieDependencies().keySet().contains(updatingReleaseId)) {
        this.kieModule.addKieDependency(updatedKieModule);
    }
    synchronized (this) {
        cleanIndex();
        // this might override class definitions, not sure we can do it any other way though
        init();
        // reset resource provider so it will serve resources from updated kmodule
        this.cl.setResourceProvider(kieModule.createResourceProvider());
    }
    return oldKieBaseModels;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) HashMap(java.util.HashMap) ReleaseId(org.kie.api.builder.ReleaseId)

Example 10 with KieBaseModel

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

the class MemoryKieModule method cloneForIncrementalCompilation.

MemoryKieModule cloneForIncrementalCompilation(ReleaseId releaseId, KieModuleModel kModuleModel, MemoryFileSystem newFs) {
    MemoryKieModule clone = new MemoryKieModule(releaseId, kModuleModel, newFs);
    for (InternalKieModule dep : getKieDependencies().values()) {
        clone.addKieDependency(dep);
    }
    for (KieBaseModel kBaseModel : getKieModuleModel().getKieBaseModels().values()) {
        clone.cacheKnowledgeBuilderForKieBase(kBaseModel.getName(), getKnowledgeBuilderForKieBase(kBaseModel.getName()));
    }
    clone.setPomModel(getPomModel());
    for (InternalKieModule dependency : getKieDependencies().values()) {
        clone.addKieDependency(dependency);
    }
    clone.setUnresolvedDependencies(getUnresolvedDependencies());
    return clone;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel)

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