<commit_before>/*******************************************************************************
 * Copyright (C) 2007 The University of Manchester   
 * 
 *  Modifications to the initial code base are copyright of their
 *  respective authors, or their employers as appropriate.
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *    
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *    
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.workbench.views.monitor.progressreport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.WeakHashMap;

import net.sf.taverna.t2.facade.WorkflowInstanceFacade;
import net.sf.taverna.t2.facade.WorkflowInstanceFacade.State;
import net.sf.taverna.t2.lang.observer.Observable;
import net.sf.taverna.t2.lang.observer.Observer;
import net.sf.taverna.t2.monitor.MonitorManager;
import net.sf.taverna.t2.monitor.MonitorableProperty;
import net.sf.taverna.t2.monitor.MonitorManager.AddPropertiesMessage;
import net.sf.taverna.t2.monitor.MonitorManager.DeregisterNodeMessage;
import net.sf.taverna.t2.monitor.MonitorManager.MonitorMessage;
import net.sf.taverna.t2.monitor.MonitorManager.RegisterNodeMessage;
import net.sf.taverna.t2.workflowmodel.Dataflow;
import net.sf.taverna.t2.workflowmodel.Processor;
import net.sf.taverna.t2.workflowmodel.processor.activity.Activity;

import org.apache.log4j.Logger;

/**
 * An implementation of the Monitor interface that updates the TreeTable when
 * MonitorableProperties change.
 * 
 */
public class WorkflowRunProgressMonitor implements Observer<MonitorMessage> {

	private static final String STATUS_FINISHED = "Finished";
	private static final String STATUS_CANCELLED = "Cancelled";

	// Workflow run status label - we can only tell of workflow is running
	// or is finished from inside this monitor. If workfow run is stopped or
	// paused - this will be updated form the run-ui.
//	private JLabel workflowRunStatusLabel;
	
	private static Logger logger = Logger.getLogger(WorkflowRunProgressMonitor.class);
	
	private static long deregisterDelay = 10000;

	private static long monitorRate = 1000;

	private final WorkflowRunProgressTreeTable progressTreeTable;

	// Map of owning process ids for processors to their corresponding MonitorNodes
	// (we only create MonitorNodes for processors)
	private Map<String, WorkflowRunProgressMonitorNode> processorMonitorNodes = new HashMap<String, WorkflowRunProgressMonitorNode>();

	// Map of owning process ids to workflow objects (including the processor,
	// dataflow and facade objects)
	private Map<String, Object> workflowObjects = Collections.synchronizedMap(new HashMap<String, Object>());

	// Map from invocation process ID to start time
	private static Map<String, Date> activitityInvocationStartTimes = Collections.synchronizedMap(new HashMap<String, Date>());
	
	private static Map<Processor, List<Long>> processorInvocationTimes = Collections.synchronizedMap(new WeakHashMap<Processor, List<Long>>());
	
	//private Map<String, ResultListener> resultListeners = new HashMap<String, ResultListener>();

	private Timer updateTimer = new Timer("Progress table monitor update timer", true);

	private UpdateTask updateTask;

	// Filter only events for the workflow shown in the progressTreeTable
	private String filter;
	
	private WorkflowInstanceFacade facade;

	public WorkflowRunProgressMonitor(WorkflowRunProgressTreeTable progressTreeTable, WorkflowInstanceFacade facade) {
		this.progressTreeTable = progressTreeTable;
		this.facade = facade;
	}

	public void onDispose() {
	    updateTimer.cancel();
	    updateTask.run();
	}

	@Override
	protected void finalize() throws Throwable {
		onDispose();
	}

	public void notify(Observable<MonitorMessage> sender, MonitorMessage message)
			throws Exception {
		if (message instanceof RegisterNodeMessage) {
			RegisterNodeMessage regMessage = (RegisterNodeMessage) message;
			registerNode(regMessage.getWorkflowObject(), regMessage
					.getOwningProcess(), regMessage.getProperties());
		} else if (message instanceof DeregisterNodeMessage) {
			deregisterNode(message.getOwningProcess());
		} else if (message instanceof AddPropertiesMessage) {
			AddPropertiesMessage addMessage = (AddPropertiesMessage) message;
			addPropertiesToNode(addMessage.getOwningProcess(), addMessage
					.getNewProperties());
		} else {
			logger.warn("Unknown message " + message + " from " + sender);
		}		
	}

	private void registerNode(Object workflowObject, String[] owningProcess,
			Set<MonitorableProperty<?>> properties) {
				
		if (filter == null && owningProcess.length == 1) {
			filter = owningProcess[0];<commit_msg>Fixed NullPointerException<commit_after>/*******************************************************************************
 * Copyright (C) 2007 The University of Manchester   
 * 
 *  Modifications to the initial code base are copyright of their
 *  respective authors, or their employers as appropriate.
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *    
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *    
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/
package net.sf.taverna.t2.workbench.views.monitor.progressreport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.WeakHashMap;

import net.sf.taverna.t2.facade.WorkflowInstanceFacade;
import net.sf.taverna.t2.facade.WorkflowInstanceFacade.State;
import net.sf.taverna.t2.lang.observer.Observable;
import net.sf.taverna.t2.lang.observer.Observer;
import net.sf.taverna.t2.monitor.MonitorManager;
import net.sf.taverna.t2.monitor.MonitorableProperty;
import net.sf.taverna.t2.monitor.MonitorManager.AddPropertiesMessage;
import net.sf.taverna.t2.monitor.MonitorManager.DeregisterNodeMessage;
import net.sf.taverna.t2.monitor.MonitorManager.MonitorMessage;
import net.sf.taverna.t2.monitor.MonitorManager.RegisterNodeMessage;
import net.sf.taverna.t2.workflowmodel.Dataflow;
import net.sf.taverna.t2.workflowmodel.Processor;
import net.sf.taverna.t2.workflowmodel.processor.activity.Activity;

import org.apache.log4j.Logger;

/**
 * An implementation of the Monitor interface that updates the TreeTable when
 * MonitorableProperties change.
 * 
 */
public class WorkflowRunProgressMonitor implements Observer<MonitorMessage> {

	private static final String STATUS_FINISHED = "Finished";
	private static final String STATUS_CANCELLED = "Cancelled";

	// Workflow run status label - we can only tell of workflow is running
	// or is finished from inside this monitor. If workfow run is stopped or
	// paused - this will be updated form the run-ui.
//	private JLabel workflowRunStatusLabel;
	
	private static Logger logger = Logger.getLogger(WorkflowRunProgressMonitor.class);
	
	private static long deregisterDelay = 10000;

	private static long monitorRate = 1000;

	private final WorkflowRunProgressTreeTable progressTreeTable;

	// Map of owning process ids for processors to their corresponding MonitorNodes
	// (we only create MonitorNodes for processors)
	private Map<String, WorkflowRunProgressMonitorNode> processorMonitorNodes = new HashMap<String, WorkflowRunProgressMonitorNode>();

	// Map of owning process ids to workflow objects (including the processor,
	// dataflow and facade objects)
	private Map<String, Object> workflowObjects = Collections.synchronizedMap(new HashMap<String, Object>());

	// Map from invocation process ID to start time
	private static Map<String, Date> activitityInvocationStartTimes = Collections.synchronizedMap(new HashMap<String, Date>());
	
	private static Map<Processor, List<Long>> processorInvocationTimes = Collections.synchronizedMap(new WeakHashMap<Processor, List<Long>>());
	
	//private Map<String, ResultListener> resultListeners = new HashMap<String, ResultListener>();

	private Timer updateTimer = new Timer("Progress table monitor update timer", true);

	private UpdateTask updateTask;

	// Filter only events for the workflow shown in the progressTreeTable
	private String filter;
	
	private WorkflowInstanceFacade facade;

	public WorkflowRunProgressMonitor(WorkflowRunProgressTreeTable progressTreeTable, WorkflowInstanceFacade facade) {
		this.progressTreeTable = progressTreeTable;
		this.facade = facade;
	}

	public void onDispose() {
	    if (updateTimer != null) {
		updateTimer.cancel();
	    }
	    if (updateTask != null) {
		updateTask.run();
	    }
	}

	@Override
	protected void finalize() throws Throwable {
		onDispose();
	}

	public void notify(Observable<MonitorMessage> sender, MonitorMessage message)
			throws Exception {
		if (message instanceof RegisterNodeMessage) {
			RegisterNodeMessage regMessage = (RegisterNodeMessage) message;
			registerNode(regMessage.getWorkflowObject(), regMessage
					.getOwningProcess(), regMessage.getProperties());
		} else if (message instanceof DeregisterNodeMessage) {
			deregisterNode(message.getOwningProcess());
		} else if (message instanceof AddPropertiesMessage) {
			AddPropertiesMessage addMessage = (AddPropertiesMessage) message;
			addPropertiesToNode(addMessage.getOwningProcess(), addMessage
					.getNewProperties());
		} else {
			logger.warn("Unknown message " + message + " from " + sender);
		}		
	}

	private void registerNode(Object workflowObject, String[] owningProcess,
			Set<MonitorableProperty<?>> properties) {
				
		if (filter == null && owningProcess.length == 1) {
			filter = owningProcess[0];
{'fraction_non_alphanumeric': 0.1092015753669889, 'fraction_numerical': 0.008234872896527032, 'mean_word_length': 5.145764576457646, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4829797', 'n_tokens_mistral': 3198, 'n_tokens_neox': 2955, 'n_words': 936}