fusionplace.java
来自「Rakiura JFern是一个非常轻型的带有模拟器的Petri网络框架」· Java 代码 · 共 182 行
JAVA
182 行
// 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.Collection;import java.util.Iterator;import java.util.List;import org.rakiura.cpn.event.PlaceEvent;import org.rakiura.cpn.event.PlaceListener;import org.rakiura.cpn.event.TokensAddedEvent;import org.rakiura.cpn.event.TokensRemovedEvent;/** * Represents a fusion place. Fusion place in Opal works as * a simple synchronization mechanis between two or more places. * The actual nets may be executed in two completely separete * simulators, and may work completely independently from * one another, with their original places. If two or more nets are * instantiated from the XML, the actual fusion place connecting these * seperate nets must be created seperately from within the API working * with these nets. Fusion place should be thus treated purely as a * runtime feature (not as a static feature of some petri nets). * *<br><br> * FusionPlace.java<br> * Created: Fri Apr 12 16:47:11 2002<br> * *@author <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version 2.1.0 $Revision: 1.9 $ *@since 2.0 */public class FusionPlace extends Place { /**/ private List places = new ArrayList(); /** * Creates a new <code>FusionPlace</code> instance. */ public FusionPlace() { } public Multiset getTokens() { if (this.places.size() > 0) { return ((Place) this.places.get(0)).getTokens(); } else { return new Multiset(); } } /** * Adds given tokens to this place. * @param aMultiset a <code>Multiset</code> value */ public void addTokens(final Multiset aMultiset) { if (this.places.size() > 0) { ((Place) this.places.get(0)).addTokens(aMultiset); } } public void addToken(final Object aToken) { if (this.places.size() > 0) { ((Place) this.places.get(0)).addToken(aToken); } } public void removeTokens(final Multiset aMultiset) { ((Place) this.places.get(0)).removeTokens(aMultiset); } /** * Removes given token from the given multiset from this place. *@param aToken token to be removed. *@return <code>true</code> if the token was succesfully removed, * <code>false</code> if the token was not present in the place and * was not removed. */ public boolean removeToken(final Object aToken) { if (this.places.size() > 0) { return ((Place) this.places.get(0)).removeToken(aToken); } return false; } /** * Removes all the tokens from this place. */ public void clearTokens() { if (this.places.size() > 0) { ((Place) this.places.get(0)).clearTokens(); } } /** * Adds the new Place to this Fusion Place. All added places * to this fusion place will share same marking (same tokens multiset) * as this fusion place. If the added place already has marking, it will be * reset to the existing fused places marking. *@param aPlace a <code>Place</code> value to be added * to this fusion place. */ public void addPlace(final Place aPlace) { final Multiset current = aPlace.getTokens(); final Iterator iter = this.places.iterator(); if (iter.hasNext()) { aPlace.clearTokens(); final Place existingPlace = (Place) iter.next(); final List intersection = new ArrayList(current); intersection.retainAll(existingPlace.getTokens()); final List tokensToBeAdded = new ArrayList(existingPlace.getTokens()); tokensToBeAdded.removeAll(intersection); if (tokensToBeAdded.size() > 0) { aPlace.addTokens(tokensToBeAdded); } } this.places.add(aPlace); aPlace.addFusionPlace(this); } public void addPlaceListener(final PlaceListener aListener) { if (this.places.size() > 0) { ((Place) this.places.get(0)).addPlaceListener(aListener); } } /** * Removes a given place from this fusion place. *@param aPlace a <code>Place</code> value *@return <code>true</code> if a place was present in this fusion place * and was removed succesfully, <code>false</code> otherwise. */ public boolean removePlace(final Place aPlace) { aPlace.removeFusionPlace(this); return this.places.remove(aPlace); } /** * Synchronization routine. This method is synchronizing all the registered places * with this single fusion place. * @param anEvent a <code>PlaceEvent</code> value */ public void notify(final TokensAddedEvent anEvent) { final Place theEventPlace = anEvent.getPlace(); final Multiset changed = anEvent.getTokens(); final Iterator iter = this.places.iterator(); while (iter.hasNext()) { final Place place = (Place) iter.next(); if (place != theEventPlace) place.addTokensQuietly(changed); } } /** * Synchronization routine. This method is synchronizing all the registered places * with this single fusion place. * @param anEvent a <code>PlaceEvent</code> value */ public void notify(final TokensRemovedEvent anEvent) { final Place theEventPlace = anEvent.getPlace(); final Multiset changed = anEvent.getTokens(); final Iterator iter = this.places.iterator(); while (iter.hasNext()) { final Place place = (Place) iter.next(); if (place != theEventPlace) place.removeTokensQuietly(changed); } }} // FusionPlace//////////////////// end of file ////////////////////
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?