Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/behavior/GestureBehavior.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.Amplitude; 00040 import edu.wpi.hri.bml.behavior.BehaviorEnums.BehaviorType; 00041 import edu.wpi.hri.bml.behavior.BehaviorEnums.Direction; 00042 import edu.wpi.hri.bml.behavior.BehaviorEnums.Distance; 00043 import edu.wpi.hri.bml.behavior.BehaviorEnums.GestureType; 00044 import edu.wpi.hri.bml.behavior.BehaviorEnums.HandShape; 00045 import edu.wpi.hri.bml.behavior.BehaviorEnums.HorizontalLocation; 00046 import edu.wpi.hri.bml.behavior.BehaviorEnums.Power; 00047 import edu.wpi.hri.bml.behavior.BehaviorEnums.Side; 00048 import edu.wpi.hri.bml.behavior.BehaviorEnums.Trajectory; 00049 import edu.wpi.hri.bml.behavior.BehaviorEnums.TwoHanded; 00050 import edu.wpi.hri.bml.behavior.BehaviorEnums.VerticalLocation; 00051 import edu.wpi.hri.log.Logger; 00052 import edu.wpi.hri.log.Logger.LoggerLevel; 00053 00060 public class GestureBehavior extends Behavior { 00061 00062 private static final String TYPE_ATTR = "type"; 00063 private static final String HAND_ATTR = "hand"; 00064 private static final String REPETITION_ATTR = "repetition"; 00065 private static final String TARGET_ATTR = "target"; 00066 private static final String HAND_SHAPE_ATTR = "handshape"; 00067 private static final String FINGER_DIR_ATTR = "extendedfingerdirection"; 00068 private static final String PALM_DIR_ATTR = "palmdirection"; 00069 private static final String TRAJECTORY_ATTR = "trajectory"; 00070 private static final String TWO_HANDED_ATTR = "twohanded"; 00071 private static final String LOCATION_VERTICAL_ATTR = "location_vertical"; 00072 private static final String LOCATION_HORIZONTAL_ATTR = "location_horizontal"; 00073 private static final String LOCATION_DISTANCE_ATTR = "location_distance"; 00074 private static final String MOVE_DIR_ATTR = "movementdirection"; 00075 private static final String LEXEME_ATTR = "lexeme"; 00076 private static final String AMPLITUDE_ATTR = "amplitude"; 00077 private static final String POWER_ATTR = "power"; 00078 private static final String FINISH_ATTR = "finish"; 00079 00080 private final GestureType gestureType; 00081 private final Side hand; 00082 private final int repetition; 00083 private final String target; 00084 private final HandShape handShape; 00085 private final Direction fingerDir; 00086 private final Direction palmDir; 00087 private final Trajectory trajectory; 00088 private final TwoHanded twoHanded; 00089 private final VerticalLocation vertLoc; 00090 private final HorizontalLocation horzLoc; 00091 private final Distance distLoc; 00092 private final Direction moveDir; 00093 private final String lexeme; 00094 private final Amplitude amplitude; 00095 private final Power power; 00096 private final boolean finish; 00097 00106 public GestureBehavior(Logger logger, Element elem) { 00107 super(logger, elem); 00108 00109 // find the type of this gesture 00110 if (elem.hasAttribute(TYPE_ATTR)) 00111 this.gestureType = GestureType 00112 .valueOf(elem.getAttribute(TYPE_ATTR)); 00113 else 00114 throw new IllegalArgumentException("Type not given for Gesture"); 00115 00116 // find the hand to use 00117 if (elem.hasAttribute(HAND_ATTR)) 00118 this.hand = Side.valueOf(elem.getAttribute(HAND_ATTR)); 00119 else 00120 this.hand = Side.BOTH; 00121 00122 // find the number of repetitions 00123 if (elem.hasAttribute(REPETITION_ATTR)) 00124 this.repetition = Integer.parseInt(elem 00125 .getAttribute(REPETITION_ATTR)); 00126 else 00127 this.repetition = 1; 00128 00129 // find the target 00130 if (elem.hasAttribute(TARGET_ATTR)) 00131 this.target = elem.getAttribute(TARGET_ATTR); 00132 else 00133 this.target = ""; 00134 00135 // find the hand shape 00136 if (elem.hasAttribute(HAND_SHAPE_ATTR)) 00137 this.handShape = HandShape.fromString(elem 00138 .getAttribute(HAND_SHAPE_ATTR)); 00139 else 00140 this.handShape = HandShape.INDEXFINGER; 00141 00142 // find the finger direction 00143 if (elem.hasAttribute(FINGER_DIR_ATTR)) 00144 this.fingerDir = Direction.valueOf(elem 00145 .getAttribute(FINGER_DIR_ATTR)); 00146 else 00147 this.fingerDir = Direction.POLAR; 00148 00149 // find the palm direction 00150 if (elem.hasAttribute(PALM_DIR_ATTR)) 00151 this.palmDir = Direction.valueOf(elem.getAttribute(PALM_DIR_ATTR)); 00152 else 00153 this.palmDir = Direction.POLAR; 00154 00155 // find the trajectory 00156 if (elem.hasAttribute(TRAJECTORY_ATTR)) 00157 this.trajectory = Trajectory.fromString(elem 00158 .getAttribute(TRAJECTORY_ATTR)); 00159 else 00160 this.trajectory = Trajectory.STRAIGHT; 00161 00162 // find the two handedness 00163 if (elem.hasAttribute(TWO_HANDED_ATTR)) 00164 this.twoHanded = TwoHanded.valueOf(elem 00165 .getAttribute(TWO_HANDED_ATTR)); 00166 else 00167 this.twoHanded = TwoHanded.MIRROR; 00168 00169 // find the vertical location 00170 if (elem.hasAttribute(LOCATION_VERTICAL_ATTR)) 00171 this.vertLoc = VerticalLocation.valueOf(elem 00172 .getAttribute(LOCATION_VERTICAL_ATTR)); 00173 else 00174 this.vertLoc = VerticalLocation.CENTER; 00175 00176 // find the horizontal location 00177 if (elem.hasAttribute(LOCATION_HORIZONTAL_ATTR)) 00178 this.horzLoc = HorizontalLocation.valueOf(elem 00179 .getAttribute(LOCATION_HORIZONTAL_ATTR)); 00180 else 00181 this.horzLoc = HorizontalLocation.CENTER; 00182 00183 // find the location distance 00184 if (elem.hasAttribute(LOCATION_DISTANCE_ATTR)) 00185 this.distLoc = Distance.valueOf(elem 00186 .getAttribute(LOCATION_DISTANCE_ATTR)); 00187 else 00188 this.distLoc = Distance.MEDIUM; 00189 00190 // find the movement direction 00191 if (elem.hasAttribute(MOVE_DIR_ATTR)) 00192 this.moveDir = Direction.valueOf(elem.getAttribute(MOVE_DIR_ATTR)); 00193 else 00194 this.moveDir = Direction.POLAR; 00195 00196 // find the lexeme 00197 if (elem.hasAttribute(LEXEME_ATTR)) 00198 this.lexeme = elem.getAttribute(LEXEME_ATTR); 00199 else 00200 this.lexeme = ""; 00201 00202 // find the amplitude 00203 if (elem.hasAttribute(AMPLITUDE_ATTR)) 00204 this.amplitude = Amplitude.fromString(elem 00205 .getAttribute(AMPLITUDE_ATTR)); 00206 else 00207 this.amplitude = Amplitude.MEDIUM; 00208 00209 // find the power 00210 if (elem.hasAttribute(POWER_ATTR)) 00211 this.power = Power.valueOf(elem.getAttribute(POWER_ATTR)); 00212 else 00213 this.power = Power.NORMAL; 00214 00215 // find the finish 00216 if (elem.hasAttribute(FINISH_ATTR)) 00217 this.finish = Boolean.parseBoolean(elem.getAttribute(FINISH_ATTR)); 00218 else 00219 this.finish = true; 00220 00221 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00222 } 00223 00268 public GestureBehavior(Logger logger, String id, GestureType type, 00269 Side hand, int repetition, String target, HandShape handShape, 00270 Direction fingerDir, Direction palmDir, Trajectory trajectory, 00271 TwoHanded twoHanded, VerticalLocation vertLoc, 00272 HorizontalLocation horzLoc, Distance distLoc, Direction moveDir, 00273 String lexeme, Amplitude amplitude, Power power, boolean finish, 00274 boolean required) { 00275 super(logger, id, required); 00276 this.gestureType = type; 00277 this.hand = hand; 00278 this.repetition = repetition; 00279 this.target = target; 00280 this.handShape = handShape; 00281 this.fingerDir = fingerDir; 00282 this.palmDir = palmDir; 00283 this.trajectory = trajectory; 00284 this.twoHanded = twoHanded; 00285 this.vertLoc = vertLoc; 00286 this.horzLoc = horzLoc; 00287 this.distLoc = distLoc; 00288 this.moveDir = moveDir; 00289 this.lexeme = lexeme; 00290 this.amplitude = amplitude; 00291 this.power = power; 00292 this.finish = finish; 00293 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00294 } 00295 00299 public GestureType getGestureType() { 00300 return gestureType; 00301 } 00302 00306 public Side getHand() { 00307 return hand; 00308 } 00309 00313 public int getRepetition() { 00314 return repetition; 00315 } 00316 00320 public String getTarget() { 00321 return target; 00322 } 00323 00327 public HandShape getHandShape() { 00328 return handShape; 00329 } 00330 00334 public Direction getFingerDir() { 00335 return fingerDir; 00336 } 00337 00341 public Direction getPalmDir() { 00342 return palmDir; 00343 } 00344 00348 public Trajectory getTrajectory() { 00349 return trajectory; 00350 } 00351 00355 public TwoHanded getTwoHanded() { 00356 return twoHanded; 00357 } 00358 00362 public VerticalLocation getVertLoc() { 00363 return vertLoc; 00364 } 00365 00369 public HorizontalLocation getHorzLoc() { 00370 return horzLoc; 00371 } 00372 00376 public Distance getDistLoc() { 00377 return distLoc; 00378 } 00379 00383 public Direction getMoveDir() { 00384 return moveDir; 00385 } 00386 00390 public String getLexeme() { 00391 return lexeme; 00392 } 00393 00397 public Amplitude getAmplitude() { 00398 return amplitude; 00399 } 00400 00404 public Power getPower() { 00405 return power; 00406 } 00407 00411 public boolean getFinish() { 00412 return finish; 00413 } 00414 00415 @Override 00416 protected void appendAttributes(Document doc, Element elem) { 00417 elem.setAttribute(TYPE_ATTR, this.gestureType.toString()); 00418 00419 if (this.hand != null) 00420 elem.setAttribute(HAND_ATTR, this.hand.toString()); 00421 00422 if (this.repetition != 1) 00423 elem.setAttribute(REPETITION_ATTR, String.valueOf(this.repetition)); 00424 00425 if (this.target != null) 00426 elem.setAttribute(TARGET_ATTR, this.target); 00427 00428 if (this.handShape != null) 00429 elem.setAttribute(HAND_SHAPE_ATTR, this.handShape.toString()); 00430 00431 if (this.fingerDir != null) 00432 elem.setAttribute(FINGER_DIR_ATTR, this.fingerDir.toString()); 00433 00434 if (this.palmDir != null) 00435 elem.setAttribute(PALM_DIR_ATTR, this.palmDir.toString()); 00436 00437 if (this.trajectory != null) 00438 elem.setAttribute(TRAJECTORY_ATTR, this.trajectory.toString()); 00439 00440 if (this.twoHanded != null) 00441 elem.setAttribute(TWO_HANDED_ATTR, this.twoHanded.toString()); 00442 00443 if (this.vertLoc != null) 00444 elem.setAttribute(LOCATION_VERTICAL_ATTR, this.vertLoc.toString()); 00445 00446 if (this.horzLoc != null) 00447 elem.setAttribute(LOCATION_HORIZONTAL_ATTR, this.horzLoc.toString()); 00448 00449 if (this.distLoc != null) 00450 elem.setAttribute(LOCATION_DISTANCE_ATTR, this.distLoc.toString()); 00451 00452 if (this.moveDir != null) 00453 elem.setAttribute(MOVE_DIR_ATTR, this.moveDir.toString()); 00454 00455 if (this.lexeme != null) 00456 elem.setAttribute(LEXEME_ATTR, this.lexeme); 00457 00458 if (this.amplitude != null) 00459 elem.setAttribute(AMPLITUDE_ATTR, this.amplitude.toString()); 00460 00461 if (this.power != null) 00462 elem.setAttribute(POWER_ATTR, this.power.toString()); 00463 00464 if (!this.finish) 00465 elem.setAttribute(FINISH_ATTR, String.valueOf(this.finish)); 00466 } 00467 00468 @Override 00469 protected String getElementName() { 00470 return BehaviorType.GESTURE.elementName; 00471 } 00472 } |