Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/behavior/SyncRef.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.behavior; 00035 00042 public class SyncRef { 00043 private Behavior behavior; 00044 private final String behaviorID; 00045 private final SyncPoint point; 00046 private final double secondOffset; 00047 00059 public SyncRef(Behavior behav, SyncPoint point, double offset) { 00060 this.behavior = behav; 00061 this.behaviorID = behav.getID(); 00062 this.point = point; 00063 this.secondOffset = offset; 00064 } 00065 00076 public SyncRef(String id, SyncPoint point, double offset) { 00077 this.behavior = null; 00078 this.behaviorID = id; 00079 this.point = point; 00080 this.secondOffset = offset; 00081 } 00082 00094 public SyncRef(String id, String pt, String offset) { 00095 this(id, SyncPoint.fromString(pt), Double.parseDouble(offset)); 00096 } 00097 00107 public SyncRef(String id, String pt) { 00108 this(id, pt.split("[+-]")[0], 00109 pt.matches(".*[-+].*") ? pt.split("[+-]")[1] : "0.0"); 00110 } 00111 00119 public SyncRef(String ref) { 00120 this(ref.split(":")[0].trim(), ref.split(":")[1].trim()); 00121 } 00122 00128 public Behavior getBehavior(BehaviorList list) { 00129 if (this.behavior == null) 00130 this.behavior = list.find(this.behaviorID); 00131 return behavior; 00132 } 00133 00139 public String getID() { 00140 return this.behaviorID; 00141 } 00142 00146 public SyncPoint getPoint() { 00147 return point; 00148 } 00149 00153 public double getOffset() { 00154 return this.secondOffset; 00155 } 00156 00157 @Override 00158 public String toString() { 00159 String base = this.behaviorID + ":" + this.point.toString(); 00160 return (this.secondOffset == 0) ? base : base 00161 + ((this.secondOffset > 0) ? " +" : " ") + this.secondOffset; 00162 } 00163 00170 public static enum SyncPoint { 00172 START(ros.pkg.bml_msgs.msg.Behavior.START), 00174 READY(ros.pkg.bml_msgs.msg.Behavior.READY), 00176 STROKE_START(ros.pkg.bml_msgs.msg.Behavior.STROKE_START), 00178 STROKE(ros.pkg.bml_msgs.msg.Behavior.STROKE), 00180 STROKE_END(ros.pkg.bml_msgs.msg.Behavior.STROKE_END), 00184 RELAX(ros.pkg.bml_msgs.msg.Behavior.RELAX), 00186 END(ros.pkg.bml_msgs.msg.Behavior.END); 00187 00189 public final short rosValue; 00190 00191 private SyncPoint(short val) { 00192 this.rosValue = val; 00193 } 00194 00202 public static SyncPoint fromString(String str) { 00203 str = str.trim(); 00204 if (str != null && !str.isEmpty()) 00205 for (SyncPoint sp : SyncPoint.values()) 00206 if (sp.toString().equalsIgnoreCase(str)) 00207 return sp; 00208 return END; 00209 } 00210 00218 public static SyncPoint fromROS(short ros) { 00219 for (SyncPoint sp : SyncPoint.values()) 00220 if (sp.rosValue == ros) 00221 return sp; 00222 return END; 00223 } 00224 00225 @Override 00226 public String toString() { 00227 return super.toString().toLowerCase(); 00228 } 00229 00235 public SyncPoint next() { 00236 if (this.ordinal() + 1 == SyncPoint.values().length) 00237 return null; 00238 else 00239 return SyncPoint.values()[this.ordinal() + 1]; 00240 } 00241 00247 public SyncPoint previous() { 00248 if (this.ordinal() == 0) 00249 return null; 00250 else 00251 return SyncPoint.values()[this.ordinal() - 1]; 00252 } 00253 00259 public boolean isAfter(SyncPoint point) { 00260 return this.ordinal() > point.ordinal(); 00261 } 00262 } 00263 } |