Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/behavior/GazeBehavior.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 java.util.EnumSet; 00037 import java.util.Set; 00038 00039 import org.w3c.dom.Document; 00040 import org.w3c.dom.Element; 00041 import org.w3c.dom.NodeList; 00042 00043 import edu.wpi.hri.bml.behavior.BehaviorEnums.BehaviorType; 00044 import edu.wpi.hri.bml.behavior.BehaviorEnums.Direction; 00045 import edu.wpi.hri.bml.behavior.BehaviorEnums.Modality; 00046 import edu.wpi.hri.log.Logger; 00047 import edu.wpi.hri.log.Logger.LoggerLevel; 00048 00056 public class GazeBehavior extends Behavior { 00057 00058 private static final String TARGET_ATTR = "target"; 00059 private static final String OFFSET_ANGLE_ATTR = "offsetangle"; 00060 private static final String OFFSET_DIRECTION_ATTR = "offsetdirection"; 00061 private static final String POLAR_ANGLE_ATTR = "polarangle"; 00062 private static final String MODALITY_ELEMENT = "modality"; 00063 00064 private final String target; 00065 private final float offsetAngle; 00066 private final Direction offsetDirection; 00067 private final float polarAngle; 00068 private final Set<Modality> modality; 00069 00078 public GazeBehavior(Logger logger, Element elem) { 00079 super(logger, elem); 00080 00081 // find the target attribute if it exists 00082 if (elem.hasAttribute(TARGET_ATTR)) 00083 this.target = elem.getAttribute(TARGET_ATTR); 00084 else 00085 this.target = ""; 00086 00087 // find the offset angle 00088 if (elem.hasAttribute(OFFSET_ANGLE_ATTR)) 00089 this.offsetAngle = Float.parseFloat(elem 00090 .getAttribute(OFFSET_ANGLE_ATTR)); 00091 else 00092 this.offsetAngle = 0.0f; 00093 00094 // find the offset direction 00095 if (elem.hasAttribute(OFFSET_DIRECTION_ATTR)) 00096 this.offsetDirection = Direction.valueOf(elem 00097 .getAttribute(OFFSET_DIRECTION_ATTR)); 00098 else 00099 this.offsetDirection = Direction.POLAR; 00100 00101 // find the polar angle 00102 if (elem.hasAttribute(POLAR_ANGLE_ATTR)) 00103 this.polarAngle = Float.parseFloat(elem 00104 .getAttribute(POLAR_ANGLE_ATTR)); 00105 else 00106 this.polarAngle = 0.0f; 00107 00108 // find all of the modalities 00109 this.modality = EnumSet.noneOf(Modality.class); 00110 NodeList list = elem.getElementsByTagName(MODALITY_ELEMENT); 00111 for (int c = 0; c < list.getLength(); c++) 00112 this.modality.add(Modality.valueOf(((Element) list.item(c)) 00113 .getTextContent())); 00114 00115 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00116 } 00117 00137 private GazeBehavior(Logger logger, String id, String target, 00138 float offsetAngle, Direction offsetDirection, float polarAngle, 00139 boolean required) { 00140 super(logger, id, required); 00141 this.target = target; 00142 this.offsetAngle = offsetAngle; 00143 this.offsetDirection = offsetDirection; 00144 this.polarAngle = polarAngle; 00145 this.modality = EnumSet.noneOf(Modality.class); 00146 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00147 } 00148 00162 public GazeBehavior(Logger logger, String id, String target, 00163 boolean required) { 00164 this(logger, id, target, 0.0f, Direction.POLAR, 0.0f, required); 00165 } 00166 00185 public GazeBehavior(Logger logger, String id, String target, float angle, 00186 Direction dir, boolean required) { 00187 this(logger, id, target, angle, dir, 0.0f, required); 00188 } 00189 00207 public GazeBehavior(Logger logger, String id, String target, float angle, 00208 float polar, boolean required) { 00209 this(logger, id, target, angle, Direction.POLAR, polar, required); 00210 } 00211 00220 public boolean addModality(Modality mode) { 00221 return this.modality.add(mode); 00222 } 00223 00228 public void resetModalities() { 00229 this.modality.clear(); 00230 } 00231 00235 public String getTarget() { 00236 return target; 00237 } 00238 00242 public float getOffsetAngle() { 00243 return offsetAngle; 00244 } 00245 00249 public Direction getOffsetDirection() { 00250 return offsetDirection; 00251 } 00252 00256 public float getPolarAngle() { 00257 return polarAngle; 00258 } 00259 00263 public Set<Modality> getModality() { 00264 return modality; 00265 } 00266 00267 @Override 00268 protected void appendAttributes(Document doc, Element elem) { 00269 // set the target of the gaze 00270 if (this.target != null && !this.target.isEmpty()) 00271 elem.setAttribute(TARGET_ATTR, this.target); 00272 00273 // set the offset values next 00274 if (this.offsetDirection == Direction.POLAR) { 00275 // the angle is in a polar fashion, but make sure it isn't 0 00276 if (this.polarAngle != 0.0f) { 00277 elem.setAttribute(OFFSET_DIRECTION_ATTR, this.offsetDirection 00278 .toString()); 00279 elem.setAttribute(POLAR_ANGLE_ATTR, String 00280 .valueOf(this.polarAngle)); 00281 } 00282 } 00283 // the direction is known, but make sure it isn't 0 00284 else if (this.offsetAngle != 0.0f) { 00285 elem.setAttribute(OFFSET_DIRECTION_ATTR, this.offsetDirection 00286 .toString()); 00287 elem.setAttribute(OFFSET_ANGLE_ATTR, String 00288 .valueOf(this.offsetAngle)); 00289 } 00290 00291 // finally add any modalities that are wanted 00292 for (Modality mode : this.modality) { 00293 Element m = doc.createElement(MODALITY_ELEMENT); 00294 m.setTextContent(mode.toString()); 00295 elem.appendChild(m); 00296 } 00297 } 00298 00299 @Override 00300 protected String getElementName() { 00301 return BehaviorType.GAZE.elementName; 00302 } 00303 } |