📄 basicnet.java
字号:
// This is copyrighted source file, part of Rakiura JFern package. // See the file LICENSE for copyright information and the terms and conditions// for copying, distributing and modifications of Rakiura JFern package.// Copyright (C) 1999-2002 by Mariusz Nowostawski and others [http://www.rakiura.org]package org.rakiura.cpn;/**/import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.rakiura.cpn.event.PlaceListener;import org.rakiura.cpn.event.TransitionListener;/** * Implements a basic Petri Net. This implementation is based on two * HashMaps. * *<br><br> * BasicNet.java<br> * Created: Tue Sep 26 15:46:46 2000<br> * *@author <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version 2.1.0 $Revision: 1.9 $ *@since 1.0 */public class BasicNet extends Node implements Net { /**/ private boolean cachedTransitions = false; /**/ private List allTransitions ; /**/ private Map set_forID = new HashMap(); /**/ private Map set_forName = new HashMap(); /** * Creates new instance of the Net. */ public BasicNet() { } /** * Creates a new <code>BasicNet</code> instance with a given name. * @param name a <code>String</code> name value for this Net. */ public BasicNet(String name){ super(name); } /** * Adds given PlaceListener to all Places in this net. *@param aListener a <code>PlaceListener</code> to be * registered with all the places in this net. *@since 2.0 */ public void addPlaceListener(final PlaceListener aListener) { final Iterator i = places().iterator(); while (i.hasNext()) { ((Place) i.next()).addPlaceListener(aListener); } } /** * Adds given TransitionListener to all Transitions in this net. *@param aListener a <code>TransitionListener</code> to be * registered with all the transitions in this net. *@since 2.0 */ public void addTransitionListener(final TransitionListener aListener) { final Iterator i = transitions().iterator(); while (i.hasNext()) { ((Transition) i.next()).addTransitionListener(aListener); } } /** * Adds a node to the net. *@param node NetElement to be added to the net. *@return this net. */ public Net add(NetElement node) { this.set_forName.put(node.getName(), node); this.set_forID.put(node.getID(), node); this.cachedTransitions = false; return this; } /** * Removes the specified node from the net. *@param node NetElement to be removed. *@return null if there was no such element in the net, or the * removed node. */ public NetElement remove(NetElement node) { this.set_forName.remove(node); this.cachedTransitions = false; return (NetElement)this.set_forID.remove(node); } /** * Looks up a node by its ID. *@param id ID of the node. *@return the node object or null if not found. */ public NetElement forID(String id) { return (NetElement) this.set_forID.get(id); } /** * Looks up a node by its name. *@param name Name of the node. *@return the node object or null if not found. */ public NetElement forName(String name) { return (NetElement) this.set_forName.get(name); } /** * Returns all the transitions from this net. *@return list of all the transitions. */ public List transitions() { if (cachedTransitions) { return new ArrayList(allTransitions); } else { final List list = new ArrayList(); final NetVisitor walker = new NetVisitorAdapter() { public void transition(Transition transition) { list.add(transition); } }; final Iterator iter = set_forID.values().iterator(); while (iter.hasNext()) { ((NetElement) iter.next()).apply(walker); } this.allTransitions = new ArrayList(list); this.cachedTransitions = true; return list; } } /** * Returns all the places from this net. *@return list with all the places. */ public List places() { final List list = new ArrayList(); final NetVisitor walker = new NetVisitorAdapter() { public void place(Place place) { list.add(place); } }; final Iterator iter = set_forID.values().iterator(); while(iter.hasNext()){ ((NetElement)iter.next()).apply(walker); } return list; } /** * Visitor pattern. * @param aVisitor a <code>NetVisitor</code> value * @return a <code>NetElement</code> value */ public NetElement apply(final NetVisitor aVisitor) { aVisitor.net(this); return this; } /** * Returns the current marking of the net. *@return current marking of the net. */ public Marking marking() { final Marking m = new Marking(); final Iterator iter = places().iterator(); while (iter.hasNext()) { final Place p = (Place) iter.next(); m.put(p, p.getTokens()); } return m; } /** * Sets new marking for the entire net. If some places are not * referenced in the new marking, the existing marking is left * unchanged. *@param aMarking new Marking for the entire net. *@return previous marking of the net. */ public Marking setMarking(final Marking aMarking) { final Marking old = marking(); final Iterator iter = marking().places().iterator(); while (iter.hasNext()) { final Place p = (Place) iter.next(); p.clearTokens(); p.addTokens(aMarking.forID(p.getID())); } return old; } /** * Resets the marking in the entire net. *@return previous marking of the net. */ public Marking resetMarking() { final Marking old = marking(); final Iterator iter = places().iterator(); while(iter.hasNext()) { ((Place) iter.next()).clearTokens(); } return old; } /** * Rehashes the names of all the net elements. This method should be * used to update the internal net datastructures after changing * the name of any, already inserted into Net, net elements. */ public void rehash() { this.set_forName.clear(); final NetVisitor walker = new NetVisitorAdapter() { public void transition(Transition t) { set_forName.put(t.getName(), t); } public void place(Place t) { set_forName.put(t.getName(), t); } public void inputArc(InputArc t) { set_forName.put(t.getName(), t); } public void outputArc(OutputArc t) { set_forName.put(t.getName(), t); } }; final Iterator iter = this.set_forID.values().iterator(); while (iter.hasNext()) { ((NetElement) iter.next()).apply(walker); } } /** * / * @return a <code>String</code> value */ public String toString() { String res = "(Network: "; final Iterator iter = this.set_forID.values().iterator(); while (iter.hasNext()) { res += iter.next().toString(); } return res + ")"; }} // BasicNet//////////////////// end of file ////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -