Project: engagement_generation License: BSD Dependencies:
Used by:
None |
engagement_generation/test/edu/wpi/hri/gen/ebml/EBMLListTest.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.gen.ebml; 00035 00036 import junit.framework.TestCase; 00037 00038 import org.junit.Test; 00039 import org.w3c.dom.Document; 00040 import org.w3c.dom.Element; 00041 00042 import edu.wpi.hri.bml.XMLInterface; 00043 import edu.wpi.hri.bml.behavior.HeadBehavior; 00044 import edu.wpi.hri.bml.behavior.BehaviorEnums.HeadMovementType; 00045 import edu.wpi.hri.gen.Generation; 00046 import edu.wpi.hri.log.Logger; 00047 00053 public class EBMLListTest extends TestCase { 00054 00055 @Test 00056 public void testGetAllBehaviors() { 00057 Logger logger = Logger.createDefault(); 00058 XMLInterface xml = Generation.createXML(logger); 00059 EBMLList list = new EBMLList(logger, xml, "test"); 00060 assertEquals(2, list.getAllBehaviors().size()); 00061 00062 HeadBehavior head = new HeadBehavior(logger, "head-test", 00063 HeadMovementType.NOD, 1, 8, false); 00064 list.getHeads().add(head); 00065 assertTrue(list.getAllBehaviors().contains(head)); 00066 00067 ReferenceBehavior ref = createReference(logger, xml, "ref-test", 00068 "test-obj", 0.5); 00069 list.getReferences().add(ref); 00070 assertTrue(list.getAllBehaviors().contains(ref)); 00071 } 00072 00073 @Test 00074 public void testSize() { 00075 Logger logger = Logger.createDefault(); 00076 XMLInterface xml = Generation.createXML(logger); 00077 EBMLList list = new EBMLList(logger, xml, "test"); 00078 assertEquals(2, list.size()); 00079 00080 HeadBehavior head = new HeadBehavior(logger, "head-test", 00081 HeadMovementType.NOD, 1, 8, false); 00082 list.getHeads().add(head); 00083 assertEquals(3, list.size()); 00084 00085 ReferenceBehavior ref = createReference(logger, xml, "ref-test", 00086 "test-obj", 0.5); 00087 list.getReferences().add(ref); 00088 assertEquals(4, list.size()); 00089 } 00090 00091 @Test 00092 public void testGetReferences() { 00093 Logger logger = Logger.createDefault(); 00094 XMLInterface xml = Generation.createXML(logger); 00095 EBMLList list = new EBMLList(logger, xml, "test"); 00096 assertEquals(2, list.getAllBehaviors().size()); 00097 00098 ReferenceBehavior ref = createReference(logger, xml, "ref-test", 00099 "test-obj", 0.5); 00100 list.getReferences().add(ref); 00101 assertTrue(list.getReferences().contains(ref)); 00102 } 00103 00104 @Test 00105 public void testRemoveReference() { 00106 Logger logger = Logger.createDefault(); 00107 XMLInterface xml = Generation.createXML(logger); 00108 EBMLList list = new EBMLList(logger, xml, "test"); 00109 assertEquals(2, list.getAllBehaviors().size()); 00110 00111 ReferenceBehavior ref = createReference(logger, xml, "ref-test", 00112 "test-obj", 0.5); 00113 list.getReferences().add(ref); 00114 assertTrue(list.getReferences().contains(ref)); 00115 00116 list.removeReference(ref); 00117 assertFalse(list.getReferences().contains(ref)); 00118 } 00119 00120 @Test 00121 public void testParseEBML() { 00122 Logger logger = Logger.createDefault(); 00123 XMLInterface xml = Generation.createXML(logger); 00124 String ebml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 00125 + "<ebml:ebml id=\"test\" xmlns=\"org.mindmakers.bml\" " 00126 + "xmlns:ebml=\"edu.wpi.ebml\" " 00127 + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " 00128 + "xsi:schemaLocation=\"edu.wpi.ebml ebml.xsd\"> " 00129 + "<speech id=\"speech-1\"><text>Put the</text></speech> " 00130 + "<reference id=\"ref-1\" target=\"blue-4\" minReliability=\"0.5\" >" 00131 + "<speechOption text=\"text one\" cost=\"0.5\" ><distractor object=\"one\" /></speechOption>" 00132 + "<speechOption text=\"text two\" cost=\"0.6\" ><distractor object=\"two\" /></speechOption>" 00133 + "</reference></ebml:ebml>"; 00134 EBMLList list = EBMLList.parseEBML(logger, xml, ebml); 00135 assertEquals(4, list.size()); 00136 assertEquals("speech-1", list.getSpeeches().get(0).getID()); 00137 00138 ReferenceBehavior ref = list.getReferences().get(0); 00139 assertEquals("ref-1", ref.getID()); 00140 assertEquals("blue-4", ref.getTarget()); 00141 assertEquals(0.5, ref.getMinReliability(), 0.0001); 00142 00143 SpeechOption so = ref.getSpeechOptions().get(0); 00144 assertEquals("text one", so.getText()); 00145 assertEquals(0.5, so.getCost(), 0.0001); 00146 assertTrue(so.getDistractors().contains("one")); 00147 00148 so = ref.getSpeechOptions().get(1); 00149 assertEquals("text two", so.getText()); 00150 assertEquals(0.6, so.getCost(), 0.0001); 00151 assertTrue(so.getDistractors().contains("two")); 00152 } 00153 00154 public static ReferenceBehavior createReference(Logger logger, 00155 XMLInterface xml, String id, String object, double reliable) { 00156 Document doc = xml.createDocument(); 00157 Element ref = doc.createElement("ebml:reference"); 00158 ref.setAttribute("id", id); 00159 ref.setAttribute("target", object); 00160 ref.setAttribute("minReliability", String.valueOf(reliable)); 00161 doc.createElement("SUPER").appendChild(ref); 00162 00163 // add speech options 00164 Element so = doc.createElement("speechOption"); 00165 so.setAttribute("text", "one"); 00166 so.setAttribute("cost", "3"); 00167 ref.appendChild(so); 00168 00169 so = doc.createElement("speechOption"); 00170 so.setAttribute("text", ""); 00171 so.setAttribute("cost", "0"); 00172 Element soChild = doc.createElement("distractor"); 00173 soChild.setAttribute("object", "name-one"); 00174 so.appendChild(soChild); 00175 ref.appendChild(so); 00176 00177 so = doc.createElement("speechOption"); 00178 so.setAttribute("text", "three"); 00179 so.setAttribute("cost", "5"); 00180 soChild = doc.createElement("distractor"); 00181 soChild.setAttribute("object", "name-one"); 00182 so.appendChild(soChild); 00183 soChild = doc.createElement("distractor"); 00184 soChild.setAttribute("object", "name-two"); 00185 so.appendChild(soChild); 00186 ref.appendChild(so); 00187 00188 return new ReferenceBehavior(logger, ref); 00189 } 00190 } |