All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jbpt.petri.AbstractRun Maven / Gradle / Ivy

Go to download

The jBPT code library is a compendium of technologies that support research on design, execution, and evaluation of business processes.

The newest version!
package org.jbpt.petri;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Implementation of a run of a net system.
 * 
 * @author Artem Polyvyanyy
 */
public class AbstractRun, N extends INode, P extends IPlace, T extends ITransition, M extends IMarking> 
		extends ArrayList> 
		implements IRun {

	private static final long serialVersionUID = -4466441471941425047L;
	
	protected INetSystem sys = null;
	
	protected M currentMarking = null;
	protected M initialMarking = null;
	protected Set possibleExtensions = null;
	
	public AbstractRun() {};
	
	public AbstractRun(INetSystem sys) {
		this.setNetSystem(sys);
	}
	
	@SuppressWarnings("unchecked")
	private void reset() {
		this.currentMarking = (M) this.initialMarking.clone();
		this.possibleExtensions = new HashSet(this.sys.getEnabledTransitionsAtMarking(this.currentMarking));
	}
	
	@Override
	public Set getPossibleExtensions() {		
		return this.possibleExtensions;
	}
	
	@Override
	public boolean append(T transition) {
		if (this.possibleExtensions.contains(transition)) {
			IStep step = this.createStep(this.sys,this.currentMarking,transition);
			this.currentMarking = step.getOutputMarking();
			this.possibleExtensions.clear();
			this.possibleExtensions.addAll(this.sys.getEnabledTransitionsAtMarking(this.currentMarking));
			return super.add(step);
		}
		else
			return false;
	}
	
	@Override
	public boolean add(IStep arg0) {
		throw new UnsupportedOperationException("Cannot modify runs by adding transitions at arbitrary position.");
	}

	@Override
	public boolean addAll(Collection> arg0) {
		throw new UnsupportedOperationException("Cannot modify runs by adding transitions at arbitrary position.");
	}

	@Override
	public void clear() {
		super.clear();
		this.reset();
	}

	@SuppressWarnings("unchecked")
	@Override
	public IRun clone() {
		AbstractRun run = null;
		try {
			run = AbstractRun.class.newInstance();
		} catch (InstantiationException exception) {
			return null;
		} catch (IllegalAccessException exception) {
			return null;
		}
		
		run.initialMarking = (M) this.initialMarking.clone();
		run.currentMarking = (M) this.initialMarking.clone();
		run.sys = this.sys;
		run.possibleExtensions = new HashSet(run.sys.getEnabledTransitionsAtMarking(run.currentMarking));
		run.copyTransitions(this);
		
		return (IRun)run;
	}
	
	protected void copyTransitions(IRun run) {
		for (IStep step : run) {
			this.append(step.getTransition());
		}
	}

	protected IStep createStep(IPetriNet net, M inputMarking, T transition, M outputMarking) {
		AbstractStep step = new AbstractStep(net, inputMarking, transition, outputMarking);	
		return (IStep)step;
	}
	
	protected IStep createStep(IPetriNet net, M inputMarking, T transition) {
		AbstractStep step = new AbstractStep(net, inputMarking, transition);	
		return (IStep)step;
	}
	
	
	@Override
	public boolean addAll(int arg0, Collection> arg1) {
		throw new UnsupportedOperationException("Cannot modify runs by adding steps at arbitrary position.");
	}
	
	@Override
	public void add(int arg0, IStep arg1) {
		throw new UnsupportedOperationException("Cannot modify runs by adding steps at arbitrary position.");
	}
	
	@Override
	public IStep remove(int arg0) {
		if (arg0==this.size()-1) {
			IStep result = super.remove(arg0);
			this.currentMarking = this.get(this.size()-1).getOutputMarking();
			this.possibleExtensions = new HashSet(this.sys.getEnabledTransitionsAtMarking(this.currentMarking));
			return result;
		}
		else
			throw new UnsupportedOperationException("Cannot remove steps other than from the end of the run.");
	}

	@Override
	public boolean remove(Object arg0) {
		throw new UnsupportedOperationException("Cannot remove steps from runs.");
	}

	@Override
	public boolean removeAll(Collection arg0) {
		throw new UnsupportedOperationException("Cannot remove steps from runs.");
	}

	@Override
	protected void removeRange(int arg0, int arg1) {
		throw new UnsupportedOperationException("Cannot remove steps from runs.");
	}

	@Override
	public boolean retainAll(Collection arg0) {
		throw new UnsupportedOperationException("Cannot modify runs at arbitrary positions.");
	}

	@Override
	public IStep set(int arg0, IStep arg1) {
		throw new UnsupportedOperationException("Cannot modify runs at arbitrary positions.");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setNetSystem(INetSystem system) {
		this.initialMarking = (M) system.getMarking().clone();
		this.sys = system;
		this.reset();
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy