Project: bml_realizer License: BSD Dependencies:
Used by:
None |
bml_realizer/src/edu/wpi/hri/bml/realizer/RealizerService.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.realizer; 00035 00036 import java.util.ArrayList; 00037 00038 import java.io.BufferedReader; 00039 import java.io.File; 00040 import java.io.InputStreamReader; 00041 00042 import ros.ServiceServer.Callback; 00043 import ros.pkg.bml_msgs.msg.BehaviorResult; 00044 import ros.pkg.bml_msgs.msg.Flag; 00045 import ros.pkg.bml_srvs.srv.BMLRealization.Request; 00046 import ros.pkg.bml_srvs.srv.BMLRealization.Response; 00047 import edu.wpi.hri.bml.XMLInterface; 00048 import edu.wpi.hri.bml.behavior.BehaviorList; 00049 import edu.wpi.hri.bml.exec.SchedulerException; 00050 import edu.wpi.hri.comm.MasterSpinner; 00051 import edu.wpi.hri.log.Logger; 00052 import edu.wpi.hri.log.Logger.LoggerLevel; 00053 00060 public class RealizerService implements Callback<Request, Response> { 00061 00062 private final Logger logger; 00063 private final BMLComm comm; 00064 private final ControlComm control; 00065 private final XMLInterface xmlInterface; 00066 private final MasterSpinner spin; 00067 00081 public RealizerService(Logger logger, BMLComm comm, ControlComm control, 00082 MasterSpinner spin) { 00083 String file = ""; 00084 try { 00085 Process p = Runtime.getRuntime().exec( 00086 "rospack find bml_realizer"); 00087 p.waitFor(); 00088 BufferedReader stdInput = new BufferedReader(new InputStreamReader( 00089 p.getInputStream())); 00090 String egFolder = stdInput.readLine(); 00091 file = egFolder + "/bml.xsd"; 00092 } catch (Exception e) { 00093 } 00094 this.xmlInterface = new XMLInterface(logger, new File(file)); 00095 this.logger = logger; 00096 this.comm = comm; 00097 this.control = control; 00098 this.spin = spin; 00099 this.logger.debug(LoggerLevel.INIT, 00100 "Created realizer service successfully."); 00101 } 00102 00103 @Override 00104 public Response call(Request req) { 00105 Response res = new Response(); 00106 res.behaviors = new ArrayList<BehaviorResult>(); 00107 res.success.value = Flag.FAILURE; 00108 try { 00109 // the user may be debugging so print the entire bml request 00110 logger.info("Starting realization"); 00111 logger.debug(LoggerLevel.IO, "Received BML Request :\n" + req.bml); 00112 00113 // create the list of behaviors in a program readable format 00114 logger.debug(LoggerLevel.BML_EXECUTION, "Parsing BML string ..."); 00115 BehaviorList behaviors = null; 00116 try { 00117 behaviors = BehaviorList.parseBML(this.logger, 00118 this.xmlInterface, req.bml); 00119 } catch (IllegalArgumentException e) { 00120 logger.warn("Invalid BML specified: " + e.getMessage()); 00121 logger.printThrowable(LoggerLevel.ALL, e); 00122 } 00123 00124 // check that the behaviors were parsed properly 00125 if (behaviors == null) { 00126 res.success.value = Flag.INVALID; 00127 return res; 00128 } 00129 00130 // then create the schedule based on the behaviors 00131 logger.debug(LoggerLevel.BML_EXECUTION, "Scheduling behaviors"); 00132 Schedule schedule = new Schedule(this.logger, behaviors, this.comm, 00133 this.control); 00134 try { 00135 schedule.schedule(); 00136 } catch (SchedulerException e) { 00137 logger.warn("Failed schedule: " + e.getMessage()); 00138 res.success.value = Flag.FAILURE; 00139 return res; 00140 } 00141 00142 // then start a new thread for the spinning before starting 00143 // execution 00144 this.spin.startNewMaster(); 00145 synchronized (this) { 00146 00147 // respond to the service with the correct answer from the 00148 // execution. 00149 res.success.value = schedule.execute(); 00150 res.behaviors = behaviors.getFinished(); 00151 logger.debug(LoggerLevel.BML_EXECUTION, "Execution returned " 00152 + res.success.value); 00153 } 00154 } catch (Exception e) { 00155 this.logger.printThrowable(LoggerLevel.BML_EXECUTION, e); 00156 res.success.value = Flag.FAILURE; 00157 } 00158 return res; 00159 } 00160 } |