📄 datalayer.java
字号:
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) {
// Pete: Place/Transitions now always moveable
// 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) {
// Pete: Place/Transitions now always moveable
// 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);
}
// Pete: Removed ArrowLabel class
// 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 if (pnObject instanceof AnnotationNote) {
labelsArray.add(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 didSomething = false;
ArrayList attachedArcs = null;
Iterator i = null;
//System.out.println("removing: " + pnObject.getClass().getName());
if (setPetriNetObjectArrayList(pnObject)) {
didSomething = changeArrayList.remove(pnObject);
// we want to remove all attached arcs also
if (pnObject 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
Arc removeArc;
PlaceTransitionObject attached;
// iterate over all the attached arcs, removing them all
while(!attachedArcs.isEmpty()) {
removeArc = (Arc)attachedArcs.get(0);
// removePetriNetObject(removeArc);
removeArc.delete();
}
arcsMap.remove(pnObject);
}
} else if (pnObject instanceof Arc) {
// get source and target of the arc
PlaceTransitionObject attached = ((Arc)pnObject).getSource();
if (attached != null) {
ArrayList a=(ArrayList)arcsMap.get(attached);
if(a!=null)a.remove(pnObject);
attached.removeFromArc((Arc)pnObject);
if (attached instanceof Transition)
((Transition)attached).removeArcCompareObject((Arc)pnObject);
//attached.updateConnected(); ? causing null pointer exceptions
}
attached = ((Arc)pnObject).getTarget();
if (attached != null) {
((ArrayList)arcsMap.get(attached)).remove(pnObject);
attached.removeToArc((Arc)pnObject);
if (attached instanceof Transition)
((Transition)attached).removeArcCompareObject((Arc)pnObject);
attached.updateConnected();
}
}
if (didSomething) {
setChanged();
// notifyObservers(pnObject.getBounds());
notifyObservers(pnObject);
}
}
// we reset to null so that the wrong ArrayList can't get added to
changeArrayList = null;
}
//######################################################################################
/* Returns an iterator for the transitions array. Used by Animator.class to set all
enabled transitions to highlighted */
public Iterator returnTransitions(){
// System.out.println("returnTransitionsIterator called");
return transitionsArray.iterator();
}
//######################################################################################
/**
* Sets an internal ArrayList according to the class of the object passed in.
* @param pnObject The pnObject in question.
* @return Returns True if the pnObject is of type Place, Transition or Arc
*/
private boolean setPetriNetObjectArrayList(PetriNetObject pnObject) {
// determine appropriate ArrayList
if (pnObject instanceof Transition) {
changeArrayList = transitionsArray;
return true;
} else if (pnObject instanceof Place){
changeArrayList = placesArray;
return true;
} else if (pnObject instanceof Arc) {
changeArrayList = arcsArray;
return true;
} else if(pnObject instanceof Token){
changeArrayList = tokensArray;
return true;
} else if(pnObject instanceof AnnotationNote) {
changeArrayList = labelsArray;
return true;
}
return false;
}
/**
*
* Set the Foward Incidence Matrix
*
* @param fowardIncidenceInput Foward Incidence Matrix object
*/
/* public void setFowardIncidenceMatrix(int[][] fowardIncidenceInput) {
int i = 0;
int j = 0;
Integer intergerInput = null;
for( ; i < fowardIncidenceInput.length ; i++) {
fowardsIncidenceMatrix.add(i, new ArrayList(fowardIncidenceInput[i].length));
for( ; j < fowardIncidenceInput[i].length ; j++) {
intergerInput = new Integer(fowardIncidenceInput[i][j]);
fowardsIncidenceMatrix.add(j, intergerInput);
}
}
}*/
/**
* Set the Backward Incidence Matrix
*
* @param backwardsIncidenceInput Backward Incidence Matrix object
*/
/**
* Returns an iterator of all PetriNetObjects - the order of these cannot be guaranteed.
* @return An iterator of all PetriNetObjects
*/
public Iterator getPetriNetObjects(){
ArrayList all = new ArrayList(placesArray);
all.addAll(transitionsArray);
all.addAll(arcsArray);
all.addAll(tokensArray);
all.addAll (labelsArray);
all.addAll(arrowsArray);
return all.iterator();
}
/**
* Set the Foward Incidence Matrix
*
* @param fowardIncidenceInput Foward Incidence Matrix object
*/
public void setFowardIncidenceMatrix(int[][] fowardIncidenceInput) {
fowardsIncidenceMatrix = new PNMatrix(fowardIncidenceInput);
}
/**
* Set the Backward Incidence Matrix
*
* @param backwardsIncidenceInput Backward Incidence Matrix object
*/
public void setBackwardsIncidenceMatrix(int[][] backwardsIncidenceInput) {
backwardsIncidenceMatrix = new PNMatrix(backwardsIncidenceInput);
}
/**
* Set the Incidence Matrix
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -