Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/net/PetriPart.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.List; 00037 import java.util.Set; 00038 00039 import edu.wpi.hri.log.Logger; 00040 import edu.wpi.hri.log.Logger.Colors; 00041 import edu.wpi.hri.log.Logger.LoggerLevel; 00042 00050 public abstract class PetriPart { 00051 00052 protected final Logger logger; 00053 protected final Thread thread; 00054 private boolean success; 00055 protected boolean status; 00056 00067 protected PetriPart(ThreadGroup group, Logger sublog, String name) { 00068 this.logger = sublog.sub(Colors.PETRI_NET, name); 00069 this.success = false; 00070 this.status = true; 00071 this.thread = new Thread(group, name) { 00072 00073 @Override 00074 public void run() { 00075 // wait to hear the start of the execution of this part 00076 synchronized (thread) { 00077 logger.debug(LoggerLevel.BML_EXECUTION, 00078 "Waiting for previous parts"); 00079 do { 00080 try { 00081 thread.wait(); 00082 } catch (InterruptedException e) { 00083 } 00084 } while (!shouldExecute()); 00085 } 00086 logger.debug(LoggerLevel.BML_EXECUTION, "Executing ... "); 00087 // then execute the transition or place 00088 if (status) { 00089 try { 00090 execute(); 00091 success = true; 00092 } catch (PetriException e) { 00093 success = false; 00094 } 00095 } 00096 logger.debug(LoggerLevel.BML_EXECUTION, 00097 "Completed with status [" + success + "]"); 00098 00099 // send the signals to the next parts so that they may begin 00100 sendTokens(); 00101 } 00102 }; 00103 this.thread.start(); 00104 } 00105 00106 private final void receiveToken(PetriPart from, boolean status) { 00107 this.status = this.status && status; 00108 if (this.status) { 00109 tokenFrom(from); 00110 } 00111 this.start(); 00112 } 00113 00114 private final boolean shouldExecute() { 00115 return (!status) || checkRequirements(); 00116 } 00117 00125 protected abstract boolean checkRequirements(); 00126 00135 protected abstract void execute() throws PetriException; 00136 00141 protected abstract void sendTokens(); 00142 00149 protected abstract void tokenFrom(PetriPart from); 00150 00158 protected final void sendTokensTo(Set<? extends PetriPart> parts) { 00159 logger.debug(LoggerLevel.BML_EXECUTION, "Sending all tokens ..."); 00160 for (PetriPart pp : parts) { 00161 logger.debug(LoggerLevel.BML_EXECUTION, "Sent a token"); 00162 pp.receiveToken(this, success && status); 00163 } 00164 } 00165 00170 public void start() { 00171 synchronized (thread) { 00172 logger.debug(LoggerLevel.BML_EXECUTION, 00173 "Sending start notification"); 00174 thread.notifyAll(); 00175 } 00176 } 00177 00181 public byte finish() { 00182 try { 00183 this.thread.join(); 00184 } catch (InterruptedException e) { 00185 } 00186 logger.debug(LoggerLevel.BML_EXECUTION, "Thread terminated"); 00187 return 1; 00188 } 00189 00190 public abstract boolean existsCycle(List<PetriPart> beento); 00191 } |