<commit_before>/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.core.workflow.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.drools.KnowledgeBase;
import org.drools.event.process.DefaultProcessEventListener;
import org.drools.event.process.ProcessCompletedEvent;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.openengsb.core.common.Domain;
import org.openengsb.core.common.Event;
import org.openengsb.core.common.context.ContextCurrentService;
import org.openengsb.core.common.workflow.RuleBaseException;
import org.openengsb.core.common.workflow.RuleManager;
import org.openengsb.core.common.workflow.WorkflowException;
import org.openengsb.core.common.workflow.WorkflowService;
import org.openengsb.core.common.workflow.model.RuleBaseElementId;
import org.openengsb.core.common.workflow.model.RuleBaseElementType;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.springframework.osgi.context.BundleContextAware;

public class WorkflowServiceImpl implements WorkflowService, BundleContextAware, ServiceListener {

    private static final String START_FLOW_CONSEQUENCE_LINE = "  flowHelper.startFlow(\"%s\");\n";

    private Log log = LogFactory.getLog(WorkflowServiceImpl.class);

    private static final String FLOW_TRIGGER_RULE_TEMPLATE =
        "## This rule has been autogenerated by the WorkflowService\n" + "when\n" + "  Event ( name == \"%s\" )\n"
                + "then\n";

    private RuleManager rulemanager;
    private ContextCurrentService currentContextService;
    private BundleContext bundleContext;

    private Map<String, Object> services = new HashMap<String, Object>();

    private Map<String, StatefulKnowledgeSession> sessions = new HashMap<String, StatefulKnowledgeSession>();

    private long timeout = 10000;

    @Override
    public void processEvent(Event event) throws WorkflowException {
        StatefulKnowledgeSession session = getSessionForCurrentContext();
        session.insert(event);
        for (ProcessInstance p : session.getProcessInstances()) {
            p.signalEvent(event.getType(), event);
        }
        session.fireAllRules();
    }

    @Override
    public long startFlow(String processId) throws WorkflowException {
        StatefulKnowledgeSession session = getSessionForCurrentContext();
        ProcessInstance processInstance = session.startProcess(processId);
        return processInstance.getId();
    }<commit_msg>OPENENGSB-673: retract events from working memory when processing is done<commit_after>/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.core.workflow.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.drools.KnowledgeBase;
import org.drools.event.process.DefaultProcessEventListener;
import org.drools.event.process.ProcessCompletedEvent;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.drools.runtime.rule.FactHandle;
import org.openengsb.core.common.Domain;
import org.openengsb.core.common.Event;
import org.openengsb.core.common.context.ContextCurrentService;
import org.openengsb.core.common.workflow.RuleBaseException;
import org.openengsb.core.common.workflow.RuleManager;
import org.openengsb.core.common.workflow.WorkflowException;
import org.openengsb.core.common.workflow.WorkflowService;
import org.openengsb.core.common.workflow.model.RuleBaseElementId;
import org.openengsb.core.common.workflow.model.RuleBaseElementType;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.springframework.osgi.context.BundleContextAware;

public class WorkflowServiceImpl implements WorkflowService, BundleContextAware, ServiceListener {

    private static final String START_FLOW_CONSEQUENCE_LINE = "  flowHelper.startFlow(\"%s\");\n";

    private Log log = LogFactory.getLog(WorkflowServiceImpl.class);

    private static final String FLOW_TRIGGER_RULE_TEMPLATE =
        "## This rule has been autogenerated by the WorkflowService\n" + "when\n" + "  Event ( name == \"%s\" )\n"
                + "then\n";

    private RuleManager rulemanager;
    private ContextCurrentService currentContextService;
    private BundleContext bundleContext;

    private Map<String, Object> services = new HashMap<String, Object>();

    private Map<String, StatefulKnowledgeSession> sessions = new HashMap<String, StatefulKnowledgeSession>();

    private long timeout = 10000;

    @Override
    public void processEvent(Event event) throws WorkflowException {
        StatefulKnowledgeSession session = getSessionForCurrentContext();
        session.insert(event);
        session.fireAllRules();
        for (ProcessInstance p : session.getProcessInstances()) {
            p.signalEvent(event.getType(), event);
        }
        FactHandle eventFactHandle = session.getFactHandle(event);
        session.retract(eventFactHandle);
    }

    @Override
    public long startFlow(String processId) throws WorkflowException {
        StatefulKnowledgeSession session = getSessionForCurrentContext();
        ProcessInstance processInstance = session.startProcess(processId);
        return processInstance.getId();
    }
{'fraction_non_alphanumeric': 0.08885209713024282, 'fraction_numerical': 0.0040011037527593815, 'mean_word_length': 4.689952904238618, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1570343', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1946, 'n_words': 505}