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

org.jbpt.petri.AbstractPetriNet 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.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jbpt.graph.abs.AbstractDirectedGraph;

/**
 * An implementation of IPetriNet interface.
 * 
 * TODO create and extend a bipartite graph.
 * 
 * @author Artem Polyvyanyy
 * @author Matthias Weidlich
 * @author Andreas Meyer
 */
public abstract class AbstractPetriNet, N extends INode, P extends IPlace, T extends ITransition> 
	extends AbstractDirectedGraph implements IPetriNet {
	
	/**
	 * Empty constructor.
	 */
	public AbstractPetriNet(){}
	
	@SuppressWarnings("unchecked")
	@Override
	public F addFlow(P place, T transition) {
		return this.addFlow((N)place,(N)transition);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public F addFlow(T transition, P place) {
		return this.addFlow((N)transition,(N)place);
	}
	
	@Override
	public N addNode(N node) {
		return this.addVertex(node);
	}
	
	@Override
	public Collection addNodes(Collection nodes) {
		Collection result = this.addVertices(nodes);
		return result==null ? new ArrayList() : result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public P addPlace(P place) {
		return this.addVertex((N)place)==null ? null : place;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection

addPlaces(Collection

places) { Collection

result = new ArrayList

(); if (places == null) return result; for (P place : places) if (this.addVertex((N)place) != null) result.add(place); return result; } @SuppressWarnings("unchecked") @Override public T addTransition(T transition) { return this.addVertex((N)transition)==null ? null : transition; } @SuppressWarnings("unchecked") @Override public Collection addTransitions(Collection transitions) { Collection result = new ArrayList(); if (transitions == null) return result; for (T transition : transitions) if (this.addVertex((N)transition) != null) result.add(transition); return result; } @Override public N removeNode(N node) { return this.removeVertex(node); } @Override public Collection removeNodes(Collection nodes) { Collection result = this.removeVertices(nodes); return result==null ? new ArrayList() : result; } @SuppressWarnings("unchecked") @Override public P removePlace(P place) { return this.removeVertex((N)place) == null ? null : place; } @SuppressWarnings("unchecked") @Override public Collection

removePlaces(Collection

places) { Collection

result = new ArrayList

(); if (places == null) return result; for (P place : places) if (this.removeVertex((N)place) != null) result.add(place); return result; } @SuppressWarnings("unchecked") @Override public T removeTransition(T transition) { return this.removeVertex((N)transition) == null ? null : transition; } @SuppressWarnings("unchecked") @Override public Collection removeTransitions(Collection transitions) { Collection result = new ArrayList(); if (transitions == null) return result; for (T transition : transitions) if (this.removeVertex((N)transition) != null) result.add(transition); return result; } @Override public F removeFlow(F flow) { return this.removeEdge(flow); } @Override public Collection removeFlow(Collection flow) { Collection result = this.removeEdges(flow); return result==null ? new ArrayList() : result; } @Override public Set getNodes() { // TODO this.getVertices() must return set. return new HashSet(this.getVertices()); } @SuppressWarnings("unchecked") @Override public Set

getPlaces() { Set

result = new HashSet

(); for (N node : this.getVertices()) if (node instanceof IPlace) result.add((P)node); return result; } @SuppressWarnings("unchecked") @Override public Set getTransitions() { Set result = new HashSet(); for (N node : this.getVertices()) if (node instanceof ITransition) result.add((T)node); return result; } @Override public Set getFlow() { // TODO this.getEdges() must return set. return new HashSet(this.getEdges()); } @SuppressWarnings("unchecked") @Override public Set getSilentTransitions() { Set result = new HashSet(); for (N node : this.getVertices()) if (node instanceof ITransition && node.getLabel().isEmpty()) result.add((T)node); return result; } @SuppressWarnings("unchecked") @Override public Set getObservableTransitions() { Set result = new HashSet(); for (N node : this.getVertices()) if (node instanceof ITransition && !node.getLabel().isEmpty()) result.add((T)node); return result; } @SuppressWarnings("unchecked") @Override public Set

getPostset(T transition) { Set

result = new HashSet

(); for (N node : this.getDirectSuccessors((N)transition)) if (node instanceof IPlace) result.add((P)node); return result; } @SuppressWarnings("unchecked") @Override public Set

getPostsetPlaces(Collection transitions) { Set

result = new HashSet

(); for (T transition : transitions) for (N node : this.getDirectSuccessors((N)transition)) if (node instanceof IPlace) result.add((P)node); return result; } @SuppressWarnings("unchecked") @Override public Set getPostset(P place) { Set result = new HashSet(); for (N node : this.getDirectSuccessors((N)place)) { if (node instanceof ITransition) result.add((T)node); } return result; } @SuppressWarnings("unchecked") @Override public Set getPostsetTransitions(Collection

places) { Set result = new HashSet(); for (P place : places) for (N node : this.getDirectSuccessors((N)place)) if (node instanceof ITransition) result.add((T)node); return result; } @Override public Set getPostset(N node) { // TODO this.getDirectSuccessors(node) must return set. return new HashSet(this.getDirectSuccessors(node)); } @Override public Set getPostset(Collection nodes) { // TODO this.getDirectSuccessors(nodes) must return set. return new HashSet(this.getDirectSuccessors(nodes)); } @SuppressWarnings("unchecked") @Override public Set

getPreset(T transition) { Set

result = new HashSet

(); for (N node : this.getDirectPredecessors((N)transition)) if (node instanceof IPlace) result.add((P)node); return result; } @SuppressWarnings("unchecked") @Override public Set

getPresetPlaces(Collection transitions) { Set

result = new HashSet

(); for (T transition : transitions) for (N node : this.getDirectPredecessors((N)transition)) if (node instanceof IPlace) result.add((P)node); return result; } @SuppressWarnings("unchecked") @Override public Set getPreset(P place) { Set result = new HashSet(); for (N node : this.getDirectPredecessors((N)place)) if (node instanceof ITransition) result.add((T)node); return result; } @SuppressWarnings("unchecked") @Override public Set getPresetTransitions(Collection

places) { Set result = new HashSet(); for (P place : places) for (N node : this.getDirectPredecessors((N)place)) if (node instanceof ITransition) result.add((T)node); return result; } @Override public Set getPreset(N node) { // TODO this.getDirectPredecessors(node) must return set return new HashSet(this.getDirectPredecessors(node)); } @Override public Set getPreset(Collection nodes) { // TODO this.getDirectPredecessors(nodes) must return set return new HashSet(this.getDirectPredecessors(nodes)); } @SuppressWarnings("unchecked") @Override public Set

getSourcePlaces() { Set

result = new HashSet

(); for (N node : this.getSourceNodes()) if (node instanceof IPlace) result.add((P)node); return result; } @SuppressWarnings("unchecked") @Override public Set getSourceTransitions() { Set result = new HashSet(); for (N node : this.getSourceNodes()) if (node instanceof ITransition) result.add((T)node); return result; } @SuppressWarnings("unchecked") @Override public Set

getSinkPlaces() { Set

result = new HashSet

(); for (N node : this.getSinkNodes()) if (node instanceof IPlace) result.add((P)node); return result; } @SuppressWarnings("unchecked") @Override public Set getSinkTransitions() { Set result = new HashSet(); for (N node : this.getSinkNodes()) if (node instanceof ITransition) result.add((T)node); return result; } @Override public Set getMin() { return this.getSourceNodes(); } @Override public Set getMax() { return this.getSinkNodes(); } @Override public Set getSourceNodes() { Set result = new HashSet(); for (N n : this.getNodes()) if (this.getPreset(n).isEmpty()) result.add(n); return result; } @Override public Set getSinkNodes() { Set result = new HashSet(); for (N n : this.getNodes()) if (this.getPostset(n).isEmpty()) result.add(n); return result; } @Override public IPetriNet clone() { return this.clone(new HashMap()); } @SuppressWarnings("unchecked") @Override public IPetriNet clone(Map map) { IPetriNet clone = null; try { clone = (IPetriNet) PetriNet.class.newInstance(); } catch (InstantiationException exception) { return null; } catch (IllegalAccessException exception) { return null; } for (P p : this.getPlaces()) { P np = (P) p.clone(); map.put((N)p,(N)np); clone.addPlace(np); } for (T t : this.getTransitions()) { T nt = (T) t.clone(); map.put((N)t,(N)nt); clone.addTransition(nt); } for (F f : this.getFlow()) { clone.addFlow(map.get(f.getSource()), map.get(f.getTarget())); } return clone; } @Override public String toDOT() { String result = "digraph G {\n"; result += "graph [fontname=\"Helvetica\" fontsize=\"10\" nodesep=\"0.35\" ranksep=\"0.25 equally\"];\n"; result += "node [fontname=\"Helvetica\" fontsize=\"10\" fixedsize=\"true\" style=\"filled\" fillcolor=\"white\" penwidth=\"2\"];\n"; result += "edge [fontname=\"Helvetica\" fontsize=\"10\" arrowhead=\"normal\" color=\"black\"];\n"; result += "\n"; result += "node [shape=circle];\n"; for (P place : this.getPlaces()) { result += String.format("\tn%s[label=\"%s\" width=\".3\" height=\".3\"];\n", place.getId().replace("-", ""), place.getLabel()); } result += "\n"; result += "node [shape=box];\n"; for (T transition : this.getTransitions()) { if (transition.isSilent()) result += String.format("\tn%s[label=\"\" width=\".3\" height=\".1\"];\n", transition.getId().replace("-", "")); else result += String.format("\tn%s[label=\"%s\" width=\".3\" height=\".3\"];\n", transition.getId().replace("-", ""), transition.getLabel()); } result += "\n"; for (F flow: this.getFlow()) { result += String.format("\tn%s->n%s;\n", flow.getSource().getId().replace("-", ""), flow.getTarget().getId().replace("-", "")); } result += "}\n"; return result; } @SuppressWarnings("unchecked") @Override public T createTransition() { T t = null; try { t = (T) Transition.class.newInstance(); return t; } catch (InstantiationException exception) { return t; } catch (IllegalAccessException exception) { return t; } } @SuppressWarnings("unchecked") @Override public P createPlace() { P p = null; try { p = (P) Place.class.newInstance(); return p; } catch (InstantiationException exception) { return p; } catch (IllegalAccessException exception) { return p; } } @Override public void clear() { this.removeVertices(this.getVertices()); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy