package pipe.dataLayer;
// Collections
import java.lang.Object;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Hashtable;
// Parser & XSLT
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.dom.DOMSource;
import java.io.FileOutputStream;
import java.lang.StringBuffer;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.crimson.tree.XmlDocument;
import javax.xml.transform.stream.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
// DOM
import org.w3c.dom.DocumentType;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Attr;
import org.w3c.dom.Text;
import org.w3c.dom.DOMImplementation;
// Exceptions
import java.io.IOException;
import java.io.FileNotFoundException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;
import org.w3c.dom.DOMException;
import java.io.UnsupportedEncodingException;
import java.util.Observable; // Model View Controller Design Pattern
import java.util.Random; // For random number generation
/**
* DataLayer - Encapsulates entire Petri-Net, also contains functions to perform calculations
*
* @see
PNML - Petri-Net XMLSchema (stNet.xsd)
* @see
DataLayer UML
* @version 1.0
* @author James D Bloom
*/
public class DataLayer extends Observable implements pipe.gui.Constants{
private static Random randomNumber = new Random(); // Random number generator
/** PNML File Name */
private String pnmlName = null;
/** List containing all the Place objects in the Petri-Net */
private ArrayList placesArray = null;
/** ArrayList containing all the Transition objects ing2D.setStroke(new BasicStroke(1.5f)); the Petri-Net */
private ArrayList transitionsArray = null;
/** ArrayList containing all the Arc objects in the Petri-Net */
private ArrayList arcsArray = null;
/** ArrayList containing all the Token objects in the Petri-Net */
private ArrayList tokensArray = null;
/** ArrayList containing all the Arrow objects in the Petri-Net */
private ArrayList arrowsArray = null;
/** An ArrayList used to point to either the Arc, Place or Transition ArrayLists when these ArrayLists are being update */
private ArrayList changeArrayList = null;
/** Initial Markup Matrix */
private int[] initialMarkupMatrix = null;
/** Initial Markup Matrix */
private int[] currentMarkupMatrix = null;
/** Markup Matrix Storage used during animation */
private int[] markupMatrixAnimationStorage = null;
/** Markup Matrix Storage */
private Object[] markupStore = {null, null, null, null, null, null, null, null, null, null};
/** Markup Matrix Storage Position */
private int position = 0;
/** Markup Storage Array */
private Matrix markupHistoryMatrix = null;
/** Foward Incidence Matrix */
private Matrix fowardsIncidenceMatrix = null;
/** Backward Incidence Matrix */
private Matrix backwardsIncidenceMatrix = null;
/** Incidence Matrix */
private Matrix incidenceMatrix = null;
/** X-Axis Scale Value */
private final int DISPLAY_SCALE_FACTORX = 7; // Scale factors for loading other Petri-Nets (not yet implemented)
/** Y-Axis Scale Value */
private final int DISPLAY_SCALE_FACTORY = 7; // Scale factors for loading other Petri-Nets (not yet implemented)
/** X-Axis Shift Value */
private final int DISPLAY_SHIFT_FACTORX = 270; // Scale factors for loading other Petri-Nets (not yet implemented)
/** Y-Axis Shift Value */
private final int DISPLAY_SHIFT_FACTORY = 120; // Scale factors for loading other Petri-Nets (not yet implemented)
/** Hashtable which maps PlaceTransitionObjects to their list of connected arcs */
private Hashtable arcsMap = null;
/**
* Create Petri-Net object from PNML file with URI pnmlFileName
*
*
* @param pnmlFileName Name of PNML File
* @throws IOException
* @throws SAXException
* @throws TransformerException
* @throws ParserConfigurationException
*/
public DataLayer(String pnmlFileName)
throws IOException, SAXException, TransformerException, ParserConfigurationException {
initializeMatrixes();
try {
loadPNML(pnmlFileName);
} catch (IOException e) {
System.out.println("IOException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
} catch (SAXException e) {
System.out.println("SAXException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
} catch (TransformerException e) {
System.out.println("TransformerException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException thrown in dataLayer(String pnmlFileName) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
}
}
/**
* Create Petri-Net object from pnmlFile
*
* @param pnmlFile PNML File
* @throws IOException
* @throws SAXException
* @throws TransformerException
* @throws ParserConfigurationException
*/
public DataLayer(File pnmlFile)
throws IOException, SAXException, TransformerException, ParserConfigurationException {
initializeMatrixes();
try {
loadPNML(pnmlFile.getAbsolutePath());
} catch (IOException e) {
System.out.println("IOException thrown in dataLayer(File pnmlFile) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
} catch (SAXException e) {
System.out.println("SAXException thrown in dataLayer(File pnmlFile) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
} catch (TransformerException e) {
System.out.println("TransformerException thrown in dataLayer(File pnmlFile) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException thrown in dataLayer(File pnmlFile) : dataLayer Class : dataLayer Package");
e.printStackTrace(System.err);
}
}
/**
* Create empty Petri-Net object
*/
public DataLayer() {
initializeMatrixes();
}
/**
* Initialize Arrays
*/
private void initializeMatrixes() {
placesArray = new ArrayList();
transitionsArray = new ArrayList();
arcsArray = new ArrayList();
tokensArray = new ArrayList();
arrowsArray = new ArrayList();
initialMarkupMatrix = null;
fowardsIncidenceMatrix = null;
backwardsIncidenceMatrix = null;
incidenceMatrix = null;
// may as well do the hashtable here as well
arcsMap = new Hashtable();
}
/**
* Function populates the arcsMap hashtable enabling easier cross referencing of places, transitions and the arcs connected to them.
*
*/
public void setArcConnectionMap() {
// map (PTO, arcslist)
// get all the PlaceTransitionObjects
ArrayList allPTO = new ArrayList(placesArray);
allPTO.addAll(transitionsArray);
Iterator pto;
Iterator arcs;
PlaceTransitionObject current ;
pto = allPTO.iterator();
while (pto.hasNext()) {
current = (PlaceTransitionObject)pto.next();
// make an entry for each pto in our mapping structure
arcsMap.put(current,new ArrayList());
}
arcs = arcsArray.iterator();
Arc currentArc;
PlaceTransitionObject source;
PlaceTransitionObject target;
ArrayList arcslist = null;
// iterate over the arcs, getting the source and destination ptos and adding the arc to their lists
while (arcs.hasNext()) {
currentArc = (Arc)arcs.next();
source = currentArc.getSource();
target = currentArc.getTarget();
// get the list of arcs attached to the source and target and add the arc to them
try {
((ArrayList)arcsMap.get(source)).add(currentArc);
} catch (NullPointerException ne1) {
// System.out.println("Populating arcsMap: " + ne1.toString());
}
try {
((ArrayList)arcsMap.get(target)).add(currentArc);
} catch (NullPointerException ne2) {
// System.out.println("Populating arcsMap: " + ne2.toString());
}
}
}
/**
* Set the ArrayList of Place Objects with an ArrayList
*
* @param placesInput Input ArrayList of Place objects
*/
/*
public void setPlaces(ArrayList placesInput) {
placesArray = placesInput;
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < placesArray.size() ; i++) {
//notifyObservers(((Place)placesArray.get(i)).getBounds());
notifyObservers((Place)placesArray.get(i));
}
}
*/
/**
* Set the ArrayList of Place Objects with an Array
*
* @param placesInput Input Array of Place objects (i.e Place[])
*/
/*
public void setPlaces(Place placesInput[]) {
placesArray = new ArrayList();
for(int i = 0 ; i < placesInput.length ; i++)
placesArray.add(i, placesInput);
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < placesArray.size() ; i++) {
// notifyObservers(((Place)placesArray.get(i)).getBounds());
notifyObservers((Place)placesArray.get(i));
}
}
*/
/**
* Set the ArrayList of Transition Objects with an ArrayList
*
* @param transitionsInput Input ArrayList of Transition objects
*/
/*
public void setTransitions(ArrayList transitionsInput) {
transitionsArray = transitionsInput;
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < transitionsArray.size() ; i++)
if(transitionsArray.get(i) instanceof Transition) {
//notifyObservers(((Transition)transitionsArray.get(i)).getBounds());
notifyObservers((Transition)transitionsArray.get(i));
}
}
*/
/**
* Set the ArrayList of Transition Objects with an Array
*
* @param transitionsInput Input Array of Transition objects (i.e Transition[])
*/
/*
public void setTransitions(Transition transitionsInput[]) {
for(int i = 0 ; i < transitionsInput.length ; i++)
transitionsArray.add(i, transitionsInput);
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < transitionsArray.size() ; i++)
if(transitionsArray.get(i) instanceof Transition) {
// notifyObservers(((Transition)transitionsArray.get(i)).getBounds());
notifyObservers((Transition)transitionsArray.get(i));
}
}
*/
/**
* Set the ArrayList of Arc Objects with an ArrayList
*
* @param arcsInput Input ArrayList of Arc objects
*/
/*
public void setArcs(ArrayList arcsInput) {
arcsArray = arcsInput;
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < arcsArray.size() ; i++)
if(arcsArray.get(i) instanceof Arc){
//notifyObservers(((Arc)arcsArray.get(i)).getBounds());
notifyObservers((Arc)arcsArray.get(i));
}
}
*/
/**
* Set the ArrayList of Arc Objects with an Array
*
* @param arcsInput Input Array of Arc objects (i.e Arc[])
*/
/*
public void setArcs(Arc arcsInput[]) {
for(int i = 0 ; i < arcsInput.length ; i++)
arcsArray.add(i, arcsInput);
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < arcsArray.size() ; i++)
if(arcsArray.get(i) instanceof Arc) {
// notifyObservers(((Arc)arcsArray.get(i)).getBounds());
notifyObservers((Arc)arcsArray.get(i));
}
}
*/
/**
* Set the ArrayList of Token Objects with an Array
*
* @param tokensInput Input Array of Token objects (i.e Token[])
*/
/*
public void setTokens(Token tokensInput[]){
for(int i = 0; i < tokensInput.length; i++)
tokensArray.add(i, tokensInput);
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < tokensArray.size() ; i++)
if(tokensArray.get(i) instanceof Token) {
// notifyObservers(((Token)tokensArray.get(i)).getBounds());
notifyObservers((Token)tokensArray.get(i));
}
}
*/
/**
* Set the ArrayList of Token Objects with an ArrayList
*
* @param tokensInput Input ArrayList of Token objects
*/
/*
public void setTokens(ArrayList tokensInput) {
tokensArray = tokensInput;
// Update Petri-Net visual representation
setChanged();
for(int i = 0 ; i < tokensArray.size() ; i++)
if(tokensArray.get(i) instanceof Token) {
//notifyObservers(((Token)tokensArray.get(i)).getBounds());
notifyObservers((Token)tokensArray.get(i));
}
}
*/
/**
* Replace any Place that has an id equal to idInput with placeInput
*
* @param idInput id of Place object to overwrite in Petri-Net
* @param placeInput New Place object
*/
/*
public void setPlace(Place placeInput, String idInput) {
for(int i = 0 ; i < placesArray.size(); i++)
if(idInput.equals(((Place)placesArray.get(i)).getId())) {
placesArray.set(i, placeInput);
setChanged();
// notifyObservers(placeInput.getBounds());
notifyObservers(placeInput);
}
}
*/
/**
* Replace any Transition that has an id equal to idInput with transitionInput
*
* @param idInput id of Transition object to overwrite in Petri-Net
* @param transitionInput New Transition object
*/
/*
public void setTransition(Transition transitionInput, String idInput) {
for(int i = 0 ; i < transitionsArray.size(); i++)
if(idInput.equals(((Transition)transitionsArray.get(i)).getId())) {
transitionsArray.set(i, transitionInput);
setChanged();
// notifyObservers(transitionInput.getBounds());
notifyObservers(transitionInput);
}
}
*/
/**
* Replace any Arc that has an id equal to idInput with arcInput
*
* @param idInput id of Arc object to overwrite in Petri-Net
* @param arcInput New Arc object
*/
/*
public void setArc(Arc arcInput, String idInput) {
for(int i = 0 ; i < arcsArray.size(); i++)
if(idInput.equals(((Arc)arcsArray.get(i)).getId())) {
arcsArray.set(i, arcInput);
setChanged();
// notifyObservers(arcInput.getBounds());
notifyObservers(arcInput);
}
}
*/
/**
* Remove first Place that has an id equal to idInput
*
* @param idInput id of Place object to remove from Petri-Net
*/
public void removePlace(String idInput) {
for(int i = 0 ; i < placesArray.size(); i++)
if(idInput.equals(((Place)placesArray.get(i)).getId())) {
placesArray.remove(i);
setChanged();
}
}
/**
* Remove first Transition that has an id equal to idInput
*
* @param idInput id of Transition object to remove from Petri-Net
*/
public void removeTransition(String idInput) {
for(int i = 0 ; i < transitionsArray.size(); i++)
if(idInput.equals(((Transition)transitionsArray.get(i)).getId())) {
transitionsArray.remove(i);
setChanged();
}
}
/**
* Remove first Arc that has an id equal to idInput
*
* @param idInput id of Arc object to remove from Petri-Net
*/
public void removeArc(String idInput) {
for(int i = 0 ; i < arcsArray.size(); i++)
if(idInput.equals(((Arc)arcsArray.get(i)).getId())) {
arcsArray.remove(i);
setChanged();
}
}
/**
* Add placeInput to the back of the Place ArrayList
* All observers are notified of this change (Model-View Architecture)
*
* @param placeInput Place Object to add
*/
public void addPlace(Place placeInput) {
boolean unique = true;
if(placeInput != null) {
if(placeInput.getId() != null && placeInput.getId().length() > 0) {
for(int i = 0 ; i < placesArray.size() ; i++) {
if(placeInput.getId().equals(((Place)placesArray.get(i)).getId())) {
unique = false;
}
}
} else {
String id = null;
if(placesArray != null && placesArray.size() > 0) {
int no = placesArray.size();
// id = "P" + no;
do {
// System.out.println("in while loop");
for(int i = 0 ; i < placesArray.size() ; i++) {
id = "P" + no;
if(placesArray.get(i) != null) {
if(id.equals(((Place)placesArray.get(i)).getId())) {
// System.out.println("testing id: " + id);
unique = false;
no++;
} else {
unique = true;
}
}
}
} while(!unique);
} else {
id = "P0";
}
if(id != null) {
placeInput.setId(id);
} else {
placeInput.setId("error");
}
}
placesArray.add(placeInput);
setChanged();
// notifyObservers(placeInput.getBounds());
notifyObservers(placeInput);
}
}
/**
* Add transitionInput to back of the Transition ArrayList
* All observers are notified of this change (Model-View Architecture)
*
* @param transitionInput Transition Object to add
*/
public void addTransition(Transition transitionInput) {
boolean unique = true;
if(transitionInput != null) {
if(transitionInput.getId() != null && transitionInput.getId().length() > 0) {
for(int i = 0 ; i < transitionsArray.size() ; i++) {
if(transitionInput.getId().equals(((Transition)transitionsArray.get(i)).getId())) {
unique = false;
}
}
} else {
String id = null;
if(transitionsArray != null && transitionsArray.size() > 0) {
int no = transitionsArray.size();
do {
// System.out.println("transition while loop");
for(int i = 0 ; i < transitionsArray.size() ; i++) {
id = "T" + no;
if(transitionsArray.get(i) != null) {
if(id.equals(((Transition)transitionsArray.get(i)).getId())) {
unique = false;
no++;
} else {
unique = true;
}
}
}
} while(!unique);
} else {
id = "T0";
}
if(id != null) {
transitionInput.setId(id);
} else {
transitionInput.setId("error");
}
}
transitionsArray.add(transitionInput);
setChanged();
// notifyObservers(transitionInput.getBounds());
notifyObservers(transitionInput);
}
}
/**
* Add arcInput to back of the Arc ArrayList
* All observers are notified of this change (Model-View Architecture)
*
* @param arcInput Arc Object to add
*/
public void addArc(Arc arcInput) {
boolean unique = true;
if(arcInput != null) {
if(arcInput.getId() != null && arcInput.getId().length() > 0) {
for(int i = 0 ; i < arcsArray.size() ; i++) {
if(arcInput.getId().equals(((Arc)arcsArray.get(i)).getId())) {
unique = false;
}
}
} else {
String id = null;
if(arcsArray != null && arcsArray.size() > 0) {
int no = arcsArray.size();
do {
for(int i = 0 ; i < arcsArray.size() ; i++) {
id = "A" + no;
if(arcsArray.get(i) != null) {
if(id.equals(((Arc)arcsArray.get(i)).getId())) {
unique = false;
no++;
} else {
unique = true;
}
}
}
} while(!unique);
} else {
id = "A0";
}
if(id != null) {
arcInput.setId(id);
} else {
arcInput.setId("error");
}
}
arcsArray.add(arcInput);
addArcToArcsMap(arcInput);
setChanged();
//notifyObservers(arcInput.getBounds());
notifyObservers(arcInput);
}
}
/** Update the arcsMap hashtable to reflect the new arc
* @param arcInput New Arc
* */
private void addArcToArcsMap(Arc arcInput) {
// now we want to add the arc to the list of arcs for it's source and target
PlaceTransitionObject source = arcInput.getSource();
PlaceTransitionObject target = arcInput.getTarget();
ArrayList newList = null;
if (source != null) {
source.setMovable(false);
if (arcsMap.get(source)!=null) {
// System.out.println("adding arc to existing list");
((ArrayList)arcsMap.get(source)).add(arcInput);
} else {
// System.out.println("creating new arc list");
newList = new ArrayList();
newList.add(arcInput);
arcsMap.put(source,newList);
}
}
if (target != null) {
target.setMovable(false);
if (arcsMap.get(target)!=null) {
// System.out.println("adding arc to existing list2");
((ArrayList)arcsMap.get(target)).add(arcInput);
} else {
// System.out.println("creating new arc list2");
newList = new ArrayList();
newList.add(arcInput);
arcsMap.put(target,newList);
}
}
// System.out.println("arcsMap size: " + arcsMap.size());
}
/**
* Add tokenInput to the back of the Token ArrayList
* All observers are notified of this change.
*
* @param tokenInput Token Object to add
*/
public void addToken(Token tokenInput) {
tokensArray.add(tokenInput);
setChanged();
// notifyObservers(tokenInput.getBounds());
notifyObservers(tokenInput);
}
public void addArrow(ArrowLabel arrowInput) {
arrowsArray.add(arrowInput);
setChanged();
notifyObservers(arrowInput);
}
/**
* Add any PetriNetObject - the object will be added to the appropriate list.
* If the object passed in isn't a Transition, Place or Arc nothing will happen.
* All observers are notified of this change.
* @param pnObject The PetriNetObject to be added.
*/
public void addPetriNetObject(PetriNetObject pnObject) {
if (setPetriNetObjectArrayList(pnObject)) {
if (pnObject instanceof Arc) {
addArcToArcsMap((Arc)pnObject);
addArc((Arc)pnObject);
} else if (pnObject instanceof Place) {
addPlace((Place)pnObject);
} else if (pnObject instanceof Transition) {
addTransition((Transition)pnObject);
} else { // arrows, other labels.
changeArrayList.add(pnObject);
setChanged();
notifyObservers(pnObject);
}
}
// we reset to null so that the wrong ArrayList can't get added to
changeArrayList = null;
}
/**
* Removes the specified object from the appropriate ArrayList of objects.
* All observers are notified of this change.
* @param pnObject The PetriNetObject to be removed.
*/
public void removePetriNetObject(PetriNetObject pnObject){
boolean isRemoved = false;
ArrayList attachedArcs = null;
Iterator i = null;
//System.out.println("removing: " + pnObject.getClass().getName());
if (setPetriNetObjectArrayList(pnObject)) {
isRemoved = changeArrayList.remove(pnObject);
// nb we want to remove all attached arcs also
if (pnObject instanceof PlaceTransitionObject) {
// System.out.println("pnObject is instanceof PlaceTransitionObject.");
if ( (ArrayList)arcsMap.get(pnObject) != null) {
// get the list of attached arcs for the object we are removing
attachedArcs = ((ArrayList)arcsMap.get(pnObject));
// remove the object we are removing from the arcsMap
// System.out.println("size of list was:" + ((ArrayList)arcsMap.get(pnObject)).size());
arcsMap.remove(pnObject);
i = attachedArcs.iterator();
Arc removeArc;
ArrowLabel removeArrow;
PlaceTransitionObject attached;
// iterate over all the attached arcs, and remove each it and its arrow
while(i.hasNext()) {
removeArc = (Arc)i.next();
removeArrow = removeArc.getArrow();
removePetriNetObject(removeArc);
}
}
} else if (pnObject instanceof Arc) {
// get source and target of the arc
PlaceTransitionObject connected[] = {((Arc)pnObject).getSource(),((Arc)pnObject).getTarget()};
// If this has no other arcs attached to it we allow it to move
ArrayList secondaryList;
for (int a=0;a 0)
positionXInput = Double.valueOf(positionXTempStorage).doubleValue()*(false ? DISPLAY_SCALE_FACTORX : 1)+(false ? DISPLAY_SHIFT_FACTORX : 1);
if (positionYTempStorage.length() > 0)
positionYInput = Double.valueOf(positionYTempStorage).doubleValue()*(false ? DISPLAY_SCALE_FACTORY : 1)+(false ? DISPLAY_SHIFT_FACTORY : 1);
if (idTempStorage.length() > 0)
idInput = idTempStorage;
else if (nameTempStorage.length() > 0)
idInput = nameTempStorage;
if (nameTempStorage.length() > 0)
nameInput = nameTempStorage;
else if (idTempStorage.length() > 0)
nameInput = idTempStorage;
if (nameOffsetYTempStorage.length() > 0)
nameOffsetXInput = Double.valueOf(nameOffsetYTempStorage).doubleValue();
if (nameOffsetXTempStorage.length() > 0)
nameOffsetYInput = Double.valueOf(nameOffsetXTempStorage).doubleValue();
if (initialMarkingTempStorage.length() > 0)
initialMarkingInput = Integer.valueOf(initialMarkingTempStorage).intValue();
if (markingOffsetXTempStorage.length() > 0)
markingOffsetXInput = Double.valueOf(markingOffsetXTempStorage).doubleValue();
if (markingOffsetYTempStorage.length() > 0)
markingOffsetYInput = Double.valueOf(markingOffsetYTempStorage).doubleValue();
return new Place(positionXInput, positionYInput, idInput, nameInput, nameOffsetXInput, nameOffsetYInput, initialMarkingInput, markingOffsetXInput, markingOffsetYInput, DEFAULT_ELEMENT_COLOR);
}
/**
* Creates a Transition object from a Transition DOM Element
*
* @param inputTransitionElement Input Transition DOM Element
* @return Transition Object
*/
private Transition createTransition(Element inputTransitionElement){
double positionXInput = 0;
double positionYInput = 0;
String idInput = null;
String nameInput = null;
double nameOffsetYInput = 0;
double nameOffsetXInput = 0;
String positionXTempStorage = inputTransitionElement.getAttribute("positionX");
String positionYTempStorage = inputTransitionElement.getAttribute("positionY");
String idTempStorage = inputTransitionElement.getAttribute("id");
String nameTempStorage = inputTransitionElement.getAttribute("name");
String nameOffsetYTempStorage = inputTransitionElement.getAttribute("nameOffsetX");
String nameOffsetXTempStorage = inputTransitionElement.getAttribute("nameOffsetY");
if (positionXTempStorage.length() > 0)
positionXInput = Double.valueOf(positionXTempStorage).doubleValue()*(false ? DISPLAY_SCALE_FACTORX : 1)+(false ? DISPLAY_SHIFT_FACTORX : 1);
if (positionYTempStorage.length() > 0)
positionYInput = Double.valueOf(positionYTempStorage).doubleValue()*(false ? DISPLAY_SCALE_FACTORY : 1)+(false ? DISPLAY_SHIFT_FACTORY : 1);
if (idTempStorage.length() > 0)
idInput = idTempStorage;
else if (nameTempStorage.length() > 0)
idInput = nameTempStorage;
if (nameTempStorage.length() > 0)
nameInput = nameTempStorage;
else if (idTempStorage.length() > 0)
nameInput = idTempStorage;
if (nameOffsetXTempStorage.length() > 0)
nameOffsetXInput = Double.valueOf(nameOffsetXTempStorage).doubleValue();
if (nameOffsetYTempStorage.length() > 0)
nameOffsetYInput = Double.valueOf(nameOffsetYTempStorage).doubleValue();
return new Transition(positionXInput, positionYInput, idInput, nameInput, nameOffsetXInput, nameOffsetYInput, DEFAULT_ELEMENT_COLOR);
}
/**
* Creates a Arc object from a Arc DOM Element
*
* @param inputArcElement Input Arc DOM Element
* @return Arc Object
*/
private Arc createArc(Element inputArcElement){
String idInput = null;
String sourceInput = null;
String targetInput = null;
int wieghtingInput = 1;
double inscriptionOffsetXInput = 0;
double inscriptionOffsetYInput = 0;
double startX = 0;
double startY = 0;
double endX = 0;
double endY = 0;
sourceInput = inputArcElement.getAttribute("source");
targetInput = inputArcElement.getAttribute("target");
String idTempStorage = inputArcElement.getAttribute("id");
String sourceTempStorage = inputArcElement.getAttribute("source");
String targetTempStorage = inputArcElement.getAttribute("target");
String inscriptionTempStorage = inputArcElement.getAttribute("inscription");
String inscriptionOffsetXTempStorage = inputArcElement.getAttribute("inscriptionOffsetX");
String inscriptionOffsetYTempStorage = inputArcElement.getAttribute("inscriptionOffsetY");
if (idTempStorage.length() > 0)
idInput = idTempStorage;
if (sourceTempStorage.length() > 0)
sourceInput = sourceTempStorage;
if (targetTempStorage.length() > 0)
targetInput = targetTempStorage;
if (inscriptionTempStorage.length() > 0)
wieghtingInput = Integer.valueOf((inputArcElement.getAttribute("inscription") != null ? inputArcElement.getAttribute("inscription") : "1")).intValue();
if (inscriptionOffsetXTempStorage.length() > 0)
inscriptionOffsetXInput = Double.valueOf(inputArcElement.getAttribute("inscriptionOffsetX")).doubleValue();
if (inscriptionOffsetYTempStorage.length() > 0)
inscriptionOffsetYInput = Double.valueOf(inputArcElement.getAttribute("inscriptionOffsetY")).doubleValue();
if (sourceInput.length() > 0) {
if (getPlaceTransitionObject(sourceInput) != null) {
// System.out.println("PNMLDATA: sourceInput is not null");
startX = getPlaceTransitionObject(sourceInput).getPositionX();
startY = getPlaceTransitionObject(sourceInput).getPositionY();
}
}
if (targetInput.length() > 0) {
if (getPlaceTransitionObject(targetInput) != null) {
// System.out.println("PNMLDATA: targetInput is not null");
endX = getPlaceTransitionObject(targetInput).getPositionX();
endY = getPlaceTransitionObject(targetInput).getPositionY();
}
}
return new Arc(startX, startY, endX, endY, idInput, getPlaceTransitionObject(sourceInput), getPlaceTransitionObject(targetInput), wieghtingInput, inscriptionOffsetXInput, inscriptionOffsetYInput, DEFAULT_ELEMENT_COLOR);
}
/**
* Creates a Place Element for a PNML Petri-Net DOM
*
* @param inputPlace Input Place
* @param document Any DOM to enable creation of Elements and Attributes
* @return Place Element for a PNML Petri-Net DOM
*/
private Element createPlaceElement(Place inputPlace, Document document){
Element placeElement = null;
if(document != null) {
placeElement = document.createElement("place");
}
if(inputPlace != null ) {
Integer attrValue = null;
Double positionXInput = inputPlace.getPositionXObject();
Double positionYInput = inputPlace.getPositionYObject();
String idInput = inputPlace.getId();
String nameInput = inputPlace.getName();
Double nameOffsetYInput = inputPlace.getNameOffsetXObject();
Double nameOffsetXInput = inputPlace.getNameOffsetXObject();
Integer initialMarkingInput = inputPlace.getCurrentMarkingObject();
Double markingOffsetXInput = inputPlace.getMarkingOffsetXObject();
Double markingOffsetYInput = inputPlace.getMarkingOffsetYObject();
placeElement.setAttribute("positionX", (positionXInput != null ? String.valueOf(positionXInput) : ""));
placeElement.setAttribute("positionY", (positionYInput != null ? String.valueOf(positionYInput) : ""));
placeElement.setAttribute("name", (nameInput != null ? nameInput : (idInput != null && idInput.length() > 0? idInput : "")));
placeElement.setAttribute("id", (idInput != null ? idInput : "error"));
placeElement.setAttribute("nameOffsetX", (nameOffsetXInput != null ? String.valueOf(nameOffsetXInput) : ""));
placeElement.setAttribute("nameOffsetY", (nameOffsetYInput != null ? String.valueOf(nameOffsetYInput) : ""));
placeElement.setAttribute("initialMarking", (initialMarkingInput != null ? String.valueOf(initialMarkingInput) : "0"));
placeElement.setAttribute("markingOffsetX", (markingOffsetXInput != null ? String.valueOf(markingOffsetXInput) : ""));
placeElement.setAttribute("markingOffsetY", (markingOffsetYInput != null ? String.valueOf(markingOffsetYInput) : ""));
}
return placeElement;
}
/**
* Creates a Transition Element for a PNML Petri-Net DOM
*
* @param inputTransition Input Transition
* @param document Any DOM to enable creation of Elements and Attributes
* @return Transition Element for a PNML Petri-Net DOM
*/
private Element createTransitionElement(Transition inputTransition, Document document){
Element transitionElement = null;
if(document != null) {
transitionElement = document.createElement("transition");
}
if(inputTransition != null ) {
Integer attrValue = null;
Double positionXInput = inputTransition.getPositionXObject();
Double positionYInput = inputTransition.getPositionYObject();
String idInput = inputTransition.getId();
String nameInput = inputTransition.getName();
Double nameOffsetXInput = inputTransition.getNameOffsetXObject();
Double nameOffsetYInput = inputTransition.getNameOffsetYObject();
transitionElement.setAttribute("positionX", (positionXInput != null ? String.valueOf(positionXInput) : ""));
transitionElement.setAttribute("positionY", (positionYInput != null ? String.valueOf(positionYInput) : ""));
transitionElement.setAttribute("name", (nameInput != null ? nameInput : (idInput != null && idInput.length() > 0? idInput : "")));
transitionElement.setAttribute("id", (idInput != null ? idInput : "error"));
transitionElement.setAttribute("nameOffsetX", (nameOffsetXInput != null ? String.valueOf(nameOffsetXInput) : ""));
transitionElement.setAttribute("nameOffsetY", (nameOffsetYInput != null ? String.valueOf(nameOffsetYInput) : ""));
}
return transitionElement;
}
/**
* Creates a Arc Element for a PNML Petri-Net DOM
*
* @param inputArc Input Arc
* @param document Any DOM to enable creation of Elements and Attributes
* @return Arc Element for a PNML Petri-Net DOM
*/
private Element createArcElement(Arc inputArc, Document document){
Element arcElement = null;
if(document != null) {
arcElement = document.createElement("arc");
}
if(inputArc != null ) {
Integer attrValue = null;
Double positionXInput = inputArc.getStartPositionXObject();
Double positionYInput = inputArc.getStartPositionYObject();
String idInput = inputArc.getId();
String sourceInput = inputArc.getSource().getId();
String targetInput = inputArc.getTarget().getId();
int inscriptionInput = (inputArc != null ? inputArc.getWeighting() : 1);
Double inscriptionPositionXInput = inputArc.getInscriptionOffsetYObject();
Double inscriptionPositionYInput = inputArc.getInscriptionOffsetYObject();
arcElement.setAttribute("positionX", (positionXInput != null ? String.valueOf(positionXInput) : ""));
arcElement.setAttribute("positionY", (positionYInput != null ? String.valueOf(positionYInput) : ""));
arcElement.setAttribute("id", (idInput != null ? idInput : "error"));
arcElement.setAttribute("source", (sourceInput != null ? sourceInput : ""));
arcElement.setAttribute("target", (targetInput != null ? targetInput : ""));
arcElement.setAttribute("inscription", new Integer(inscriptionInput).toString());
arcElement.setAttribute("inscriptionOffsetX", (inscriptionPositionXInput != null ? String.valueOf(inscriptionPositionXInput) : ""));
arcElement.setAttribute("inscriptionOffsetY", (inscriptionPositionYInput != null ? String.valueOf(inscriptionPositionYInput) : ""));
}
return arcElement;
}
/**
* Creates all Petri-Net Matrixes from current Petri-Net
*
*/
private void createMatrixes(){
createFowardIncidenceMatrix();
createBackwardsIncidenceMatrix();
createIncidenceMatrix();
createInitialMarkupMatrix();
createCurrentMarkupMatrix();
}
/**
* Creates Foward Incidence Matrix from current Petri-Net
*
*/
private void createFowardIncidenceMatrix(){
int placeNo = 0, transitionNo = 0;
int placeSize = placesArray.size();
int transitionSize = transitionsArray.size();
Arc arc = null;
Place place = null;
Transition transition = null;
fowardsIncidenceMatrix = new Matrix(placeSize, transitionSize);
fowardsIncidenceMatrix.setToZero();
for(int i = 0 ; i < arcsArray.size() ; i++) {
arc = (Arc)arcsArray.get(i);
if(arc != null) {
PetriNetObject pnObject = arc.getTarget();
if(pnObject != null){
if(pnObject instanceof Place) {
place = (Place)pnObject;
pnObject = arc.getSource();
if(pnObject != null){
if(pnObject instanceof Transition){
transition = (Transition)pnObject;
transitionNo = getListPosition(transition);
placeNo = getListPosition(place);
fowardsIncidenceMatrix.set(placeNo, transitionNo, arc.getWeighting());
}
}
}
}
}
}
}
/**
* Creates Backwards Incidence Matrix from current Petri-Net
*
*/
private void createBackwardsIncidenceMatrix(){
int placeNo = 0, transitionNo = 0;
int placeSize = placesArray.size();
int transitionSize = transitionsArray.size();
Arc arc = null;
Place place = null;
Transition transition = null;
backwardsIncidenceMatrix = new Matrix(placeSize, transitionSize);
backwardsIncidenceMatrix.setToZero();
for(int i = 0 ; i < arcsArray.size() ; i++) {
arc = (Arc)arcsArray.get(i);
if(arc != null) {
PetriNetObject pnObject = arc.getSource();
if(pnObject != null){
if(pnObject instanceof Place) {
place = (Place)pnObject;
pnObject = arc.getTarget();
if(pnObject != null){
if(pnObject instanceof Transition){
transition = (Transition)pnObject;
transitionNo = getListPosition(transition);
placeNo = getListPosition(place);
backwardsIncidenceMatrix.set(placeNo, transitionNo, arc.getWeighting());
}
}
}
}
}
}
}
/**
* Creates Incidence Matrix from current Petri-Net
*
*/
private void createIncidenceMatrix(){
createFowardIncidenceMatrix();
createBackwardsIncidenceMatrix();
incidenceMatrix = new Matrix(fowardsIncidenceMatrix.getIntArray());
incidenceMatrix.minus(backwardsIncidenceMatrix);
}
/**
* Creates Initial Markup Matrix from current Petri-Net
*
*/
private void createInitialMarkupMatrix(){
int placeNo = 0;
int placeSize = placesArray.size();
initialMarkupMatrix = new int[placeSize];
for( ; placeNo < placeSize ; placeNo++)
initialMarkupMatrix[placeNo] = ((Place)placesArray.get(placeNo)).getInitialMarking();
}
/**
* Creates Initial Markup Matrix from current Petri-Net
*
*/
private void createCurrentMarkupMatrix(){
int placeNo = 0;
int placeSize = placesArray.size();
currentMarkupMatrix = new int[placeSize];
for( ; placeNo < placeSize ; placeNo++)
currentMarkupMatrix[placeNo] = ((Place)placesArray.get(placeNo)).getCurrentMarking();
}
/**
* Stores Initial Markup Matrix from current Petri-Net Markup
*
*/
public void storeInitialMarking(){
createMatrixes();
int placeNo = 0;
int placeSize = placesArray.size();
initialMarkupMatrix = new int[placeSize];
for( ; placeNo < placeSize ; placeNo++)
initialMarkupMatrix[placeNo] = ((Place)placesArray.get(placeNo)).getCurrentMarking();
}
/**
* Restores Initial Markup Matrix to current Petri-Net Markup
*
*/
public void restoreInitialMarking(){
createMatrixes();
int placeNo = 0;
int placeSize = placesArray.size();
for( ; placeNo < placeSize ; placeNo++) {
Place place = ((Place)placesArray.get(placeNo));
if(place != null) {
place.setCurrentMarking(initialMarkupMatrix[placeNo]);
setChanged();
notifyObservers(place);
}
}
}
/**
* Stores Current Markup
*
*/
public void storeState(){
int placeNo = 0;
int placeSize = placesArray.size();
markupMatrixAnimationStorage = new int[placeSize];
for( ; placeNo < placeSize ; placeNo++)
markupMatrixAnimationStorage[placeNo] = ((Place)placesArray.get(placeNo)).getCurrentMarking();
}
/**
* Restores To previous Stored Markup
*/
public void restoreState(){
if(markupMatrixAnimationStorage != null) {
int placeNo = 0;
int placeSize = placesArray.size();
for( ; placeNo < placeSize ; placeNo++) {
Place place = ((Place)placesArray.get(placeNo));
if(place != null) {
place.setCurrentMarking(markupMatrixAnimationStorage[placeNo]);
setChanged();
notifyObservers(place);
}
}
}
}
/**
* Fire a specified transition, no affect if transtions not enabled
* @param transition Reference of specifiec Transition
*/
public void fireTransition(Transition transition) {
if(transition != null) {
setEnabledTransitions();
if(transition.isEnabled() && placesArray != null){
int transitionNo = transitionsArray.indexOf(transition);
for(int placeNo = 0 ; placeNo < placesArray.size() ; placeNo++) {
((Place)placesArray.get(placeNo)).setCurrentMarking((currentMarkupMatrix[placeNo] + incidenceMatrix.get(placeNo, transitionNo)));
}
}
}
}
/**
* Fire a specified transition, no affect if transtions not enabled
* @param transitionNo Position of Transition in internal ArrayList
*/
private void fireTransition(int transitionNo) {
setEnabledTransitions();
if(((Transition)transitionsArray.get(transitionNo)).isEnabled() && placesArray != null){
for(int placeNo = 0 ; placeNo < placesArray.size() ; placeNo++) {
((Place)placesArray.get(placeNo)).setCurrentMarking((currentMarkupMatrix[placeNo] + incidenceMatrix.get(placeNo, transitionNo)));
}
}
}
/**
* Fire a random transition, takes rate (probability) of Transitions into account
*/
public void fireRandomTransition() {
setEnabledTransitions();
int transitionsSize = transitionsArray.size()*transitionsArray.size()*transitionsArray.size();
int randonTransition = 0;
int transitionNo = 0;
int rate = 0;
ArrayList enabledTransitions = new ArrayList();
for(transitionNo = 0 ; transitionNo < transitionsArray.size() ; transitionNo++) {
if(((Transition)transitionsArray.get(transitionNo)).isEnabled()) {
rate = ((Transition)transitionsArray.get(transitionNo)).getRate();
for(int i = 0 ; i < rate ; i++ ) {
enabledTransitions.add(new Integer(transitionNo));
}
}
}
randonTransition = randomNumber.nextInt((enabledTransitions.size() > 0 ? enabledTransitions.size() : 1 ));
if(randonTransition < enabledTransitions.size()) {
if(enabledTransitions.get(randonTransition) != null) {
fireTransition(((Integer)enabledTransitions.get(randonTransition)).intValue());
}
}
resetEnabledTransitions();
}
public void fireTransitionBackwards(Transition transition) {
if(transition != null) {
setEnabledTransitionsBackwards();
if(transition.isEnabled() && placesArray != null){
int transitionNo = transitionsArray.indexOf(transition);
for(int placeNo = 0 ; placeNo < placesArray.size() ; placeNo++) {
((Place)placesArray.get(placeNo)).setCurrentMarking((currentMarkupMatrix[placeNo] - incidenceMatrix.get(placeNo, transitionNo)));
}
}
}
}
private void fireTransitionBackwards(int transitionNo) {
setEnabledTransitionsBackwards();
if(((Transition)transitionsArray.get(transitionNo)).isEnabled() && placesArray != null){
for(int placeNo = 0 ; placeNo < placesArray.size() ; placeNo++) {
((Place)placesArray.get(placeNo)).setCurrentMarking((currentMarkupMatrix[placeNo] - incidenceMatrix.get(placeNo, transitionNo)));
}
}
}
public void fireRandomTransitionBackwards() {
setEnabledTransitionsBackwards();
int transitionsSize = transitionsArray.size()*transitionsArray.size()*transitionsArray.size();
int randonTransition = 0;
do {
randonTransition = randomNumber.nextInt(transitionsArray.size());
transitionsSize--;
if(transitionsSize <= 0) break;
} while(!((Transition)transitionsArray.get(randonTransition)).isEnabled());
fireTransitionBackwards(randonTransition);
// System.out.println("Random Fired Transition Backwards" + ((Transition)transitionsArray.get(randonTransition)).getId());
}
/**
* Determines whether all transitions are enabled and sets
* the correct value of the enabled boolean
*/
public void setEnabledTransitions() {
createMatrixes();
if(transitionsArray != null && placesArray != null) {
for(int transitionNo = 0 ; transitionNo < transitionsArray.size() ; transitionNo++) {
Transition transition = (Transition)transitionsArray.get(transitionNo);
if(transition != null) {
int test = placesArray.size();
for(int placeNo = 0 ; placeNo < placesArray.size() ; placeNo++) {
if(backwardsIncidenceMatrix.get(placeNo, transitionNo) <= currentMarkupMatrix[placeNo]) {
test--;
}
}
if(test <= 0) {
transition.setEnabled(true);
setChanged();
notifyObservers(transition);
} else {
transition.setEnabled(false);
setChanged();
notifyObservers(transition);
}
}
}
}
}
public void resetEnabledTransitions() {
createMatrixes();
if(transitionsArray != null && placesArray != null) {
for(int transitionNo = 0 ; transitionNo < transitionsArray.size() ; transitionNo++) {
Transition transition = (Transition)transitionsArray.get(transitionNo);
if(transition != null) {
int test = placesArray.size();
for(int placeNo = 0 ; placeNo < placesArray.size() ; placeNo++) {
if(backwardsIncidenceMatrix.get(placeNo, transitionNo) <= currentMarkupMatrix[placeNo]) {
test--;
}
}
if(test <= 0) {
transition.setEnabled(false);
} else {
transition.setEnabled(false);
}
setChanged();
notifyObservers(transition);
}
}
}
}
/**
* Determines whether all transitions are enabled and sets
* the correct value of the enabled boolean
*/
public void setEnabledTransitionsBackwards() {
createMatrixes();
if(transitionsArray != null && placesArray != null) {
for(int transitionNo = 0 ; transitionNo < transitionsArray.size() ; transitionNo++) {
Transition transition = (Transition)transitionsArray.get(transitionNo);
if(transition != null) {
int test = placesArray.size();
for(int placeNo = 0 ; placeNo < placesArray.size() ; placeNo++) {
if(fowardsIncidenceMatrix.get(placeNo, transitionNo) <= currentMarkupMatrix[placeNo]) {
test--;
}
}
if(test <= 0) {
transition.setEnabled(true);
} else {
transition.setEnabled(false);
}
setChanged();
notifyObservers(transition);
}
}
}
}
/**
* Empty all attributes, turn into empty Petri-Net
*/
public void emptyPNML() {
pnmlName = null;
placesArray = null;
transitionsArray = null;
arcsArray = null;
tokensArray = null;
arrowsArray = null;
changeArrayList = null;
initialMarkupMatrix = null;
markupHistoryMatrix = null;
fowardsIncidenceMatrix = null;
backwardsIncidenceMatrix = null;
incidenceMatrix = null;
arcsMap = null;
initializeMatrixes();
}
/**
* Get position of Petri-Net Object in ArrayList of given Petri-Net Object's type
*
* @param pnObject PlaceTransitionObject to get the position of
* @return Position (-1 if not present) of Petri-Net Object in ArrayList of given Petri-Net Object's type
*/
public int getListPosition(PetriNetObject pnObject){
if (setPetriNetObjectArrayList(pnObject))
return changeArrayList.indexOf(pnObject);
else
return -1;
}
/**
* Get an List of all the Place objects in the Petri-Net
*
* @return An List of all the Place objects
*/
public Place[] getPlaces() {
Place[] returnArray = new Place[placesArray.size()];
for(int i = 0 ; i < placesArray.size() ; i++)
returnArray[i] = (Place)placesArray.get(i);
return returnArray;
}
/**
* Get an List of all the Transition objects in the Petri-Net
*
* @return An List of all the Transition objects
*/
public Transition[] getTransitions() {
// setEnabledTransitions();
Transition[] returnArray = new Transition[transitionsArray.size()];
for(int i = 0 ; i < transitionsArray.size() ; i++)
returnArray[i] = (Transition)transitionsArray.get(i);
return returnArray;
}
/**
* Get an List of all the Arcs objects in the Petri-Net
*
* @return An List of all the Arc objects
*/
public Arc[] getArcs() {
Arc[] returnArray = new Arc[arcsArray.size()];
for(int i = 0 ; i < arcsArray.size() ; i++)
returnArray[i] = (Arc)arcsArray.get(i);
return returnArray;
}
/**
* Return the Transition called transitionName from the Petri-Net
*
* @param transitionName Name of Transition object to return
* @return The first Transition object found with a name equal to transitionName
*/
public Transition getTransition(String transitionName) {
Transition returnTransition = null;
if(transitionsArray != null) {
if(transitionName != null) {
for(int i = 0 ; i < transitionsArray.size(); i++) {
if(transitionName.equalsIgnoreCase(((Transition)transitionsArray.get(i)).getId())) {
returnTransition = (Transition)transitionsArray.get(i);
}
}
}
}
return returnTransition;
}
/**
* Return the Transition called transitionName from the Petri-Net
*
* @param transitionNo No of Transition object to return
* @return The Transition object
*/
public Transition getTransition(int transitionNo) {
Transition returnTransition = null;
if(transitionsArray != null) {
if(transitionNo < transitionsArray.size()) {
returnTransition = (Transition)transitionsArray.get(transitionNo);
}
}
return returnTransition;
}
/**
* Return the Place called placeName from the Petri-Net
*
* @param placeName Name of Place object to return
* @return The first Place object found with a name equal to placeName
*/
public Place getPlace(String placeName) {
Place returnPlace = null;
if(placesArray != null) {
if(placeName != null) {
for(int i = 0 ; i < placesArray.size(); i++) {
if(placeName.equalsIgnoreCase(((Place)placesArray.get(i)).getId())) {
returnPlace = (Place)placesArray.get(i);
}
}
}
}
return returnPlace;
}
/**
* Return the Place called placeName from the Petri-Net
*
* @param placeNo No of Place object to return
* @return The Place object
*/
public Place getPlace(int placeNo) {
Place returnPlace = null;
if(placesArray != null) {
if(placeNo < placesArray.size()) {
returnPlace = (Place)placesArray.get(placeNo);
}
}
return returnPlace;
}
/**
* Return the Arc called arcName from the Petri-Net
*
* @param arcName Name of Arc object to return
* @return The first Arc object found with a name equal to arcName
*/
public Arc getArc(String arcName) {
Arc returnArc = null;
if(arcsArray != null) {
if(arcsArray != null) {
for(int i = 0 ; i < arcsArray.size(); i++) {
if(arcName.equalsIgnoreCase(((Arc)arcsArray.get(i)).getId())) {
returnArc = (Arc)arcsArray.get(i);
}
}
}
}
return returnArc;
}
/**
* Return the Arc called arcName from the Petri-Net
*
* @param arcName Name of Arc object to return
* @return The first Arc object found with a name equal to arcName
*/
public Arc getArcWithSource(PetriNetObject arcName) {
Arc returnArc = null;
if(arcsArray != null) {
for(int i = 0 ; i < arcsArray.size(); i++) {
if(arcsArray.get(i) != null) {
Arc arc = (Arc)arcsArray.get(i);
if(arc.getTarget() != null) {
if(arcName.equals(arc.getSource())) {
returnArc = arc;
}
}
}
}
}
return returnArc;
}
/**
* Return the Arc called arcName from the Petri-Net
*
* @param arcName Name of Arc object to return
* @return The first Arc object found with a name equal to arcName
*/
public Arc getArcWithTarget(PetriNetObject arcName) {
Arc returnArc = null;
if(arcsArray != null) {
for(int i = 0 ; i < arcsArray.size(); i++) {
if(arcsArray.get(i) != null) {
Arc arc = (Arc)arcsArray.get(i);
if(arc.getTarget() != null) {
if(arcName.equals(arc.getTarget())) {
returnArc = arc;
}
}
}
}
}
return returnArc;
}
/**
* Return the PlaceTransitionObject called ptoName from the Petri-Net
*
* @param ptoId Id of PlaceTransitionObject object to return
* @return The first Arc PlaceTransitionObject found with a name equal to ptoName
*/
public PlaceTransitionObject getPlaceTransitionObject(String ptoId) {
if (ptoId != null) {
if (getPlace(ptoId) != null)
return getPlace(ptoId);
else if (getTransition(ptoId) != null)
return getTransition(ptoId);
}
return null;
}
/**
* Return the Foward Incidence Matrix for the Petri-Net
*
* @return The Foward Incidence Matrix for the Petri-Net
*/
public int[][] getFowardsIncidenceMatrix() {
createFowardIncidenceMatrix();
return (fowardsIncidenceMatrix != null ? fowardsIncidenceMatrix.getIntArray() : null);
}
/**
* Return the Backward Incidence Matrix for the Petri-Net
*
* @return The Backward Incidence Matrix for the Petri-Net
*/
public int[][] getBackwardsIncidenceMatrix() {
createBackwardsIncidenceMatrix();
return (backwardsIncidenceMatrix != null ? backwardsIncidenceMatrix.getIntArray() : null);
}
/**
* Return the Incidence Matrix for the Petri-Net
*
* @return The Incidence Matrix for the Petri-Net
*/
public int[][] getIncidenceMatrix() {
createIncidenceMatrix();
return (incidenceMatrix != null ? incidenceMatrix.getIntArray() : null);
}
/**
* Return the Initial Markup Matrix for the Petri-Net
*
* @return The Initial Markup Matrix for the Petri-Net
*/
public int[] getInitialMarkupMatrix() {
createInitialMarkupMatrix();
return initialMarkupMatrix;
}
/**
* Return the Initial Markup Matrix for the Petri-Net
*
* @return The Initial Markup Matrix for the Petri-Net
*/
public int[] getCurrentMarkupMatrix() {
createCurrentMarkupMatrix();
return currentMarkupMatrix;
}
/**
* Return a DOM for the PNML file at URI pnmlFileName
*
* @param pnmlFileName URI of PNML file
* @return A DOM for the PNML file pnmlFileName
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public Document getDOM(String pnmlFileName)
throws ParserConfigurationException, IOException, SAXException{
Document document = null;
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setIgnoringElementContentWhitespace(true);
// POSSIBLY ADD VALIDATING
// documentBuilderFactory.setValidating(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.parse(pnmlFileName);
} catch (ParserConfigurationException e) {
System.out.println("javax.xml.parsers.ParserConfigurationException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
}
catch (IOException e) {
System.out.println("ERROR: File may not be present or have the correct attributes");
System.out.println("java.io.IOException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
}
catch (SAXException e) {
System.out.println("org.xml.sax.SAXException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
}
return document;
}
/**
* Return a DOM for the PNML File pnmlFile
*
* @param pnmlFile File Object for PNML of Petri-Net
* @return A DOM for the File Object for PNML of Petri-Net
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public Document getDOM(File pnmlFile)
throws ParserConfigurationException, IOException, SAXException{
Document document = null;
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setIgnoringElementContentWhitespace(true);
// POSSIBLY ADD VALIDATING
// documentBuilderFactory.setValidating(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.parse(pnmlFile);
} catch (ParserConfigurationException e) {
System.out.println("javax.xml.parsers.ParserConfigurationException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
}
catch (IOException e) {
System.out.println("ERROR: File may not be present or have the correct attributes");
System.out.println("java.io.IOException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
}
catch (SAXException e) {
System.out.println("org.xml.sax.SAXException thrown in getDom(String pnmlFileName) : dataLayer Class : dataLayer Package");
}
return document;
}
/**
* Return a DOM for the Petri-Net
*
* @return A DOM for the Petri-Net
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public Document getDOM()
throws ParserConfigurationException, IOException, SAXException{
return getDOM(pnmlName);
}
/**
*
* Return a URI for the PNML file for the Petri-Net
*
* @return A DOM for the Petri-Net
*/
public String getURI() {
return pnmlName;
}
/** prints out a brief representation of the dataLayer object */
public void print() {
System.out.println("No of Places = " + placesArray.size() + "\"");
System.out.println("No of Transitions = " + transitionsArray.size() + "\"");
System.out.println("No of Arcs = " + arcsArray.size() + "\"");
System.out.println("No of Arrows = " + arrowsArray.size() + "\" (Model View Controller Design Pattern)");
}
/**
* dataLayer - Encapsulates entire Matrix, also contains functions to perform some manipulation TODO: Combined with IntMatrix
*
* @see Matrix UML
* @version 1.0
* @author James D Bloom
*/
public class Matrix {
/** Internal Storage of integers */
private int[][] matrix = null;
/** Return value for function when specified index is out of bounds this should be an exception */
public static final int OUT_OF_BOUNDS_ERROR = -666666666;
/**
* Construct Matrix and set size of internal array
* @param i First dimension of array i.e. int[i][j]
* @param j Second dimension of array i.e. int[i][j]
*/
public Matrix(int i, int j) {
matrix = new int[i][j];
}
/**
* Construct Matrix from an integer array
* @param matrix Input integer array
*/
public Matrix(int[][] matrix) {
this.matrix = matrix;
}
/**
* Set all values to zero
*/
public void setToZero() {
for(int i = 0 ; i < matrix.length ; i++)
for(int j = 0 ; j < matrix[i].length ; j++)
matrix[i][j] = 0;
}
/**
* Add a another matrix current matrix
* @param matrixInput Other matrix to add to current matrix
*/
public void add(Matrix matrixInput) {
int[][] matrixIntputIntArray = matrixInput.getIntArray();
for(int i = 0 ; i < matrix.length ; i++)
for(int j = 0 ; j < matrix[i].length ; j++)
matrix[i][j] += matrixIntputIntArray[i][j];
}
/**
* Subtract a another matrix from current matrix
* @param matrixInput Other matrix to subtract from current matrix
*/
public void minus(Matrix matrixInput) {
int[][] matrixIntputIntArray = matrixInput.getIntArray();
for(int i = 0 ; i < matrix.length ; i++)
for(int j = 0 ; j < matrix[i].length ; j++)
matrix[i][j] -= matrixIntputIntArray[i][j];
}
/**
* Set specific value in matrix
* @param i First dimension of array i.e. int[i][j]
* @param j Second dimension of array i.e. int[i][j]
* @param value Value to set int[i][j] to
*/
public void set(int i, int j, int value) {
if(i < matrix.length && j < matrix[i].length) {
matrix[i][j] = value;
}
}
/**
* Get specific value in matrix
* @param i First dimension of array i.e. int[i][j]
* @param j Second dimension of array i.e. int[i][j]
* @return Value from int[i][j]
*/
public int get(int i, int j) {
if(i < matrix.length && j < matrix[i].length)
return matrix[i][j];
else
return OUT_OF_BOUNDS_ERROR;
}
/**
* Return internal array of matrix
* @return Return int[][] from inside matrix
*/
public int[][] getIntArray() {
return matrix;
}
}
}