<commit_before>/**
 * Copyright 2010 JBoss Inc
 *
 * 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.drools.management;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import javax.management.ObjectName;

import org.drools.WorkingMemory;
import org.drools.common.InternalRuleBase;
import org.drools.common.InternalWorkingMemory;
import org.drools.event.ActivationCancelledEvent;
import org.drools.event.ActivationCreatedEvent;
import org.drools.event.AfterActivationFiredEvent;
import org.drools.event.AgendaEventListener;
import org.drools.event.AgendaGroupPoppedEvent;
import org.drools.event.AgendaGroupPushedEvent;
import org.drools.event.BeforeActivationFiredEvent;
import org.drools.event.RuleFlowGroupActivatedEvent;
import org.drools.event.RuleFlowGroupDeactivatedEvent;
import org.drools.event.process.ProcessCompletedEvent;
import org.drools.event.process.ProcessEventListener;
import org.drools.event.process.ProcessNodeLeftEvent;
import org.drools.event.process.ProcessNodeTriggeredEvent;
import org.drools.event.process.ProcessStartedEvent;
import org.drools.event.process.ProcessVariableChangedEvent;
import org.drools.management.KnowledgeSessionMonitoring.AgendaStats.AgendaStatsData;
import org.drools.management.KnowledgeSessionMonitoring.ProcessStats.ProcessInstanceStatsData;
import org.drools.management.KnowledgeSessionMonitoring.ProcessStats.ProcessStatsData;

/**
 * An MBean to monitor a given knowledge session
 * 
 * @author etirelli
 */
public class KnowledgeSessionMonitoring implements KnowledgeSessionMonitoringMBean {

    private static final String KSESSION_PREFIX = "org.drools.kbases";
    
    private static final long NANO_TO_MILLISEC = 1000000;
    
    private InternalWorkingMemory ksession;
    private InternalRuleBase kbase;
    private ObjectName name;
    public AgendaStats agendaStats;
    public ProcessStats processStats;
    
    public KnowledgeSessionMonitoring(InternalWorkingMemory ksession) {
        this.ksession = ksession;
        this.kbase = (InternalRuleBase) ksession.getRuleBase();
        this.name = DroolsManagementAgent.createObjectName(KSESSION_PREFIX + ":type="+kbase.getId()+",group=Sessions,sessionId=Session-"+ksession.getId());
        this.agendaStats = new AgendaStats();
        this.processStats = new ProcessStats();
        this.ksession.addEventListener( agendaStats );
        if (ksession.getProcessRuntime() != null) {
        	this.ksession.getProcessRuntime().addEventListener( processStats );
        }
    }
    
    public void dispose() {
        this.ksession.removeEventListener( agendaStats );
        if (ksession.getProcessRuntime() != null) {
        	this.ksession.getProcessRuntime().removeEventListener( processStats );
        }
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#reset()
     */
    public void reset() {
        this.agendaStats.reset();
        this.processStats.reset();
    }

    public InternalWorkingMemory getKsession() {
        return ksession;
    }

    public InternalRuleBase getKbase() {
        return kbase;
    }

    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getName()
     */
    public ObjectName getName() {
        return name;
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getKnowledgeBaseId()
     */
    public String getKnowledgeBaseId() {
        return kbase.getId();
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getKnowledgeSessionId()
     */
    public long getKnowledgeSessionId() {
        return ksession.getId();
    }

    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getTotalFactCount()
     */
    public long getTotalFactCount() {
        return ksession.getTotalFactCount();
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getTotalActivationsFired()
     */
    public long getTotalActivationsFired() {
        return this.agendaStats.getConsolidatedStats().activationsFired.get();
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getTotalActivationsCancelled()
     */
    public long getTotalActivationsCancelled() {
        return this.agendaStats.getConsolidatedStats().activationsCancelled.get();
    }
    
    /* (non-Javadoc)<commit_msg>JBRULES-2835: fixing missing long sessionId<commit_after>/**
 * Copyright 2010 JBoss Inc
 *
 * 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.drools.management;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

import javax.management.ObjectName;

import org.drools.WorkingMemory;
import org.drools.common.InternalRuleBase;
import org.drools.common.InternalWorkingMemory;
import org.drools.event.ActivationCancelledEvent;
import org.drools.event.ActivationCreatedEvent;
import org.drools.event.AfterActivationFiredEvent;
import org.drools.event.AgendaEventListener;
import org.drools.event.AgendaGroupPoppedEvent;
import org.drools.event.AgendaGroupPushedEvent;
import org.drools.event.BeforeActivationFiredEvent;
import org.drools.event.RuleFlowGroupActivatedEvent;
import org.drools.event.RuleFlowGroupDeactivatedEvent;
import org.drools.event.process.ProcessCompletedEvent;
import org.drools.event.process.ProcessEventListener;
import org.drools.event.process.ProcessNodeLeftEvent;
import org.drools.event.process.ProcessNodeTriggeredEvent;
import org.drools.event.process.ProcessStartedEvent;
import org.drools.event.process.ProcessVariableChangedEvent;
import org.drools.management.KnowledgeSessionMonitoring.AgendaStats.AgendaStatsData;
import org.drools.management.KnowledgeSessionMonitoring.ProcessStats.ProcessInstanceStatsData;
import org.drools.management.KnowledgeSessionMonitoring.ProcessStats.ProcessStatsData;

/**
 * An MBean to monitor a given knowledge session
 * 
 * @author etirelli
 */
public class KnowledgeSessionMonitoring implements KnowledgeSessionMonitoringMBean {

    private static final String KSESSION_PREFIX = "org.drools.kbases";
    
    private static final long NANO_TO_MILLISEC = 1000000;
    
    private InternalWorkingMemory ksession;
    private InternalRuleBase kbase;
    private ObjectName name;
    public AgendaStats agendaStats;
    public ProcessStats processStats;
    
    public KnowledgeSessionMonitoring(InternalWorkingMemory ksession) {
        this.ksession = ksession;
        this.kbase = (InternalRuleBase) ksession.getRuleBase();
        this.name = DroolsManagementAgent.createObjectName(KSESSION_PREFIX + ":type="+kbase.getId()+",group=Sessions,sessionId=Session-"+ksession.getId());
        this.agendaStats = new AgendaStats();
        this.processStats = new ProcessStats();
        this.ksession.addEventListener( agendaStats );
        if (ksession.getProcessRuntime() != null) {
        	this.ksession.getProcessRuntime().addEventListener( processStats );
        }
    }
    
    public void dispose() {
        this.ksession.removeEventListener( agendaStats );
        if (ksession.getProcessRuntime() != null) {
        	this.ksession.getProcessRuntime().removeEventListener( processStats );
        }
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#reset()
     */
    public void reset() {
        this.agendaStats.reset();
        this.processStats.reset();
    }

    public InternalWorkingMemory getKsession() {
        return ksession;
    }

    public InternalRuleBase getKbase() {
        return kbase;
    }

    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getName()
     */
    public ObjectName getName() {
        return name;
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getKnowledgeBaseId()
     */
    public String getKnowledgeBaseId() {
        return kbase.getId();
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getKnowledgeSessionId()
     */
    public int getKnowledgeSessionId() {
        return ksession.getId();
    }

    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getTotalFactCount()
     */
    public long getTotalFactCount() {
        return ksession.getTotalFactCount();
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getTotalActivationsFired()
     */
    public long getTotalActivationsFired() {
        return this.agendaStats.getConsolidatedStats().activationsFired.get();
    }
    
    /* (non-Javadoc)
     * @see org.drools.management.KnowledgeSessionMonitoringMBean#getTotalActivationsCancelled()
     */
    public long getTotalActivationsCancelled() {
        return this.agendaStats.getConsolidatedStats().activationsCancelled.get();
    }
    
    /* (non-Javadoc)
{'fraction_non_alphanumeric': 0.09374696100359817, 'fraction_numerical': 0.003306428085189147, 'mean_word_length': 3.8971428571428572, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '6964029', 'n_tokens_mistral': 3007, 'n_tokens_neox': 2913, 'n_words': 571}