package jas2.job;
import java.util.*;
import jas2.util.*;
import jas2.jds.module.AnnotatedEventSource;
import jas2.util.tree.*;
import hepjas.analysis.*;
import hepjas.analysis.peer.*;
import java.io.*;
import java.lang.reflect.*;
import jas2.hist.*;
/**
* Note: AbstractJob is extended by LocalJob (for the local case) and JDSJob (for the remote case).
* It is really only JDSJob that needs to extends Observable, but multiple inheritance is not supported
* (and Observable is not an interface, which it should be).
*/
public abstract class AbstractJob extends Observable implements jas2.job.BasicJob
{
public AbstractJob(String name)
{
m_state = "Active";
m_name = name;
}
protected void setRoot(JobFolder root)
{
m_root = root;
}
public AbstractJob()
{
// for object deserialization
}
public void setHepAnalysisJob(hepjas.analysis.Job job)
{
m_hepJob = job;
}
public hepjas.analysis.Job getHepAnalysisJob()
{
return m_hepJob;
}
public SimpleNode getTreeRoot()
{
return m_root;
}
final public void setEventSource(EventGenerator gen)
{
if (m_programs == null) createProgramNode();
// check to see if this name is already in use, if so find an unused name
String newName = gen.getClass().getName();
if (m_dataSets != null && m_dataSets.findChild(newName) != null)
{
for (int i=2; ; i++)
{
newName = gen.getClass().getName()+" ("+i+")";
if (m_dataSets.findChild(newName) == null) break;
}
}
SimpleNode node = m_programs.add(new TreeItem(newName,"generator"), gen);
setCurrentEventSource(node);
notifyInfo();
}
final public void setEventSource(EventSource source)
{
if (m_dataSets == null) createDataSetNode();
// check to see if this name is already in use, if so find an unused name
// This is not perfect, it does not deal with name clashes of eventSourceFolders
// inside a dataset, but is it better than nothing.
String newName = source.getName();
if (m_dataSets.findChild(newName) != null)
{
for (int i=2; ; i++)
{
newName = source.getName()+" ("+i+")";
if (m_dataSets.findChild(newName) == null) break;
}
}
TreeItem t = new TreeItem(newName, "dataset" , DATASET | EVENTSOURCE | CURRENTEVENTSOURCE);
SimpleNode newSource;
if (source instanceof AnnotatedEventSource)
{
boolean hasEventSourceFolders = false;
TreeItem[] items = ((AnnotatedEventSource) source).getItems();
for (int i=0; i= m_analyzer.length)
{
EventAnalyzer[] temp = new EventAnalyzer[m_nAnalyzers*2];
System.arraycopy(m_analyzer,0,temp,0,m_nAnalyzers);
m_analyzer = temp;
}
m_analyzer[m_nAnalyzers++] = ana;
notifyInfo();
}
final public void removeAllEventAnalyzers()
{
// TODO: implement properly
m_nAnalyzers = 0;
notifyInfo();
}
final public void removeEventAnalyzer(EventAnalyzer ana)
{
// TODO: implement
}
public void close() // closes the job
{
setCurrentEventSource(null);
if (m_dataSets != null)
{
Enumeration e = m_dataSets.elements();
while (e.hasMoreElements())
{
EventSource s = (EventSource) e.nextElement();
s.close();
}
}
}
public void loadObject(String name) throws LoaderException
{
try
{
final Class[] noArgc = new Class[0];
setActiveThread();
Class x = m_loader.loadClass(name);
resetFolders();
if (EventGenerator.class.isAssignableFrom(x))
{
Constructor c = x.getConstructor(noArgc);
setEventSource( (EventGenerator) c.newInstance((Object[])noArgc) );
return;
}
else if (EventAnalyzer.class.isAssignableFrom(x))
{
Constructor c = x.getConstructor(noArgc);
addEventAnalyzer( (EventAnalyzer) c.newInstance((Object[])noArgc) );
return;
}
throw new LoaderException("Class "+name+" must extend EventGenerator or EventAnalyzer");
}
catch (InvocationTargetException e)
{
Throwable t = e.getTargetException();
PrintWriter p = new PrintWriter(getErrorStream());
p.println("Error occured during instantiation of "+name);
t.printStackTrace(p);
p.flush();
throw new LoaderException("Error instantiating "+name+" (See Output window for details)",t);
}
catch (NoSuchMethodException e)
{
throw new LoaderException("Class "+name+" must be public and have public constructor with zero arguments",e);
}
catch (IllegalAccessException e)
{
throw new LoaderException("Class "+name+" must be public and have public constructor with zero arguments",e);
}
catch (InstantiationException e)
{
throw new LoaderException("Error creating object of class "+name,e);
}
catch (Throwable e)
{
throw new LoaderException("Could not load class "+name,e);
}
finally
{
setActiveThread(null);
}
}
public void unloadAll()
{
m_nAnalyzers = 0;
if (m_source != null && m_source.getObject() instanceof EventGenerator)
{
EventGenerator gen = (EventGenerator) m_source.getObject();
gen.close();
}
// delete any loaded programs
if (m_programs != null)
{
m_programs.destroy();
m_programs = null;
}
}
final protected void setClassLoader(ClassLoader cl)
{
m_loader = cl;
}
public Object getItem(final TreeItem search)
{
SimpleNode node = m_root.find(search);
if (node == null) throw new NoSuchItemError("Cannot find node "+search);
Object result = node.getObject();
if (result == null) throw new NoSuchItemError("Cannot find object "+search);
return result;
}
protected void setState(String state)
{
m_state = state;
}
final private void beforeFirstEvent()
{
((EventSource) m_source.getObject()).beforeFirstEvent();
for (int i=0; i nextUpdateTime)
{
notifyInfo(m_status.m_eventsAnalyzed);
nextUpdateTime = now+100;
}
data = source.getNextEvent();
processEvent(data);
m_status.m_eventsAnalyzed++;
if (m_stopEventLoop || Thread.interrupted()) break;
}
}
catch (EndOfDataException e)
{
m_status.m_atEndOfData = true;
data = null;
afterLastEvent();
}
}
catch (EventSourceStopped e)
{
// This space intentionally left blank
}
catch (ThreadDeath e)
{
throw e;
}
catch(Throwable e)
{
PrintWriter p = new PrintWriter(getErrorStream());
e.printStackTrace(p);
p.flush();
reportError(new AnalysisException(e.getMessage()));
}
finally
{
long now = System.currentTimeMillis();
goEnded();
m_event = data;
setState("Active");
synchronized (AbstractJob.this) { m_eventLoopThread = null; }
notifyInfo();
long milli = now - startTime;
int events = m_status.m_eventsAnalyzed-startEvents;
sendMessage("Analyzed "+events+" events in "+milli+" milliseconds");
}
}
private int m_maxEvents;
}
}
Ads help maintain this website.