⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transition.java

📁 Rakiura JFern是一个非常轻型的带有模拟器的Petri网络框架
💻 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.Iterator;import java.util.List;import java.util.Map;import org.rakiura.cpn.event.PlaceEvent;import org.rakiura.cpn.event.PlaceListener;import org.rakiura.cpn.event.TokensAddedEvent;import org.rakiura.cpn.event.TokensRemovedEvent;import org.rakiura.cpn.event.TransitionFinishedEvent;import org.rakiura.cpn.event.TransitionListener;import org.rakiura.cpn.event.TransitionStartedEvent;import org.rakiura.cpn.event.TransitionStateChangedEvent;/** * Represents a transition in JFern Petri net model. * This class implements a simple transition, and the behaviour * can be overwritten by custom subclasses.<br> * The input and output arcs are stored in HashSet containers. * *<br><br> * Transition.java<br> * Created: Mon Sep 25 19:45:05 2000<br> * *@author  <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version 2.1.0 $Revision: 1.13 $ *@since 1.0 */public class Transition extends Node implements PlaceListener {  /**/  private List listeners = new ArrayList();  /**/  private List inputs = new ArrayList(10);  /**/  private List outputs = new ArrayList(10);  /**/  private boolean enabled = false;  /**/  private boolean uptodate = false;  /**/  private CpnContext binding = null;  /**/  private CpnContext context = new CpnContext();  /** Default guard. */  private Guard guard = new Guard() {      public boolean evaluate() {        return true;      }    };  /** Default action. */  private Action action = new Action();  /**   * Creates a new <code>BasicTransition</code> instance.   */  public Transition() {  }  /**   * Creates a new <code>BasicTransition</code> instance.   *@param aName a <code>String</code> value   */  public Transition(final String aName) {    super(aName);  }  /**   * Creates a new <code>BasicTransition</code> instance.   *@param anInput an <code>InputArc</code> value   *@param anOutput an <code>OutputArc</code> value   */  public Transition(final InputArc anInput, final OutputArc anOutput) {    this();    addInput(anInput);    addOutput(anOutput);  }  /**   * Adds an input Arc. This method is not for the user to call   * directly, it is called by newly created arcs which plug   * themselves automatically to the appropriate transitions.   *@return this Transition.   */  public Transition addInput(final InputArc anArc){    this.inputs.add(anArc);    anArc.place().addPlaceListener(this);    anArc.setContext(new CpnContext(this.context));    return this;  }  /**   * Adds an output Arc. This method is not for the user to call   * directly, it is called by newly created arcs which plug   * themselves automatically to the appropriate transitions.   *@return this Transition.   */  public Transition addOutput(final OutputArc anArc){    this.outputs.add(anArc);    anArc.setContext(new CpnContext(this.context));    return this;  }  /**   * Returns list of all input arcs.   *@return set of all input arcs for this transition.   */  public List inputArcs(){ return this.inputs; }  /**   * Returns list of all output arcs.   *@return set of all output arcs for this transition.   */  public List outputArcs(){ return this.outputs; }  /**   * Sets the guard for this transition.   *@param aGuard a <code>Guard</code> value   *@return an old (previous)  <code>Guard</code> value   */  public Guard setGuard(final Guard aGuard) {    final Guard old = this.guard;    this.guard = aGuard;    return old;  }  /**   * Sets the action for this transition.   *@param anAction an <code>Action</code> value   *@return an old (previous) <code>Action</code> value   */  public Action setAction(final Action anAction) {    final Action old = this.action;    this.action = anAction;    return old;  }  /**   * Returns the action for this transition.   *@return current <code>Action</code> value   */  public Action action() {    return this.action;  }  /**   * Returns the current context for this node.   *@return CPN context for this node.   */  public CpnContext getContext() {    return this.context;  }  /**   * Sets the current context for this node.   *@param aContext current context for this node.   */  public void setContext(final CpnContext aContext) {    this.context = aContext;  }  /**   * Expression evaluation and unification.   * Executes the expressions and tries to perform   * unification procedure. If a binding in which this transition   * is enabled is found, this transition is enabled and this   * transition context (i.e. varPool inside context) keeps the   * valid binding.   */  private boolean unify() {    if (!this.uptodate) {      final boolean oldState = this.enabled;      for (int i = 0; i < this.inputs.size(); i++) {        final InputArc arc = ((InputArc) this.inputs.get(i));        final Multiset minput = arc.place().getTokens();        arc.getContext().setMultiset(minput);        arc.expression();      }      final List arcs = inputArcs();      this.enabled = findBinding(0, arcs);      this.uptodate = true;      if (oldState != this.enabled) {        fireTransitionStateChangedEvent();      }    }    return this.enabled;  }  /**   * Finds the first binding which enables this transition.   *@param index current index of the input arc which is   * being iterated through   */  private final boolean findBinding(final int index, final List arcs) {    final int newIndex = index+1;    final CpnContext c = ((Arc) arcs.get(index)).getContext();    final List allPossible = c.getPossibleBindings();    if (allPossible.size() == 0) {      return false;    }    final Iterator iter = allPossible.iterator();    final CpnContext currentContext = getContext();    while (iter.hasNext()) {      final Map binding = (Map) iter.next();      c.setBinding(binding);      currentContext.getVarPool().clear();      currentContext.getVarPool().putAll(binding);      if (!((InputArc) arcs.get(index)).guard()) continue;      if (newIndex == arcs.size()) {        if (this.guard.evaluate()) {          // first one best          return true;        }      } else {        return findBinding(newIndex, arcs);      }    }    return false;  }  /**   * Checks if this transition is enabled.   *@return a <code>boolean</code> value, <code>true</code> if this   * transition is enabled, <code>false</code>   * otherwise.   */  public boolean isEnabled() {    return unify();  }  /**   * Fire this transition. If this transition is enabled,   * the tokens will be taken from input places according   * to the current binding, action will be executed, and   * tokens will be placed into output places according to   * the binding.   *@return this <code>Transition</code>   */  public Transition fire() {    if (this.listeners.size() > 0) {      fireTransitionStartedEvent();    }    //remove all prepared tokens from input places    final Iterator iter = this.inputs.iterator();    while (iter.hasNext()) {      final InputArc arc = ((InputArc) iter.next());      arc.place().removeTokens(arc.getContext().getBinding().values());    }    //execute the action    getContext().setMultiset(new Multiset(getContext().getVarPool().values()));    this.action.execute();    //put the appropriate multisets into output places    final Iterator oter = this.outputs.iterator();    while (oter.hasNext()) {      final OutputArc arc = ((OutputArc) oter.next());      arc.getContext().setMultiset(new Multiset(getContext().getVarPool().values()));      arc.place().addTokens(arc.expression());    }    if (this.listeners.size() > 0) {      fireTransitionFinishedEvent();    }    return this;  }  /**   * Registers a given TransitionListener with this place.   *@param aListener a <code>TransitionListener</code> to   * be registered with this Transition.   *@since 2.0   */  public void addTransitionListener(final TransitionListener aListener) {    this.listeners.add(aListener);  }  /**   * Deregisters a given TransitionListener from this place.   *@param aListener a <code>TransitionListener</code> to   * be removed with this Transition.   *@since 2.0   */  public void removeTransitionListener(final TransitionListener aListener) {    this.listeners.remove(aListener);  }  /**   * Notifies all listeners just before this transition fire.   */  public void fireTransitionStartedEvent() {    final TransitionStartedEvent event = new TransitionStartedEvent(this);    final Iterator l = this.listeners.iterator();    while (l.hasNext()) {      ((TransitionListener) l.next()).notify(event);    }  }  /**   * Notifies all listeners just after the transition finished firing.   */  public void fireTransitionFinishedEvent() {    final TransitionFinishedEvent event = new TransitionFinishedEvent(this);    final Iterator l = this.listeners.iterator();    while (l.hasNext()) {      ((TransitionListener) l.next()).notify(event);    }  }  /**   * Notifies all listeners just after this transition state changed.   */  private void fireTransitionStateChangedEvent() {    final TransitionStateChangedEvent event = new TransitionStateChangedEvent(this);    final Iterator l = this.listeners.iterator();    while (l.hasNext()) {      ((TransitionListener) l.next()).notify(event);    }  }  public void notify(final PlaceEvent anEvent) {    // ignore  }  public void notify(final TokensRemovedEvent anEvent) {    // binding re-evaluation needed.    this.uptodate = false;  }  public void notify(final TokensAddedEvent anEvent) {    // binding re-evaluation needed.    this.uptodate = false;  }  /**   * Visitor pattern.   *@param aVisitor a <code>NetVisitor</code> value   *@return this <code>NetElement</code> value   */  public NetElement apply(final NetVisitor aVisitor) {    aVisitor.transition(this);    return this;  }  /**   * @return a <code>String</code> value   */  public String toString(){    return "(Transition " + getName() + ")";  }  /**   * Represents this transition guard.   */  public abstract class Guard implements Context {    /**     * Guard function.     *@return <code>true</code> if this guard evaluates     * to enabled transition/arc;     * <code>false</code> otherwise.     */    public abstract boolean evaluate();    public void var(final String aVariable) {      getContext().var(aVariable);    }    public void var(final int aNumber) {      getContext().var(aNumber);    }    public Object get(final String aVariable) {      return getContext().get(aVariable);    }    public Multiset getMultiset() {      return getContext().getMultiset();    }  }// Guard  /**   * Represents this transition action.   */  public class Action implements Context {    /**     * Transition action.     */    public void execute(){}    public void var(final String aVariable) {      getContext().var(aVariable);    }    public void var(final int aNumber) {      getContext().var(aNumber);    }    public Object get(final String aVariable) {      return getContext().get(aVariable);    }    public Multiset getMultiset() {      return getContext().getMultiset();    }  }// Action} // Transition//////////////////// end of file ////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -