00001 /**************************************************************** 00002 * 00003 * Copyright (c) 2011, 2012 00004 * 00005 * School of Engineering, Cardiff University, UK 00006 * 00007 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00008 * 00009 * Project name: srs EU FP7 (www.srs-project.eu) 00010 * ROS stack name: srs 00011 * ROS package name: srs_knowledge 00012 * Description: 00013 * 00014 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00015 * 00016 * @author Ze Ji, email: jiz1@cf.ac.uk 00017 * 00018 * Date of creation: Oct 2011: 00019 * ToDo: 00020 * 00021 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00022 * 00023 * Redistribution and use in source and binary forms, with or without 00024 * modification, are permitted provided that the following conditions are met: 00025 * 00026 * * Redistributions of source code must retain the above copyright 00027 * notice, this list of conditions and the following disclaimer. 00028 * * Redistributions in binary form must reproduce the above copyright 00029 * notice, this list of conditions and the following disclaimer in the 00030 * documentation and/or other materials provided with the distribution. 00031 * * Neither the name of the school of Engineering, Cardiff University nor 00032 * the names of its contributors may be used to endorse or promote products 00033 * derived from this software without specific prior written permission. 00034 * 00035 * This program is free software: you can redistribute it and/or modify 00036 * it under the terms of the GNU Lesser General Public License LGPL as 00037 * published by the Free Software Foundation, either version 3 of the 00038 * License, or (at your option) any later version. 00039 * 00040 * This program is distributed in the hope that it will be useful, 00041 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00042 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00043 * GNU Lesser General Public License LGPL for more details. 00044 * 00045 * You should have received a copy of the GNU Lesser General Public 00046 * License LGPL along with this program. 00047 * If not, see <http://www.gnu.org/licenses/>. 00048 * 00049 ****************************************************************/ 00050 00051 package org.srs.srs_knowledge.task; 00052 00053 import java.io.*; 00054 import java.util.StringTokenizer; 00055 //import org.apache.commons.logging.Log; 00056 import java.util.ArrayList; 00057 import ros.pkg.srs_knowledge.msg.*; 00058 import ros.pkg.geometry_msgs.msg.Pose2D; 00059 import org.srs.srs_knowledge.knowledge_engine.*; 00060 import org.srs.srs_knowledge.task.Task; 00061 import java.util.HashMap; 00062 00069 public abstract class HighLevelActionUnit { 00070 public abstract String getActionType(); 00071 public static final int COMPLETED_SUCCESS = -10; 00072 public static final int COMPLETED_FAIL = -11; 00073 public static final int INVALID_INDEX = -100; 00074 00075 public int getNumOfActions() { 00076 return actionUnits.size(); 00077 } 00078 00079 public int getCurrentCUActionIndex() { 00080 00081 return currentActionInd; 00082 } 00083 00084 public int getNextCUActionIndex(boolean statusLastStep) { 00085 00086 if(currentActionInd == -1) { 00087 return 0; 00088 } 00089 //currentActionInd++; 00090 00091 if ( currentActionInd >= 0 && currentActionInd < actionUnits.size() ) { 00092 if(statusLastStep) { 00093 return nextActionMapIfSuccess[currentActionInd]; 00094 } 00095 else { 00096 return nextActionMapIfFail[currentActionInd]; 00097 } 00098 } 00099 else { 00100 return INVALID_INDEX; 00101 } 00102 } 00103 00104 public CUAction getCUActionAt(int ind) { 00105 00106 currentActionInd = ind; 00107 if (ind < 0) { 00108 System.out.println("Invalid index " + ind); 00109 return null; 00110 } 00111 00112 CUAction ca = new CUAction(); 00113 00114 if(ind == COMPLETED_FAIL) { 00115 GenericAction genericAction = new GenericAction(); 00116 //genericAction.actionInfo.add("finish_fail"); 00117 00118 genericAction.jsonActionInfo = SRSJSONParser.encodeCustomAction("finish_fail", null); 00119 ca.generic = genericAction; 00120 ca.actionType = "generic"; 00121 00122 ca.status = -1; 00123 00124 return ca; 00125 } 00126 else if (ind == COMPLETED_SUCCESS){ 00127 GenericAction genericAction = new GenericAction(); 00128 //genericAction.actionInfo.add("finish_success"); 00129 genericAction.jsonActionInfo = SRSJSONParser.encodeCustomAction("finish_success", null); 00130 00131 ca.generic = genericAction; 00132 ca.actionType = "generic"; 00133 00134 ca.status = 1; 00135 00136 return ca; 00137 } 00138 else if (ind == INVALID_INDEX) { 00139 GenericAction genericAction = new GenericAction(); 00140 //genericAction.actionInfo.add("no_action"); 00141 genericAction.jsonActionInfo = SRSJSONParser.encodeCustomAction("no_action", null); 00142 00143 ca.generic = genericAction; 00144 ca.actionType = "generic"; 00145 00146 ca.status = -1; 00147 return ca; 00148 } 00149 00150 GenericAction genericAction = new GenericAction(); 00151 try { 00152 genericAction = actionUnits.get(ind); 00153 } 00154 catch(IndexOutOfBoundsException e) { 00155 return null; 00156 } 00157 00158 ca.generic = genericAction; 00159 00160 ca.actionType = "generic"; 00161 return ca; 00162 } 00163 00164 public boolean addFeedback(String key, ActionFeedback fb) { 00165 if(fb == null) { 00166 return false; 00167 } 00168 feedbacks.put(key, fb); 00169 return true; 00170 } 00171 00172 public ActionFeedback getFeedback(String key) { 00173 return feedbacks.get(key); 00174 } 00175 00176 // a not very safe, but flexible way to assign parameters, using arraylist<string> 00177 //public abstract boolean setParameters(ArrayList<String> para); 00178 public abstract boolean ifParametersSet(); 00179 00180 public boolean setParameters(String action, String para, String reservedParam) { 00181 return this.ifParametersSet; 00182 } 00183 00184 protected String actionType = ""; 00185 protected ArrayList<GenericAction> actionUnits = new ArrayList<GenericAction>(); 00186 protected int[] nextActionMapIfFail; 00187 protected int[] nextActionMapIfSuccess; 00188 protected HashMap<String, ActionFeedback> feedbacks; 00189 00190 protected int currentActionInd = -1; 00191 protected boolean ifParametersSet; 00192 }