Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/net/Transition.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 package edu.wpi.hri.bml.net; 00035 00036 import java.util.HashSet; 00037 import java.util.Hashtable; 00038 import java.util.List; 00039 import java.util.Set; 00040 00041 import edu.wpi.hri.log.Logger; 00042 import edu.wpi.hri.log.Logger.LoggerLevel; 00043 00051 public class Transition extends PetriPart { 00052 00053 private Hashtable<Place, Boolean> inputs; 00054 private Set<Place> outputs; 00055 private final String name; 00056 00060 public Transition(ThreadGroup group, Logger sublog, String name) { 00061 super(group, sublog, name); 00062 this.name = name; 00063 this.inputs = new Hashtable<Place, Boolean>(); 00064 this.outputs = new HashSet<Place>(); 00065 } 00066 00067 public String getName() { 00068 return name; 00069 } 00070 00071 @Override 00072 protected boolean checkRequirements() { 00073 if (inputs.size() > 0) 00074 for (Place p : inputs.keySet()) { 00075 Boolean b = inputs.get(p); 00076 logger.debug(LoggerLevel.ALL, "Transition checking [" 00077 + p.getName() + "] of " + b); 00078 if (!b) 00079 return false; 00080 } 00081 return true; 00082 } 00083 00084 @Override 00085 protected void execute() throws PetriException { 00086 // Intentionally left blank, nothing to do 00087 } 00088 00089 @Override 00090 protected void sendTokens() { 00091 sendTokensTo(outputs); 00092 } 00093 00094 @Override 00095 protected void tokenFrom(PetriPart from) { 00096 if (from instanceof Place) { 00097 Place p = (Place) from; 00098 if (inputs.containsKey(p)) 00099 inputs.put(p, true); 00100 } 00101 } 00102 00103 @Override 00104 public boolean existsCycle(List<PetriPart> beento) { 00105 if (beento.contains(this)) 00106 return true; 00107 00108 beento.add(this); 00109 boolean cycle = false; 00110 for (Place p : outputs) 00111 cycle = cycle || p.existsCycle(beento); 00112 00113 beento.remove(this); 00114 return cycle; 00115 } 00116 00124 public boolean addInput(Place p) { 00125 if (inputs.containsKey(p)) 00126 return false; 00127 else { 00128 inputs.put(p, false); 00129 return true; 00130 } 00131 } 00132 00140 public boolean addOutput(Place p) { 00141 return outputs.add(p); 00142 } 00143 00152 public boolean mergeWith(Transition trans) { 00153 logger.debug(LoggerLevel.SCHEDULE, "Merging with Transition " 00154 + trans.name); 00155 // first check that the inputs can be moved from the places 00156 Set<Place> ins = new HashSet<Place>(); 00157 for (Place p : trans.inputs.keySet()) 00158 if (p.replaceOutput(trans, this)) 00159 ins.add(p); 00160 00161 // Then if not all of them replaced, it didn't work 00162 if (ins.size() != trans.inputs.keySet().size()) { 00163 for (Place p : ins) 00164 p.replaceOutput(this, trans); 00165 logger.debug(LoggerLevel.SCHEDULE, "The inputs failed ..."); 00166 return false; 00167 } 00168 logger.debug(LoggerLevel.SCHEDULE, 00169 "The inputs can be moved properly ..."); 00170 00171 // Second check that the outputs can be moved from the places 00172 Set<Place> outs = new HashSet<Place>(); 00173 for (Place p : trans.outputs) 00174 if (p.replaceInput(trans, this)) 00175 outs.add(p); 00176 00177 // Then we know how many succeeded, and can ensure it all worked 00178 if (outs.size() == trans.outputs.size()) { 00179 logger.debug(LoggerLevel.SCHEDULE, 00180 "The outputs can be moved properly ..."); 00181 for (Place p : ins) 00182 this.inputs.put(p, false); 00183 for (Place p : outs) 00184 this.outputs.add(p); 00185 return true; 00186 } else { 00187 logger.debug(LoggerLevel.SCHEDULE, "The outputs failed ..."); 00188 // at least one of the replaces failed 00189 for (Place p : outs) 00190 p.replaceInput(this, trans); 00191 return false; 00192 } 00193 } 00194 00195 public void end() { 00196 if(thread.isAlive()) { 00197 status = false; 00198 start(); 00199 } 00200 finish(); 00201 } 00202 } |