|
Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/net/BMLPetriNet.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.ArrayList; 00037 import java.util.Hashtable; 00038 import java.util.List; 00039 00040 import edu.wpi.hri.bml.behavior.SyncRef.SyncPoint; 00041 import edu.wpi.hri.bml.exec.Executor; 00042 import edu.wpi.hri.bml.exec.ListExecutor; 00043 import edu.wpi.hri.log.Logger; 00044 import edu.wpi.hri.log.Logger.Colors; 00045 import edu.wpi.hri.log.Logger.LoggerLevel; 00046 00055 public class BMLPetriNet { 00056 00057 private final Logger logger; 00058 private final Hashtable<String, Place> places; 00059 private final Transition beginning; 00060 private final List<Transition> allTrans; 00061 private final Place blockStart; 00062 private final ThreadGroup threads; 00063 00070 public BMLPetriNet(ListExecutor block, Logger sublog) { 00071 logger = sublog.sub(Colors.PETRI_NET, block.getID()); 00072 threads = new ThreadGroup("Petri:" + block.getID()); 00073 places = new Hashtable<String, Place>(); 00074 allTrans = new ArrayList<Transition>(); 00075 00076 // create the bml block points to execute 00077 beginning = new Transition(threads, logger, block.getID() + "-begin"); 00078 addExecutor(block); 00079 beginning.mergeWith(places.get(block.getID() + ":" + SyncPoint.START) 00080 .getInput()); 00081 logger.debug(LoggerLevel.INIT, "Created petri net"); 00082 blockStart = places.get(block.getID() + ":" + SyncPoint.START); 00083 } 00084 00092 public boolean addExecutor(Executor exec) { 00093 Transition prevTrans = new Transition(threads, logger, exec.getID() 00094 + "-start"); 00095 final Transition firstTrans = prevTrans; 00096 allTrans.add(prevTrans); 00097 00098 if (places.containsKey(exec.getID() + ":" + SyncPoint.START)) 00099 return false; 00100 00101 for (SyncPoint sp : SyncPoint.values()) { 00102 Place curr = new BehaviorPlace(threads, exec, sp, logger); 00103 curr.setInput(prevTrans); 00104 prevTrans.addOutput(curr); 00105 00106 prevTrans = new Transition(threads, logger, exec.getID() + "-" 00107 + sp.next()); 00108 allTrans.add(prevTrans); 00109 curr.addOutput(prevTrans); 00110 prevTrans.addInput(curr); 00111 00112 places.put(exec.getID() + ":" + sp, curr); 00113 } 00114 00115 if (blockStart != null) { 00116 blockStart.addOutput(firstTrans); 00117 firstTrans.addInput(blockStart); 00118 } 00119 00120 logger.debug(LoggerLevel.SCHEDULE, "Added " + exec.getID() 00121 + " to the net"); 00122 00123 return true; 00124 } 00125 00137 public boolean synchronize(String first, String second) { 00138 Place p1 = places.get(first); 00139 Place p2 = places.get(second); 00140 if (p1 == null || p2 == null) 00141 return false; 00142 return p1.getInput().mergeWith(p2.getInput()); 00143 } 00144 00154 public boolean after(String first, String second) { 00155 Place p1 = places.get(first); 00156 Place p2 = places.get(second); 00157 if (p1 == null || p2 == null) 00158 return false; 00159 logger.debug(LoggerLevel.SCHEDULE, "Setting " + p2.getInput().getName() 00160 + " to be after " + p1.getName()); 00161 return p1.addOutput(p2.getInput()) && p2.getInput().addInput(p1); 00162 } 00163 00174 public boolean before(String first, String second) { 00175 return after(second, first); 00176 } 00177 00191 public boolean after(String first, String second, long delayms) { 00192 if (delayms < 0) 00193 return false; 00194 else if (delayms == 0) 00195 return after(first, second); 00196 00197 Place p1 = places.get(first); 00198 Place p2 = places.get(second); 00199 if (p1 == null || p2 == null) 00200 return false; 00201 00202 Place wait = new TimedPlace(threads, delayms, logger); 00203 Transition trans = new Transition(threads, logger, first + "-" + second 00204 + "-" + "wait"); 00205 allTrans.add(trans); 00206 wait.setInput(trans); 00207 trans.addOutput(wait); 00208 00209 logger.debug(LoggerLevel.SCHEDULE, "Setting " + p2.getInput().getName() 00210 + " to be after " + p1.getName()); 00211 return p1.addOutput(wait.getInput()) && wait.addOutput(p2.getInput()) 00212 && wait.getInput().addInput(p1) && p2.getInput().addInput(wait); 00213 } 00214 00228 public boolean before(String first, String second, long delay) { 00229 return after(second, first, delay); 00230 } 00231 00237 public byte execute() { 00238 this.beginning.start(); 00239 byte success = 1; 00240 for (Place p : places.values()) 00241 if (p.finish() == 0) 00242 success = 0; 00243 for(Transition t : allTrans) 00244 t.end(); 00245 return success; 00246 } 00247 00248 public boolean existsCycle() { 00249 List<PetriPart> beento = new ArrayList<PetriPart>(); 00250 return this.beginning.existsCycle(beento); 00251 } 00252 } |