Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/exec/Executor.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.exec; 00035 00036 import org.w3c.dom.Document; 00037 import org.w3c.dom.Element; 00038 00039 import ros.pkg.bml_msgs.msg.Flag; 00040 import edu.wpi.hri.bml.behavior.Behavior; 00041 import edu.wpi.hri.bml.behavior.SyncRef; 00042 import edu.wpi.hri.bml.behavior.SyncRef.SyncPoint; 00043 import edu.wpi.hri.bml.realizer.BMLComm; 00044 import edu.wpi.hri.log.Logger; 00045 import edu.wpi.hri.log.Logger.Colors; 00046 import edu.wpi.hri.log.Logger.LoggerLevel; 00047 00054 public abstract class Executor { 00055 00056 private SyncPoint currentPoint; 00057 private Behavior behav; 00058 protected final Logger logger; 00059 private final BMLComm comm; 00060 private byte result; 00061 private final boolean publish; 00062 00075 protected Executor(Logger logger, BMLComm comm, Behavior behav, 00076 boolean publish) { 00077 this(logger, comm, behav.getID(), publish); 00078 this.behav = behav; 00079 } 00080 00093 protected Executor(Logger logger, BMLComm comm, String id, boolean publish) { 00094 this.publish = publish; 00095 this.comm = comm; 00096 this.behav = new Behavior(logger, id, false) { 00097 00098 @Override 00099 protected String getElementName() { 00100 return ""; 00101 } 00102 00103 @Override 00104 protected void appendAttributes(Document doc, Element elem) { 00105 } 00106 }; 00107 this.logger = logger.sub(Colors.EXECUTOR, id); 00108 this.currentPoint = SyncPoint.START; 00109 this.result = Flag.INVALID; 00110 00111 this.logger.debug(LoggerLevel.BEHAVIOR, "created ..."); 00112 } 00113 00117 public SyncPoint getPoint() { 00118 if (this.currentPoint == null) 00119 return SyncPoint.END; 00120 else 00121 return this.currentPoint.previous(); 00122 } 00123 00127 public String getID() { 00128 return this.behav.getID(); 00129 } 00130 00134 public byte getResult() { 00135 return this.result; 00136 } 00137 00138 public byte doSyncPoint(SyncPoint point) { 00139 // if we have already done this 00140 if ((this.currentPoint == null) || (this.currentPoint.isAfter(point))) 00141 return Flag.FAILURE; 00142 // then call the do method 00143 result = this.doPoint(this.currentPoint); 00144 00145 if (publish) 00146 comm 00147 .publishPerformance(new SyncRef(this.behav, point, 0.0), 00148 result); 00149 if (result == Flag.SUCCESS) { 00150 currentPoint = currentPoint.next(); 00151 // then fire off the listeners to execute at the same time 00152 this.logger.debug(LoggerLevel.BML_EXECUTION, 00153 "Successfully Completed " + point); 00154 } else { 00155 this.logger.warn("Failed at " + point); 00156 } 00157 00158 return result; 00159 } 00160 00164 protected abstract byte doPoint(SyncPoint point); 00165 } |