Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/behavior/Behavior.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.log.Logger; 00040 import edu.wpi.hri.log.Logger.Colors; 00041 import edu.wpi.hri.log.Logger.LoggerLevel; 00042 00049 public abstract class Behavior { 00050 00051 private static final String ID_ATTR = "id"; 00052 private static final String START_ATTR = "start"; 00053 private static final String END_ATTR = "end"; 00054 private static final String REQUIRED_ELEM = "required"; 00055 00056 protected final Logger logger; 00057 protected final String id; 00058 protected final boolean required; 00059 protected SyncRef start; 00060 protected SyncRef end; 00061 00068 protected Behavior(Logger logger, Element elem) { 00069 // check if this behavior is required 00070 this.required = ((Element) elem.getParentNode()).getNodeName().equals( 00071 REQUIRED_ELEM); 00072 00073 // get the id as it is required 00074 String s = elem.getAttribute(ID_ATTR); 00075 if (s == null || s.isEmpty()) 00076 throw new IllegalArgumentException("Invalid id attribute"); 00077 this.id = s; 00078 00079 // now create the logger from the id 00080 this.logger = logger.sub(Colors.BEHAVIOR, this.id); 00081 00082 // then the start attribute 00083 if (elem.hasAttribute(START_ATTR)) 00084 this.start = new SyncRef(elem.getAttribute(START_ATTR)); 00085 else 00086 this.start = null; 00087 00088 // and the end attribute 00089 if (elem.hasAttribute(END_ATTR)) 00090 this.end = new SyncRef(elem.getAttribute(END_ATTR)); 00091 else 00092 this.end = null; 00093 } 00094 00102 protected Behavior(Logger logger, String id, boolean required) { 00103 this.id = id; 00104 this.logger = logger.sub(Colors.BEHAVIOR, this.id); 00105 this.required = required; 00106 } 00107 00113 public String getID() { 00114 return this.id; 00115 } 00116 00120 public boolean isRequired() { 00121 return required; 00122 } 00123 00131 public void setStart(SyncRef start) { 00132 this.start = start; 00133 } 00134 00142 public void setEnd(SyncRef end) { 00143 this.end = end; 00144 } 00145 00149 public SyncRef getStart() { 00150 return start; 00151 } 00152 00156 public SyncRef getEnd() { 00157 return end; 00158 } 00159 00160 protected abstract String getElementName(); 00161 00162 protected abstract void appendAttributes(Document doc, Element elem); 00163 00173 public final Element toBML(Document doc) { 00174 this.logger.debug(LoggerLevel.BEHAVIOR, "Converting to BML"); 00175 Element elem = doc.createElement(this.getElementName()); 00176 00177 // always add the id to the element 00178 elem.setAttribute(ID_ATTR, this.id); 00179 00180 // possibly add the start attribute 00181 if (this.start != null) 00182 elem.setAttribute(START_ATTR, this.start.toString()); 00183 00184 // and possibly add the end to the element 00185 if (this.end != null) 00186 elem.setAttribute(END_ATTR, this.end.toString()); 00187 00188 // then append the behavior attributes 00189 this.appendAttributes(doc, elem); 00190 00191 if (this.required) { 00192 Element base = doc.createElement(REQUIRED_ELEM); 00193 base.appendChild(elem); 00194 return base; 00195 } else 00196 return elem; 00197 } 00198 } |