package org.jbpm.simulation.impl;

import java.util.ArrayList;
import java.util.List;

import org.jbpm.simulation.AggregatedSimulationEvent;
import org.jbpm.simulation.SimulationEvent;
import org.jbpm.simulation.impl.events.AggregatedActivitySimulationEvent;
import org.jbpm.simulation.impl.events.AggregatedProcessSimulationEvent;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.StatefulKnowledgeSession;

public class WorkingMemorySimulationRepository extends InMemorySimulationRepository {

    private StatefulKnowledgeSession ksession;
    private boolean fireRulesOnStore = false;
    
    public WorkingMemorySimulationRepository() {
        
    }
    
    public WorkingMemorySimulationRepository(String... rules) {
        this(false, rules);
    }
    
    public WorkingMemorySimulationRepository(Resource... rules) {
        this(false, rules);
    }
    
    public WorkingMemorySimulationRepository(boolean fireRulesOnStore, Resource... rules) {
        this.fireRulesOnStore = fireRulesOnStore;
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        
        for (Resource path : rules) {
        
            kbuilder.add(path, ResourceType.DRL);
        }
        if (kbuilder.hasErrors()) {
            throw new RuntimeException("Error while building knowledge base: " + kbuilder.getErrors());
        }
        
        this.ksession = kbuilder.newKnowledgeBase().newStatefulKnowledgeSession();
        try {
            // register global for aggregated events
            ksession.setGlobal("simulation", new ArrayList<AggregatedActivitySimulationEvent>());
            ksession.setGlobal("summary", new ArrayList<AggregatedActivitySimulationEvent>());
            AggregatedProcessSimulationEvent init = new AggregatedProcessSimulationEvent("", 0, 0, 0);
            List processOnlyList = new ArrayList<AggregatedSimulationEvent>();
            processOnlyList.add(init);
            ksession.setGlobal("processEventsOnly", processOnlyList);
        } catch (Exception e) {
            // catch it as there could be no simulation global declared
        }
    }
    
    public WorkingMemorySimulationRepository(boolean fireRulesOnStore, String... rules) {
        this.fireRulesOnStore = fireRulesOnStore;
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        
        for (String path : rules) {
        
            kbuilder.add(ResourceFactory.newClassPathResource(path), ResourceType.DRL);
        }
        if (kbuilder.hasErrors()) {
            throw new RuntimeException("Error while building knowledge base: " + kbuilder.getErrors());
        }
        
        this.ksession = kbuilder.newKnowledgeBase().newStatefulKnowledgeSession();
        try {
            // register global for aggregated events
            ksession.setGlobal("simulation", new ArrayList<AggregatedActivitySimulationEvent>());
        } catch (Exception e) {
            // catch it as there could be no simulation global declared
        }
    }
    
    public void storeEvent(SimulationEvent event) {
        super.storeEvent(event);
        ksession.insert(event);
        if (fireRulesOnStore) {
            ksession.fireAllRules();
        }
    }

    public void fireAllRules() {
        ksession.fireAllRules();
    }
    
    public StatefulKnowledgeSession getSession() {
        return this.ksession;
    }
    
    public List<AggregatedSimulationEvent> getAggregatedEvents() {
        return (List<AggregatedSimulationEvent>) this.ksession.getGlobal("simulation");
    }
    
    public Object getGlobal(String globalName) {
        return  this.ksession.getGlobal(globalName);
    }

    @Override
    public void close() {
        super.close();
        this.ksession.dispose();
    }
    
    
}

@@ -393,16 +393,55 @@
 ceType;%0A
+import org.kie.api.runtime.KieSession;%0A
 import o
@@ -482,16 +482,16 @@
 uilder;%0A
-
 import o
@@ -586,66 +586,8 @@
 ory;
-%0Aimport org.kie.internal.runtime.StatefulKnowledgeSession;
 %0A%0Apu
@@ -683,32 +683,18 @@
 private 
-StatefulKnowledg
+Ki
 eSession
@@ -1587,32 +1587,18 @@
 se().new
-StatefulKnowledg
+Ki
 eSession
@@ -2866,32 +2866,18 @@
 se().new
-StatefulKnowledg
+Ki
 eSession
@@ -3465,24 +3465,10 @@
 lic 
-StatefulKnowledg
+Ki
 eSes

remove usage od StatefulKnowledgeSession in favor of KieSession