Project: engagement_generation License: BSD Dependencies:
Used by:
None |
engagement_generation/src/edu/wpi/hri/gen/comm/TurnFragmentProvider.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.comm; 00035 00036 import java.util.ArrayList; 00037 00038 import java.io.StringReader; 00039 00040 import org.w3c.dom.Document; 00041 import org.w3c.dom.Element; 00042 00043 import ros.NodeHandle; 00044 import ros.RosException; 00045 import ros.ServiceServer; 00046 import ros.ServiceServer.Callback; 00047 import ros.pkg.engagement_msgs.msg.APAction; 00048 import ros.pkg.engagement_msgs.msg.BCOccurrence; 00049 import ros.pkg.bml_msgs.msg.BehaviorResult; 00050 import ros.pkg.bml_msgs.msg.Flag; 00051 import ros.pkg.engagement_srvs.srv.TurnFragment; 00052 import ros.pkg.engagement_srvs.srv.TurnFragment.Request; 00053 import ros.pkg.engagement_srvs.srv.TurnFragment.Response; 00054 import edu.wpi.hri.bml.XMLInterface; 00055 import edu.wpi.hri.gen.GenerationParams; 00056 import edu.wpi.hri.gen.ebml.EBMLList; 00057 import edu.wpi.hri.gen.policy.TurnPositionPolicy; 00058 import edu.wpi.hri.gen.policy.TurnPositionPolicy.APWaiter; 00059 import edu.wpi.hri.gen.policy.ref.ReferencePolicy; 00060 import edu.wpi.hri.log.Logger; 00061 import edu.wpi.hri.log.Logger.Colors; 00062 import edu.wpi.hri.log.Logger.LoggerLevel; 00063 00072 public class TurnFragmentProvider implements Callback<Request, Response> { 00073 00074 private final Logger logger; 00075 private final BMLRealizer realizer; 00076 private final XMLInterface xml; 00077 private final ReferencePolicy refPolicy; 00078 private final TurnPositionPolicy turnPolicy; 00079 private final GazeKnowledge knowledge; 00080 private final SituationKnowledge world; 00081 private final BMLEmitListener emit; 00082 00083 private final ServiceServer<TurnFragment.Request, TurnFragment.Response, TurnFragment> turnSrv; 00084 00110 public TurnFragmentProvider(NodeHandle handle, Logger logger, 00111 BMLRealizer realizer, XMLInterface xml, GazeKnowledge knowledge, 00112 SituationKnowledge world, BMLEmitListener emit, 00113 ReferencePolicy ref, TurnPositionPolicy turn) throws RosException { 00114 this.logger = logger.sub(Colors.EXEC_TURN_FRAG, "ETF"); 00115 this.realizer = realizer; 00116 this.xml = xml; 00117 this.refPolicy = ref; 00118 this.turnPolicy = turn; 00119 this.knowledge = knowledge; 00120 this.world = world; 00121 this.emit = emit; 00122 00123 // create the service (the reason for the handle 00124 this.turnSrv = handle.advertiseService("generation/turn_fragment", 00125 new TurnFragment(), this); 00126 } 00127 00131 public void shutdown() { 00132 this.turnSrv.shutdown(); 00133 } 00134 00135 @Override 00136 public Response call(Request request) { 00137 // create the response object, and let users know what is going on 00138 Response res = new Response(); 00139 res.result.value = Flag.INVALID; 00140 res.response.value = APAction.NO_ACTION; 00141 res.behaviors = new ArrayList<BehaviorResult>(); 00142 res.backchannels = new ArrayList<BCOccurrence>(); 00143 00144 logger.info("Starting Turn Fragment ..."); 00145 logger.debug(LoggerLevel.IO, "Received Request: " + request.fragment); 00146 00147 // then craete the tree of nodes from the string 00148 Document doc = xml.loadDocument(new StringReader(request.fragment)); 00149 Element root = (Element) doc.getFirstChild(); 00150 00151 // then create the list and temporary variables 00152 EBMLList list = EBMLList.parseEBML(logger, xml, request.fragment); 00153 String actor = root.getAttribute("actor"); 00154 String turnPos = root.getAttribute("turnPosition"); 00155 boolean firstTurn = Boolean 00156 .parseBoolean(root.getAttribute("firstTurn")); 00157 boolean isDelay = Boolean.parseBoolean(root.getAttribute("isDelay")); 00158 String delayTarget = root.hasAttribute("delayTarget") ? root 00159 .getAttribute("delayTarget") : ""; 00160 00161 // if we have invlide data, just return the invalid 00162 if (list == null || actor == null || actor.isEmpty()) 00163 return res; 00164 00165 // then apply the policy for executing the turn position 00166 APWaiter waiter = null; 00167 try { 00168 if (GenerationParams.RUN_FULL.getBoolean()) { 00169 waiter = this.turnPolicy.apply(list, turnPos, actor, 00170 firstTurn, isDelay, delayTarget); 00171 } 00172 } catch (Exception e) { 00173 logger.warn("failed turn position"); 00174 return res; 00175 } 00176 logger.debug(LoggerLevel.POLICY_EXECUTION, "finsihed turn position"); 00177 00178 // then apply all of the policies required for a turn fragment 00179 if (!(this.refPolicy.apply(list, knowledge, world, actor))) { 00180 logger.warn("failed references"); 00181 return res; 00182 } 00183 logger.debug(LoggerLevel.POLICY_EXECUTION, "finished references"); 00184 00185 logger.debug(LoggerLevel.IO, "Sending bml now ..."); 00186 // then call the BML realizer to do the behaviors 00187 BMLRealizer.Result result = realizer.sendBML(list); 00188 00189 // then pull out the actions which occurred during realization to return 00190 res.result.value = result.success; 00191 res.behaviors = result.behaviors; 00192 res.backchannels = result.backchannels; 00193 00194 // also ensure that we wait for the end of each connection event 00195 emit.waitForEnd(); 00196 00197 // an AP may have been started and needs to be returned 00198 res.response.value = (waiter != null) ? waiter.getResponse() 00199 : APAction.NO_ACTION; 00200 logger.debug(LoggerLevel.IO, "Realization returned " + res.result.value); 00201 logger.debug(LoggerLevel.IO, "AP returned " + res.response.value); 00202 return res; 00203 } 00204 } |