Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/behavior/LocomotionBehavior.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 00036 import org.w3c.dom.Document; 00037 import org.w3c.dom.Element; 00038 00039 import edu.wpi.hri.bml.behavior.BehaviorEnums.BehaviorType; 00040 import edu.wpi.hri.bml.behavior.BehaviorEnums.LocomotionManner; 00041 import edu.wpi.hri.bml.behavior.BehaviorEnums.LocomotionType; 00042 import edu.wpi.hri.log.Logger; 00043 import edu.wpi.hri.log.Logger.LoggerLevel; 00044 00051 public class LocomotionBehavior extends Behavior { 00052 00053 private static final String TYPE_ATTR = "type"; 00054 private static final String PROXIMITY_ATTR = "proximity"; 00055 private static final String FACING_ATTR = "facing"; 00056 private static final String MANNER_ATTR = "manner"; 00057 private static final String DYNAMIC_ATTR = "dynamic"; 00058 private static final String VELOCITY_ATTR = "velocity"; 00059 00060 private final LocomotionType type; 00061 private final float proximity; 00062 private final float facing; 00063 private final LocomotionManner manner; 00064 private final boolean dynamic; 00065 private final String velocity; 00066 00075 public LocomotionBehavior(Logger logger, Element elem) { 00076 super(logger, elem); 00077 00078 // get the type of the locomotion 00079 if (elem.hasAttribute(TYPE_ATTR)) 00080 this.type = LocomotionType.valueOf(elem.getAttribute(TYPE_ATTR)); 00081 else 00082 this.type = LocomotionType.TARGET; 00083 00084 // then the proximity 00085 if (elem.hasAttribute(PROXIMITY_ATTR)) 00086 this.proximity = Float 00087 .parseFloat(elem.getAttribute(PROXIMITY_ATTR)); 00088 else 00089 this.proximity = 0.0f; 00090 00091 // then the facing 00092 if (elem.hasAttribute(FACING_ATTR)) 00093 this.facing = Float.parseFloat(elem.getAttribute(FACING_ATTR)); 00094 else 00095 this.facing = 0.0f; 00096 00097 // then the manner 00098 if (elem.hasAttribute(MANNER_ATTR)) 00099 this.manner = LocomotionManner.valueOf(elem 00100 .getAttribute(MANNER_ATTR)); 00101 else 00102 this.manner = LocomotionManner.WALKING; 00103 00104 // then if dynamic or not 00105 if (elem.hasAttribute(DYNAMIC_ATTR)) 00106 this.dynamic = Boolean 00107 .parseBoolean(elem.getAttribute(DYNAMIC_ATTR)); 00108 else 00109 this.dynamic = true; 00110 00111 // finally the velocity 00112 if (elem.hasAttribute(VELOCITY_ATTR)) 00113 this.velocity = elem.getAttribute(VELOCITY_ATTR); 00114 else 00115 this.velocity = ""; 00116 00117 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00118 } 00119 00142 public LocomotionBehavior(Logger logger, String id, LocomotionType type, 00143 float proximity, float facing, LocomotionManner manner, 00144 boolean dynamic, String velocity, boolean required) { 00145 super(logger, id, required); 00146 this.type = type; 00147 this.proximity = proximity; 00148 this.facing = facing; 00149 this.manner = manner; 00150 this.dynamic = dynamic; 00151 this.velocity = velocity; 00152 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00153 } 00154 00158 public LocomotionType getType() { 00159 return type; 00160 } 00161 00165 public float getProximity() { 00166 return proximity; 00167 } 00168 00172 public float getFacing() { 00173 return facing; 00174 } 00175 00179 public LocomotionManner getManner() { 00180 return manner; 00181 } 00182 00186 public boolean isDynamic() { 00187 return dynamic; 00188 } 00189 00193 public String getVelocity() { 00194 return velocity; 00195 } 00196 00197 @Override 00198 protected void appendAttributes(Document doc, Element elem) { 00199 00200 // add the type 00201 if (this.type != null) 00202 elem.setAttribute(TYPE_ATTR, this.type.toString()); 00203 00204 // add the proximity 00205 if (this.proximity != 0.0f) 00206 elem.setAttribute(PROXIMITY_ATTR, String.valueOf(this.proximity)); 00207 00208 // add the facing 00209 if (this.facing != 0.0f) 00210 elem.setAttribute(FACING_ATTR, String.valueOf(this.facing)); 00211 00212 // add the manner 00213 if (this.manner != null) 00214 elem.setAttribute(MANNER_ATTR, this.manner.toString()); 00215 00216 // add the dynamic (ALWAYS) 00217 elem.setAttribute(DYNAMIC_ATTR, String.valueOf(this.dynamic)); 00218 00219 // add the velocity 00220 if (this.velocity != null && !this.velocity.isEmpty()) 00221 elem.setAttribute(VELOCITY_ATTR, this.velocity); 00222 00223 } 00224 00225 @Override 00226 protected String getElementName() { 00227 return BehaviorType.LOCOMOTION.elementName; 00228 } 00229 } |