Project: bml_lib License: BSD Dependencies: Used by: |
bml_lib/src/edu/wpi/hri/bml/behavior/ConstraintBehavior.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.ArrayList; 00037 import java.util.List; 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.log.Logger; 00045 import edu.wpi.hri.log.Logger.LoggerLevel; 00046 00052 public class ConstraintBehavior extends Behavior { 00053 00054 private static final String BEFORE_ELEMENT = "before"; 00055 private static final String SYNCHRONIZE_ELEMENT = "synchronize"; 00056 private static final String AFTER_ELEMENT = "after"; 00057 private static final String SYNC_ELEMENT = "sync"; 00058 private static final String REF_ATTR = "ref"; 00059 00060 private final List<SyncBlock> before; 00061 private final List<SyncBlock> synchronize; 00062 private final List<SyncBlock> after; 00063 00072 public ConstraintBehavior(Logger logger, Element elem) { 00073 super(logger, elem); 00074 00075 // first pull out all of the before blocks 00076 this.before = new ArrayList<SyncBlock>(); 00077 NodeList list = elem.getElementsByTagName(BEFORE_ELEMENT); 00078 for (int c = 0; c < list.getLength(); c++) 00079 this.before.add(new SyncBlock((Element) list.item(c))); 00080 00081 // then pull out all of the synchronized blocks 00082 this.synchronize = new ArrayList<SyncBlock>(); 00083 list = elem.getElementsByTagName(SYNCHRONIZE_ELEMENT); 00084 for (int c = 0; c < list.getLength(); c++) 00085 this.synchronize.add(new SyncBlock((Element) list.item(c))); 00086 00087 // finally pull out all of the after blocks 00088 this.after = new ArrayList<SyncBlock>(); 00089 list = elem.getElementsByTagName(AFTER_ELEMENT); 00090 for (int c = 0; c < list.getLength(); c++) 00091 this.after.add(new SyncBlock((Element) list.item(c))); 00092 00093 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00094 } 00095 00106 public ConstraintBehavior(Logger logger, String id, boolean required) { 00107 super(logger, id, required); 00108 this.before = new ArrayList<SyncBlock>(); 00109 this.synchronize = new ArrayList<SyncBlock>(); 00110 this.after = new ArrayList<SyncBlock>(); 00111 this.logger.debug(LoggerLevel.BEHAVIOR, "Created ..."); 00112 } 00113 00119 public List<SyncBlock> getBefore() { 00120 return before; 00121 } 00122 00128 public List<SyncBlock> getSynchronize() { 00129 return synchronize; 00130 } 00131 00137 public List<SyncBlock> getAfter() { 00138 return after; 00139 } 00140 00147 public void addBefore(SyncBlock block) { 00148 this.before.add(block); 00149 } 00150 00157 public void addSynchronized(SyncBlock block) { 00158 this.synchronize.add(block); 00159 } 00160 00167 public void addAfter(SyncBlock block) { 00168 this.after.add(block); 00169 } 00170 00171 @Override 00172 protected void appendAttributes(Document doc, Element elem) { 00173 // first add in all of the before blocks 00174 for (SyncBlock block : this.before) 00175 elem.appendChild(block.toBML(doc, BEFORE_ELEMENT)); 00176 00177 // then add in all of the synchronized blocks 00178 for (SyncBlock block : this.synchronize) 00179 elem.appendChild(block.toBML(doc, SYNCHRONIZE_ELEMENT)); 00180 00181 // last add in all of the after blocks 00182 for (SyncBlock block : this.after) 00183 elem.appendChild(block.toBML(doc, AFTER_ELEMENT)); 00184 } 00185 00186 @Override 00187 protected String getElementName() { 00188 return BehaviorType.CONSTRAINT.elementName; 00189 } 00190 00197 public static class SyncBlock { 00198 private final List<SyncRef> refs; 00199 private SyncRef startRef; 00200 00207 public SyncBlock(Element elem) { 00208 // get out the starting reference point 00209 if (elem.hasAttribute(REF_ATTR)) 00210 this.startRef = new SyncRef(elem.getAttribute(REF_ATTR)); 00211 else 00212 this.startRef = null; 00213 00214 // then each of the sub sync points. 00215 this.refs = new ArrayList<SyncRef>(); 00216 NodeList list = elem.getElementsByTagName(SYNC_ELEMENT); 00217 for (int c = 0; c < list.getLength(); c++) 00218 this.refs.add(new SyncRef(((Element) list.item(c)) 00219 .getAttribute(REF_ATTR))); 00220 } 00221 00228 public SyncBlock(SyncRef startRef) { 00229 this.startRef = startRef; 00230 this.refs = new ArrayList<SyncRef>(); 00231 } 00232 00238 public List<SyncRef> getRefs() { 00239 return refs; 00240 } 00241 00247 public SyncRef getStartRef() { 00248 return startRef; 00249 } 00250 00257 public void setStartRef(SyncRef startRef) { 00258 this.startRef = startRef; 00259 } 00260 00268 public void addRef(SyncRef ref) { 00269 this.refs.add(ref); 00270 } 00271 00279 public void removeRef(SyncRef ref) { 00280 this.refs.remove(ref); 00281 } 00282 00294 public Element toBML(Document doc, String name) { 00295 // create the base element first 00296 Element block = doc.createElement(name); 00297 block.setAttribute(REF_ATTR, this.startRef.toString()); 00298 00299 // then add in all of the sub elements 00300 for (SyncRef sr : this.refs) { 00301 Element elem = doc.createElement(SYNC_ELEMENT); 00302 elem.setAttribute(REF_ATTR, sr.toString()); 00303 block.appendChild(elem); 00304 } 00305 00306 return block; 00307 } 00308 } 00309 } |