diff options
-rw-r--r-- | C2W/C2W_Demo.cpp | 69 | ||||
-rw-r--r-- | ChangeLog | 37 | ||||
-rw-r--r-- | LogGraphOut.cpp | 5 | ||||
-rw-r--r-- | PlanCommands.h | 6 | ||||
-rw-r--r-- | Planner.cpp | 13 | ||||
-rw-r--r-- | Planner.h | 15 | ||||
-rw-r--r-- | SANet/SANet.cpp | 42 | ||||
-rw-r--r-- | SANet/SANet.h | 30 | ||||
-rw-r--r-- | SANet/SANode.cpp | 9 | ||||
-rw-r--r-- | SANet/SANode.h | 17 | ||||
-rw-r--r-- | SAPOP_w_ACE.mpc | 4 | ||||
-rw-r--r-- | SA_POP_Types.h | 18 | ||||
-rw-r--r-- | SA_POP_Utils.cpp | 352 | ||||
-rw-r--r-- | SA_POP_Utils.h | 177 | ||||
-rw-r--r-- | SA_PlanCommands.cpp | 178 | ||||
-rw-r--r-- | SA_PlanCommands.h | 54 | ||||
-rw-r--r-- | SA_PlanHeuristics.cpp | 6 | ||||
-rw-r--r-- | SA_PlanStrategy.cpp | 265 | ||||
-rw-r--r-- | SA_PlanStrategy.h | 13 | ||||
-rw-r--r-- | SA_SchedStrategy.cpp | 8 | ||||
-rw-r--r-- | SA_WorkingPlan.cpp | 875 | ||||
-rw-r--r-- | SA_WorkingPlan.h | 23 | ||||
-rw-r--r-- | WorkingPlan.h | 2 |
23 files changed, 2069 insertions, 149 deletions
diff --git a/C2W/C2W_Demo.cpp b/C2W/C2W_Demo.cpp index a49d4d55560..eeac07d8cc9 100644 --- a/C2W/C2W_Demo.cpp +++ b/C2W/C2W_Demo.cpp @@ -23,6 +23,7 @@ #include "SANet/SANetFileIn.h" #include "LogScreenOut.h" #include "LogGraphOut.h" +//#include "SA_POP_Utils.h" #include <vector> #include <map> @@ -44,7 +45,7 @@ void displayConds(SA_POP::Planner *plans, std::vector<SANet::CondID> checks, std //Produce Graph std::cout << "Create Digraph" << std::endl; cfile << "strict digraph conds {\n"; - for(int node = 0; node< checks.size(); node++) + for(size_t node = 0; node< checks.size(); node++) { std::cout << "Next Cond" << std::endl; @@ -93,6 +94,66 @@ int main (int argc, char* argv[]) std::vector<SANet::CondID> toCheck; std::map<SANet::CondID, double> condMap; +/* + SA_POP::ListMultiMap<int, int> intList; + + intList.insert(std::make_pair(1,2)); + intList.insert(std::make_pair(5,6)); + intList.insert(std::make_pair(5,7)); + + for (SA_POP::ListMultiMap<int, int>::iterator list_iter = intList.lower_bound (5);list_iter != intList.upper_bound (5);list_iter++) + { + std::cout << list_iter->second << std::endl; + } + + for (SA_POP::ListMultiMap<int, int>::iterator list_iter = intList.begin();list_iter != intList.end();list_iter++) + { + std::cout << list_iter->first << " FOR JOHN " <<list_iter->second << std::endl; + } + + intList.erase(intList.lower_bound (5), intList.upper_bound (5)); + + for (SA_POP::ListMultiMap<int, int>::iterator list_iter = intList.begin();list_iter != intList.end();list_iter++) + { + std::cout << list_iter->first << " FOR JOHN " <<list_iter->second << std::endl; + } + + intList.push_front(std::make_pair(9,9)); + intList.push_front(std::make_pair(8,8)); + intList.push_back(std::make_pair(7,7)); + + std::cout << "Front: " <<intList.front().first << " and " << intList.front().second << std::endl; + std::cout << "Back: " <<intList.back().first << " and " << intList.back().second << std::endl; + intList.pop_front(); + std::cout << "Front: " <<intList.front().first << " and " << intList.front().second << std::endl; + std::cout << "Back: " <<intList.back().first << " and " << intList.back().second << std::endl; + intList.push_front(std::make_pair(12,12)); + intList.push_front(std::make_pair(7,7)); + for (SA_POP::ListMultiMap<int, int>::iterator list_iter = intList.begin();list_iter != intList.end();list_iter++) + { + std::cout << list_iter->first << " FOR JOHN " <<list_iter->second << std::endl; + } + std::cout << "Front: " <<intList.front().first << " and " << intList.front().second << std::endl; + std::cout << "Back: " <<intList.back().first << " and " << intList.back().second << std::endl; + intList.pop_back(); + std::cout << "Front: " <<intList.front().first << " and " << intList.front().second << std::endl; + std::cout << "Back: " <<intList.back().first << " and " << intList.back().second << std::endl; + for (SA_POP::ListMultiMap<int, int>::iterator list_iter = intList.begin();list_iter != intList.end();list_iter++) + { + std::cout << list_iter->first << " FOR JOHN " <<list_iter->second << std::endl; + } + intList.push_back(std::make_pair(7,7)); + std::cout << "Front: " <<intList.front().first << " and " << intList.front().second << std::endl; + std::cout << "Back: " <<intList.back().first << " and " << intList.back().second << std::endl; + intList.erase(intList.lower_bound (7)); + std::cout << "Front: " <<intList.front().first << " and " << intList.front().second << std::endl; + std::cout << "Back: " <<intList.back().first << " and " << intList.back().second << std::endl; + for (SA_POP::ListMultiMap<int, int>::iterator list_iter = intList.begin();list_iter != intList.end();list_iter++) + { + std::cout << list_iter->first << " FOR JOHN " <<list_iter->second << std::endl; + } +*/ + // Get filenames from user. std::cout << "Task Network file: "; // sanet_filename = "../examples/output1.xml"; @@ -137,7 +198,7 @@ int main (int argc, char* argv[]) std::cerr << std::endl; std::cerr << e; } catch (...) { - std::cerr << "UNKNOWN ERROR while planning." << std::endl; + std::cerr << "UNKNOWN ERROR while building task network and task map from files." << std::endl; } @@ -187,9 +248,9 @@ int main (int argc, char* argv[]) SA_POP::LogScreenOut screen_out (std::cout); graph_out.addTracking(toCheck); planner->add_out_adapter (&graph_out); - //SA_POP::SchemaOut s_out (std::cout, *kconds); + //SA_POP::SchemaOut s_out (std::cout, *kconds); //planner->add_out_adapter (&s_out); - //planner->add_out_adapter (&screen_out); + planner->add_out_adapter (&screen_out); planner->plan (100, goal); } catch (std::string e) { diff --git a/ChangeLog b/ChangeLog index 18447a9eaf4..1a6e1a8b2c7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,40 @@ +Fri Jun 26 21:53:02 UTC 2009 Daniel L.C. Mack <daniel.l.mack@vanderbilt.edu> + + + * C2W/C2W_Demo.cpp: + Cleaned up and contains code for testing MultiMapList + + * LogGraphOut.cpp: + Changes to various display colors + + * SANet/SANet.h: + * SANet/SANet.cpp: + * SANet/SANode.h: + * SANet/SANode.cpp: + Getters and Setters for accessing components of the SANets + + + * PlanCommands.h: + * Planner.h: + * Planner.cpp: + * SAPOP_w_ACE.mpc: + * SA_POP_Types.h: + * SA_PlanCommands.h: + * SA_PlanCommands.cpp: + * SA_PlanHeuristics.cpp: + * SA_PlanStrategy.h: + * SA_PlanStrategy.cpp: + * SA_SchedStrategy.cpp: + * SA_WorkingPlan.h: + * SA_WorkingPlan.cpp: + * WorkingPlan.h: + + This is in development changes for implementation of causal threat resolution. + + * SA_POP_Utils.h: + * SA_POP_Utils.cpp: + An implementation of a MultiMap with List like capabilities(i.e. ordering purposes) + Fri Jun 12 20:58:24 UTC 2009 John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu> * SANet/SANetFileIn.cpp: diff --git a/LogGraphOut.cpp b/LogGraphOut.cpp index 95ba262c90c..b7edb97aeeb 100644 --- a/LogGraphOut.cpp +++ b/LogGraphOut.cpp @@ -25,6 +25,7 @@ #include "Planner.h" #include <fstream> #include <vector> +//#include "Thread.h" //#include "gvc.h" //#include "mfg_draw_graph.h" //#include "mfg_graph_drawer.h" @@ -72,6 +73,7 @@ void LogGraphOut::notify_eu (SA_POP::Planner *planner) } } + unsigned long WINAPI SecondThread(PVOID pvParam) { system("dot -Tgif GViz.dot -o step.gif"); @@ -80,6 +82,7 @@ unsigned long WINAPI SecondThread(PVOID pvParam) } + // Notify about plan changes. void LogGraphOut::notify_plan (SA_POP::Planner *planner) { @@ -339,6 +342,8 @@ void LogGraphOut::notify_plan (SA_POP::Planner *planner) HANDLE hThread = CreateThread(NULL, 0, SecondThread, (PVOID) imageName, 0, &dwThreadId); CloseHandle(hThread); + //int spawn (ACE_THR_FUNC SecondThread); + //system("dot -Tgif GViz.dot -o step.gif"); //system("step.gif"); diff --git a/PlanCommands.h b/PlanCommands.h index fc1553b4c6c..a9f96e18e6b 100644 --- a/PlanCommands.h +++ b/PlanCommands.h @@ -84,6 +84,8 @@ namespace SA_POP { * @return Log text for most recent execution of command. */ virtual std::string get_log_text (void) = 0; + int choices; + protected: /// ID of this command. @@ -331,6 +333,10 @@ namespace SA_POP { * @param threat The causal link threat to resolve. */ virtual void set_threat (CLThreat &threat) = 0; + + + virtual TaskInstID get_first_task(void) = 0; + virtual TaskInstID get_second_task(void)=0; }; /** diff --git a/Planner.cpp b/Planner.cpp index d110a22ac9f..e4f552a5a36 100644 --- a/Planner.cpp +++ b/Planner.cpp @@ -49,6 +49,9 @@ cur_cmd_ (0) this->plan_.sched_links.clear (); this->plan_.task_insts.clear (); this->plan_.threat_links.clear (); + + //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP**** + this->init_added = false; }; @@ -405,6 +408,11 @@ CondSet Planner::get_effects (TaskID task_id) return this->sanet_->get_effects (task_id); }; +SANet::LinkWeight Planner::get_link(SANet::TaskID id, SANet::CondID cond_ID) +{ + return this->sanet_->get_link(id, cond_ID); +} + // Get all tasks that satisfy a condition. TaskSet Planner::get_satisfying_tasks (Condition cond) { @@ -424,6 +432,11 @@ TaskID Planner::get_task_from_inst (TaskInstID inst_id) return this->working_plan_->get_task_from_inst (inst_id); }; +void Planner::generate_all_threats(void) +{ + this->working_plan_->generate_all_threats(); +} + // Get all current causal link threats. CLThreatSet Planner::get_all_threats (void) { diff --git a/Planner.h b/Planner.h index 1c6cbc8ae3f..b1d3b79349e 100644 --- a/Planner.h +++ b/Planner.h @@ -39,6 +39,13 @@ namespace SA_POP { */ class Planner { public: + + //KILL ME SOON + bool init_added; + + + + /// Constructor. Planner (void); @@ -75,6 +82,8 @@ namespace SA_POP { virtual void add_out_adapter (OutAdapter *out); + virtual void generate_all_threats(void); + // ************************************************************************ // Planning and changed planning info accessor methods. @@ -153,7 +162,7 @@ namespace SA_POP { /// Satisfy scheduling constraints in fully instantiated plan (no /// recursive call backs). /** - * @param task_inst Current task instance being tried in the plan. + * * * @return True if fully satisfied plan found, false otherwise. */ @@ -320,6 +329,8 @@ namespace SA_POP { */ virtual CondSet get_effects (TaskID task_id); + SANet::LinkWeight get_link(SANet::TaskID id, SANet::CondID cond_ID); + /// Get all tasks that satisfy a condition. /** * @param cond_id The condition id. @@ -510,6 +521,7 @@ namespace SA_POP { virtual void print_graph (std::basic_ostream<char, std::char_traits<char> >& strm, std::map<std::string, std::string>& graphmap); + protected: /// Threshold for current probability of a condition to be satisfied. const Probability cond_prob_thresh_; @@ -546,6 +558,7 @@ namespace SA_POP { /// Notify all output adapters that plans have changed. virtual void notify_plan_changed (void); + }; }; /* SA_POP namespace */ diff --git a/SANet/SANet.cpp b/SANet/SANet.cpp index d87268acbc1..163ceda79a8 100644 --- a/SANet/SANet.cpp +++ b/SANet/SANet.cpp @@ -71,6 +71,48 @@ void SANet::Network::add_task (TaskID ID, std::string name, MultFactor atten_fac } }; +Probability SANet::Network::get_prior(TaskID ID) +{ + TaskNodeMap::iterator task_iter = task_nodes_.find (ID); + if (task_iter == task_nodes_.end ()) { + throw UnknownNode (); + } + TaskNode *task_node = task_iter->second; + + return task_node->get_prior(); + +} + +LinkWeight SANet::Network::get_link(TaskID ID, CondID cond_ID) +{ + TaskNodeMap::iterator task_iter = task_nodes_.find (ID); + if (task_iter == task_nodes_.end ()) { + throw UnknownNode (); + } + TaskNode *task_node = task_iter->second; + SANet::LinkMap lmap = task_node->get_post(); + + LinkMap::iterator lIter =lmap.find(cond_ID); + if (task_iter == task_nodes_.end ()) { + throw UnknownNode (); + } + LinkWeight curWeight = lIter->second; + + return curWeight; + +} + +void SANet::Network::update_prior(TaskID tID, Probability prior) +{ + TaskNodeMap::iterator task_iter = task_nodes_.find (tID); + if (task_iter == task_nodes_.end ()) { + throw UnknownNode (); + } + TaskNode *task_node = task_iter->second; + + task_node->update_prior(prior); + +} void SANet::Network::add_cond (CondID ID, std::string name, MultFactor atten_factor, Probability true_prob, Probability false_prob, diff --git a/SANet/SANet.h b/SANet/SANet.h index d4dc6baba88..fc0810aabf4 100644 --- a/SANet/SANet.h +++ b/SANet/SANet.h @@ -67,6 +67,24 @@ namespace SANet { virtual void add_task (TaskID ID, std::string name, MultFactor atten_factor, TaskCost cost, Probability prior_prob); + /// Get a prior for the task node in the network. + /** + * @param ID Node ID. + * + * + * @returns Probability of the prior. + */ + virtual Probability get_prior (TaskID ID); + + /// Get a link weight for an effect in the network. + /** + * @param ID Task ID. + * @param ID Cond ID. + * + * @returns LinkWeight of the link. + */ + virtual LinkWeight get_link(TaskID ID, CondID cond_ID); + /// Add a new condition node to the network. /** * @param ID Node ID. @@ -132,9 +150,19 @@ namespace SANet { * @param port_ID ID of port (on task) associated with this condition * (used for data nodes). */ - void update_effect_link(TaskID task_ID, CondID cond_ID, + virtual void update_effect_link(TaskID task_ID, CondID cond_ID, LinkWeight weight, PortID port_ID= ""); + /// Update the task prior. + /** + * + * @param task_ID Task node ID. + * + * + * @param prior new prior to be used for the task; + */ + virtual void update_prior(TaskID task_ID, Probability prior); + // ************************************************************************ // Print methods. diff --git a/SANet/SANode.cpp b/SANet/SANode.cpp index 3333c029b91..a9280952a09 100644 --- a/SANet/SANode.cpp +++ b/SANet/SANode.cpp @@ -729,6 +729,15 @@ void TaskNode::update_effect (CondID ID, CondNode *node, LinkWeight weight) node->update_pre_link (ID_, this, weight); }; +Probability TaskNode::get_prior (void) +{ + return prior_prob_; +} + +void TaskNode::update_prior (Probability prior) +{ + prior_prob_ = prior; +} CondNode::CondNode (CondID ID, std::string name, MultFactor atten_factor, Probability true_prob, Probability false_prob, Utility goal_util, CondKind condkind) diff --git a/SANet/SANode.h b/SANet/SANode.h index 75e12221948..8dba108d618 100644 --- a/SANet/SANode.h +++ b/SANet/SANode.h @@ -281,6 +281,23 @@ namespace SANet { */ virtual Utility get_utility (int step); + /// Get Prior of the TaskNode. + /** + * + * + * @return Expected utility. + */ + virtual Probability get_prior (void); + + /// Update Prior of the TaskNode. + /** + * + * @param prior the new prior for the task + * + * + */ + virtual void update_prior (Probability prior); + /// Add precondition link. /** * @param ID Node ID. diff --git a/SAPOP_w_ACE.mpc b/SAPOP_w_ACE.mpc index 2e9463346b2..3fed96f461c 100644 --- a/SAPOP_w_ACE.mpc +++ b/SAPOP_w_ACE.mpc @@ -29,6 +29,8 @@ project(SA_POP) : xerces, acelib { SA_POP_Types.h SA_POP_Exceptions.h + SA_POP_Utils.h + Builder.h SA_Builder.h @@ -66,6 +68,8 @@ project(SA_POP) : xerces, acelib { Source_Files { SA_POP_Exceptions.cpp + SA_POP_Utils.cpp + SA_Builder.cpp Planner.cpp diff --git a/SA_POP_Types.h b/SA_POP_Types.h index ee45b9ee24b..9ff2defbeca 100644 --- a/SA_POP_Types.h +++ b/SA_POP_Types.h @@ -20,6 +20,8 @@ #include <map> #include <sstream> +#include "SA_POP_Utils.h" + #if defined (SA_POP_HAS_ACE) #include "ace/Log_Msg.h" #include "ace/Log_Priority.h" @@ -293,9 +295,7 @@ namespace SA_POP { /// Type of a set of conditions (condition & value). typedef std::set<Condition> CondSet; - /// Type of a set of open conditions, each associated with task instances - /// for which it is a precondition. - typedef std::multimap<Condition, TaskInstID> OpenCondMap; + /// Type of a set of causal link threats. typedef std::set<CLThreat> CLThreatSet; @@ -454,6 +454,18 @@ namespace SA_POP { bool operator< (const Goal &s) const { return this->goal_id < s.goal_id; }; }; + + + + + /// Type of a set of open conditions, each associated with task instances + /// for which it is a precondition. +// typedef std::multimap<Condition, TaskInstID> OpenCondMap; + + /// Type of a set of open conditions, each associated with task instances + /// for which it is a precondition. With new list functionality + typedef SA_POP::ListMultiMap<Condition, TaskInstID> OpenCondMap; + inline std::string to_string(int x) { std::ostringstream o; diff --git a/SA_POP_Utils.cpp b/SA_POP_Utils.cpp new file mode 100644 index 00000000000..e018ccc3109 --- /dev/null +++ b/SA_POP_Utils.cpp @@ -0,0 +1,352 @@ +// +// $Id$ +// + +//============================================================================= +/** + * @file SA_POP_Types.cpp + * + * This file contains the implementation of class types used throughout SA-POP. + * + * @author John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu> + */ +//============================================================================= + +#ifndef SA_POP_UTILS_CPP_ +#define SA_POP_UTILS_CPP_ + +#include "SA_POP_Utils.h" + +//using namespace SA_POP; + +template<typename L, typename T> +SA_POP::ListMultiMap<L,T>::ListMultiMap (SA_POP::FrontBack append_direction, SA_POP::FrontBack remove_direction) +: append_dir_ (append_direction), +remove_dir_(remove_direction) +{ + multimap_.clear(); + list_.clear (); +}; + + +// Map Insert +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::insert(const typename SA_POP::ListMultiMap<L,T>::value_type val) +{ + + if(append_dir_ == SA_POP::BACK) + { + this->list_.push_back(val); + } + else if(append_dir_ == SA_POP::FRONT) + { + this->list_.push_front(val); + } + else + { + throw "ListMultiMap<L,T>::iterator ListMultiMap<L,T>::insert(const typename ListMultiMap<L,T>::value_type val): Unknown direction to append."; + } + + return this->multimap_.insert (val); + + +}; + + +// Map Erase +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::size_type SA_POP::ListMultiMap<L,T>::erase(const typename SA_POP::ListMultiMap<L,T>::key_type& key) +{ + + //Removal all list items with matching key in the pair + + for(SA_POP::ListMultiMap<L,T>::list_iterator l_iter = list_.begin(); l_iter != list_.end();) + { + SA_POP::ListMultiMap<L,T>::list_iterator prev_iter = l_iter; + l_iter++; + if((*prev_iter).first == key) + { + this->list_.erase(prev_iter); + } + } + + + return this->multimap_.erase (key); +}; + +template<typename L, typename T> +void SA_POP::ListMultiMap<L,T>::erase(typename SA_POP::ListMultiMap<L,T>::iterator _Where) +{ + if(_Where != this->end()) + { + //Removal first or last list items with matching key in the pair + if(remove_dir_ == SA_POP::FRONT) + { + for(SA_POP::ListMultiMap<L,T>::list_iterator l_iter = list_.begin(); l_iter != list_.end();) + { + SA_POP::ListMultiMap<L,T>::list_iterator prev_iter = l_iter; + l_iter++; + if((*prev_iter).first == _Where->first && (*prev_iter).second == _Where->second ) + { + this->list_.erase(prev_iter); + break; + } + } + } + else if(remove_dir_ == SA_POP::BACK) + { + SA_POP::ListMultiMap<L,T>::list_iterator last_iter = list_.end(); + for(SA_POP::ListMultiMap<L,T>::list_iterator l_iter = list_.begin(); l_iter != list_.end();l_iter++) + { + if((*l_iter).first == _Where->first && (*l_iter).second == _Where->second) + { + last_iter = l_iter; + } + } + if(last_iter != list_.end()) + { + this->list_.erase(last_iter); + } + else + { + throw "ListMultiMap<L,T>::erase(typename ListMultiMap<L,T>::iterator _Where) : Could not find value in the list"; + } + } + else + { + throw "ListMultiMap<L,T>::erase(typename ListMultiMap<L,T>::iterator _Where): Unknown direction to remove."; + } + } + this->multimap_.erase (_Where); +}; + +template<typename L, typename T> +void SA_POP::ListMultiMap<L,T>::erase(typename SA_POP::ListMultiMap<L,T>::iterator _First, typename SA_POP::ListMultiMap<L,T>::iterator _Last) +{ + + //iterate through the range [_First, _Last) and erasing each iterator in turn + if(_First != this->end()) + { + for(SA_POP::ListMultiMap<L,T>::iterator m_iter = _First; m_iter != _Last;) + { + SA_POP::ListMultiMap<L,T>::iterator prev_iter = m_iter; + m_iter++; + this->erase(prev_iter); + } + } + +}; + +// Begin +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::begin() +{ + return this->multimap_.begin (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::begin() const +{ + return this->multimap_.begin (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::reverse_iterator SA_POP::ListMultiMap<L,T>::rbegin() +{ + return this->multimap_.rbegin (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_reverse_iterator SA_POP::ListMultiMap<L,T>::rbegin() const +{ + return this->multimap_.rbegin (); +}; + +// End +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::end() +{ + return this->multimap_.end (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::end() const +{ + return this->multimap_.end (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::reverse_iterator SA_POP::ListMultiMap<L,T>::rend() +{ + return this->multimap_.rend (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_reverse_iterator SA_POP::ListMultiMap<L,T>::rend() const +{ + return this->multimap_.rend (); +}; + +// Lower Bound +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::lower_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) +{ + return this->multimap_.lower_bound (_Keyval); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::lower_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const +{ + return this->multimap_.lower_bound (_Keyval); +}; + +// Upper Bound +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::upper_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) +{ + return this->multimap_.upper_bound (_Keyval); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::upper_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const +{ + return this->multimap_.upper_bound (_Keyval); + +}; + +// Size +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::size_type SA_POP::ListMultiMap<L,T>::size() const +{ + return this->multimap_.size (); +}; + +// Empty? +template<typename L, typename T> +bool SA_POP::ListMultiMap<L,T>::empty() const +{ + return this->multimap_.empty (); +}; + +///Count +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::size_type SA_POP::ListMultiMap<L,T>::count(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const +{ + return this->multimap_.count (_Keyval); +}; + +///Clear +template<typename L, typename T> +void SA_POP::ListMultiMap<L,T>::clear() +{ + this->multimap_.clear (); + this->list_.clear (); +}; + +///Equal Range +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::_Pairii SA_POP::ListMultiMap<L,T>::equal_range(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) +{ + return this->multimap_.equal_range (_Keyval); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::_Paircc SA_POP::ListMultiMap<L,T>::equal_range(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const +{ + return this->multimap_.equal_range (_Keyval); +}; + +//List Functions + +///Erase +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::list_iterator SA_POP::ListMultiMap<L,T>::erase(typename SA_POP::ListMultiMap<L,T>::list_iterator _Where) +{ + // Erase from the multimap + if(_Where != list_.end()) + { + + for (std::multimap<L, T>::iterator m_iter = multimap_.lower_bound ((*_Where).first);m_iter != multimap_.upper_bound ((*_Where).first);) + { + std::multimap<L, T>::iterator prev_iter = m_iter; + m_iter++; + + if(prev_iter->second == (*_Where).second) + { + this->multimap_.erase(prev_iter); + break; + } + } + + } + + return this->list_.erase(_Where); + +}; + +///Front +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::list_reference SA_POP::ListMultiMap<L,T>::front() +{ + return this->list_.front (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_list_reference SA_POP::ListMultiMap<L,T>::front() const +{ + return this->list_.front (); +}; + +///Back +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::list_reference SA_POP::ListMultiMap<L,T>::back() +{ + return this->list_.back (); +}; + +template<typename L, typename T> +typename SA_POP::ListMultiMap<L,T>::const_list_reference SA_POP::ListMultiMap<L,T>::back() const +{ + return this->list_.back (); +}; + +///Push Front +template<typename L, typename T> +void SA_POP::ListMultiMap<L,T>::push_front(const typename SA_POP::ListMultiMap<L,T>::list_Ty& _Val) +{ + + this->multimap_.insert(_Val); + this->list_.push_front (_Val); +}; + +///Pop Front +template<typename L, typename T> +void SA_POP::ListMultiMap<L,T>::pop_front() +{ + + //This will erase the front item in both list and multimap. + this->erase(this->list_.begin()); + +}; + +///Push Back +template<typename L, typename T> +void SA_POP::ListMultiMap<L,T>::push_back(const typename SA_POP::ListMultiMap<L,T>::list_Ty& _Val) +{ + + this->multimap_.insert(_Val); + + this->list_.push_back (_Val); +}; + +///Pop Back +template<typename L, typename T> +void SA_POP::ListMultiMap<L,T>::pop_back() +{ + //This will erase the back item in both list and multimap. + SA_POP::ListMultiMap<L,T>::list_iterator l_iter= -- this->list_.end(); + this->erase(l_iter); +}; + + +#endif /* SA_POP_UTILS_CPP_ */ diff --git a/SA_POP_Utils.h b/SA_POP_Utils.h new file mode 100644 index 00000000000..314dfd0388a --- /dev/null +++ b/SA_POP_Utils.h @@ -0,0 +1,177 @@ + +#ifndef SA_POP_UTILS_H_ +#define SA_POP_UTILS_H_ + +//============================================================================= +/** + * @file SA_POP_Utils.h + * + * This file contains the definitions of classes that are utilized in SA_POP. + * + * @author Daniel L.C. Mack <daniel.l.mack@vanderbilt.edu> + */ +//============================================================================= + +#include <string> +#include <set> +#include <list> +#include <map> +#include <sstream> + + +namespace SA_POP { + + /// Enumerated type for front or back (appending to an ordered list). + enum FrontBack {FRONT, BACK}; + + +/** + * @class ListMultiMap + * + * @brief A mulitmap that also contains a list for ordering operations + * + */ + template<typename L, typename T> class ListMultiMap { + public: + + + typedef std::multimap<L, T> _multimap; + typedef std::list<std::pair<L, T> > _list; + + + typedef typename _multimap::key_type key_type; + typedef typename _multimap::mapped_type mapped_type; + typedef typename _multimap::referent_type referent_type; // retained + typedef typename _multimap::key_compare key_compare; + + typedef typename _multimap::value_compare value_compare; + typedef typename _multimap::allocator_type allocator_type; + typedef typename _multimap::size_type size_type; + typedef typename _multimap::difference_type difference_type; + typedef typename _multimap::pointer pointer; + typedef typename _multimap::const_pointer const_pointer; + typedef typename _multimap::reference reference; + typedef typename _multimap::const_reference const_reference; + typedef typename _multimap::iterator iterator; + typedef typename _multimap::const_iterator const_iterator; + typedef typename _multimap::reverse_iterator reverse_iterator; + typedef typename _multimap::const_reverse_iterator + const_reverse_iterator; + typedef typename _multimap::value_type value_type; + typedef typename _multimap::_Pairib _Pairib; + typedef typename _multimap::_Pairii _Pairii; + typedef typename _multimap::_Paircc _Paircc; + + + ///TypeDefs for the List + typedef typename std::pair<L, T> list_Ty; + typedef typename _list::iterator list_iterator; + typedef typename _list::const_iterator const_list_iterator; + typedef typename _list::reverse_iterator reverse_list_iterator; + typedef typename _list::const_reverse_iterator + const_reverse_list_iterator; + typedef typename _list::reference list_reference; + typedef typename _list::const_reference const_list_reference; + + //Needs to take an enumerated type for list direction. + ///Constuctor + ListMultiMap(SA_POP::FrontBack append_direction = SA_POP::FrontBack::BACK, SA_POP::FrontBack remove_direction = SA_POP::FrontBack::FRONT); + + ///Destructor + virtual ~ListMultiMap() { }; + + ///Map Insert + iterator insert(const value_type val); + + ///Map Erase + size_type erase(const key_type& key); + void erase(iterator _Where); + void erase(iterator _First, iterator _Last); + + ///Begin + iterator begin(); + + const_iterator begin() const; + + reverse_iterator rbegin(); + + const_reverse_iterator rbegin() const; + + ///End + iterator end(); + + const_iterator end() const; + + reverse_iterator rend(); + + const_reverse_iterator rend() const; + + ///Lower Bound + iterator lower_bound(const key_type& _Keyval); + + const_iterator lower_bound(const key_type& _Keyval) const; + + ///Upper Bound + iterator upper_bound(const key_type& _Keyval); + + const_iterator upper_bound(const key_type& _Keyval) const; + + /// Size + size_type size() const; + + ///Empty? + bool empty() const; + + ///Count + size_type count(const key_type& _Keyval) const; + + ///Clear + void clear(); + + ///Equal Range + _Pairii equal_range(const key_type& _Keyval); + _Paircc equal_range(const key_type& _Keyval) const; + + //List Functions + ///Erase + list_iterator erase(list_iterator _Where); + + ///Front + list_reference front(); + const_list_reference front() const; + ///Back + list_reference back(); + const_list_reference back() const; + + ///Push Front + void push_front(const list_Ty& _Val); + + ///Pop Front + void pop_front(); + + ///Push Back + void push_back(const list_Ty& _Val); + + ///Pop Back + void pop_back(); + + + + + + + private: + + SA_POP::FrontBack append_dir_; + SA_POP::FrontBack remove_dir_; + std::multimap<L, T> multimap_; + std::list<std::pair<L, T> > list_; + + + }; + +} + +#include "SA_POP_Utils.cpp" + + #endif /* SA_POP_UTILS_H_ */
\ No newline at end of file diff --git a/SA_PlanCommands.cpp b/SA_PlanCommands.cpp index fe95276c2ae..94ccc2c45c2 100644 --- a/SA_PlanCommands.cpp +++ b/SA_PlanCommands.cpp @@ -18,6 +18,8 @@ #include "SA_PlanStrategy.h" #include <string> #include <stdlib.h> +#include <fstream> +#include <iostream> using namespace SA_POP; @@ -181,10 +183,30 @@ bool SA_AddTaskCmd::execute_next (void) SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); this->undo(); + + bool isInitial = false; + + if(this->tasks_.front() == 20){ + isInitial = true; + } + if (this->tasks_.empty ()) return false; this->working_plan_->execute (this); this->num_tries_++; + + if(!this->tasks_.empty ()) + { + if(this->tasks_.front() == 20 && isInitial) { + this->tasks_.pop_front(); + } + } + + + + + + return true; }; @@ -528,15 +550,16 @@ void SA_RemoveOpenCondsCmd::set_conds (const CondSet &conds) // Constructor. SA_AddOpenThreatsCmd::SA_AddOpenThreatsCmd (SA_PlanStrategy *plan_strat) -: plan_strat_ (plan_strat) +: plan_strat_ (plan_strat), +has_executed_ (false) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + this->threats_.clear (); }; // Destructor. SA_AddOpenThreatsCmd::~SA_AddOpenThreatsCmd (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + // Nothing specific to destruct }; // Create a deep copy of this command. @@ -548,17 +571,25 @@ PlanCommand *SA_AddOpenThreatsCmd::clone (void) // Execute next option for this command. bool SA_AddOpenThreatsCmd::execute_next (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -// SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); -// this->plan_strat_->execute (this); - return false; + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); + + if (this->threats_.empty ()) + return false; + + this->undo(); + this->plan_strat_->execute (this); + this->has_executed_ = true; + return true; }; // Undo this command. void SA_AddOpenThreatsCmd::undo (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -// this->plan_strat_->undo (this); + if (!this->has_executed_ || this->threats_.empty ()) + return; + + this->plan_strat_->undo (this); + this->threats_.clear (); }; // Get log text for most recent execution of command. @@ -574,24 +605,28 @@ std::string SA_AddOpenThreatsCmd::get_log_text (void) }; // Set the open threats to add. -void SA_AddOpenThreatsCmd::set_threats (const CLThreatSet &) +void SA_AddOpenThreatsCmd::set_threats (const CLThreatSet & threats) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + if (!this->threats_.empty ()) + throw "SA_POP::SA_AddOpenThreatsCmd::set_conds (): called while current Threat set is not empty."; + + this->threats_ = threats; }; // Constructor. SA_RemoveOpenThreatsCmd::SA_RemoveOpenThreatsCmd (SA_PlanStrategy *plan_strat) -: plan_strat_ (plan_strat) +: plan_strat_ (plan_strat), +executed_ (false) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + this->threats_.clear (); }; // Destructor. SA_RemoveOpenThreatsCmd::~SA_RemoveOpenThreatsCmd (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + //Nothing to destruct }; // Create a deep copy of this command. @@ -603,17 +638,24 @@ PlanCommand *SA_RemoveOpenThreatsCmd::clone (void) // Execute next option for this command. bool SA_RemoveOpenThreatsCmd::execute_next (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -// SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); -// this->plan_strat_->execute (this); - return false; + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); + + if (this->threats_.empty ()) + return false; + + this->undo(); + this->plan_strat_->execute (this); + this->executed_ = true; + return true; }; // Undo this command. void SA_RemoveOpenThreatsCmd::undo (void) -{ - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -// this->plan_strat_->undo (this); +{ if (!(this->executed_) || (this->threats_.empty ())) + return; + + this->plan_strat_->undo (this); + this->threats_.clear (); }; // Get log text for most recent execution of command. @@ -629,9 +671,13 @@ std::string SA_RemoveOpenThreatsCmd::get_log_text (void) }; // Set the open threats to remove. -void SA_RemoveOpenThreatsCmd::set_threats (const CLThreatSet &) +void SA_RemoveOpenThreatsCmd::set_threats (const CLThreatSet & threats) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + if (!this->threats_.empty ()) + throw "SA_POP::SA_RemoveOpenThreatsCmd::set_threats (): called while current threat set is not empty."; + + + this->threats_ = threats; }; @@ -640,13 +686,13 @@ void SA_RemoveOpenThreatsCmd::set_threats (const CLThreatSet &) SA_ResolveCLThreatCmd::SA_ResolveCLThreatCmd (SA_WorkingPlan *working_plan) : working_plan_ (working_plan) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + choices = 0; }; // Destructor. SA_ResolveCLThreatCmd::~SA_ResolveCLThreatCmd (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + //No Temps Necessary, this does nothing }; // Create a deep copy of this command. @@ -658,17 +704,77 @@ PlanCommand *SA_ResolveCLThreatCmd::clone (void) // Execute next option for this command. bool SA_ResolveCLThreatCmd::execute_next (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -// SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); -// this->working_plan_->execute (this); - return false; + bool goodOption = true; + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); + if(choices == 0) + { + //Add this next line to execute function + this->working_plan_->add_sched_link(this->threat.threat,this->threat.clink.first); + choices++; + this->first = this->threat.threat; + this->second = this->threat.clink.first; + this->condition = this->threat.clink.cond; + + + goodOption = this->working_plan_->execute (this); + + if(goodOption) + { + return goodOption; + } + + } + + if(choices == 1) + { + this->undo(); + + //Add this next line to execute function + this->working_plan_->add_sched_link(this->threat.clink.second, this->threat.threat); + choices++; + this->second = this->threat.threat; + this->first = this->threat.clink.second; + this->condition = this->threat.clink.cond; + + goodOption = this->working_plan_->execute (this); + + if(goodOption) + { + return goodOption; + } + else + { + return false; + } + + + } + else + { + this->undo(); + return false; + } + + }; // Undo this command. void SA_ResolveCLThreatCmd::undo (void) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -// this->working_plan_->undo (this); + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, this->get_log_text ()); + if(choices == 1) + { + //Add this next line to execute function + this->working_plan_->remove_sched_link(this->threat.threat,this->threat.clink.first); + return this->working_plan_->undo (this); + } + else if(choices == 2) + { + //Add this next line to execute function + this->working_plan_->remove_sched_link(this->threat.clink.second, this->threat.threat); + return this->working_plan_->undo (this); + } }; // Get log text for most recent execution of command. @@ -684,11 +790,17 @@ std::string SA_ResolveCLThreatCmd::get_log_text (void) }; // Set the causal link threat to resolve. -void SA_ResolveCLThreatCmd::set_threat (CLThreat &) +void SA_ResolveCLThreatCmd::set_threat (CLThreat & tht) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + threat = tht; }; +// Set the task instances to order. +void SA_ResolveCLThreatCmd::set_task_insts (TaskInstID task_inst_a, TaskInstID task_inst_b) +{ + this->first = task_inst_a; + this->second = task_inst_b; +}; // Constructor. diff --git a/SA_PlanCommands.h b/SA_PlanCommands.h index adb6be44cfa..e4024155afe 100644 --- a/SA_PlanCommands.h +++ b/SA_PlanCommands.h @@ -355,6 +355,8 @@ namespace SA_POP { SA_AdjustMinTimesCmd min_adj_cmd; SA_AdjustMaxTimesCmd max_adj_cmd; + bool got_to_scheduling; + // The changes made in the precedence set due to the selection of this task instance // are recorded in these sets. std::set< std::pair<TaskInstID,TaskInstID> > causal_insertions; @@ -541,6 +543,12 @@ namespace SA_POP { /// PlanStrategy object that this command works on. SA_PlanStrategy *plan_strat_; + /// Set of Threats to add to open threats. + CLThreatSet threats_; + + /// Flag for whether this has been executed. + bool has_executed_; + }; /** @@ -596,6 +604,14 @@ namespace SA_POP { /// PlanStrategy object that this command works on. SA_PlanStrategy *plan_strat_; + /// Set of threats to remove from open threats. + CLThreatSet threats_; + + /// Flag for whether this has been executed. + bool executed_; + + + }; /** @@ -646,10 +662,47 @@ namespace SA_POP { */ virtual void set_threat (CLThreat &threat); + + /// Set the task instances to order. + /** + * @param task_inst_a One task instance. + * + * @param task_inst_b The other task instance. + */ + virtual void set_task_insts (TaskInstID task_inst_a, + TaskInstID task_inst_b); + + // int choices; + // A Type of a list of ResolveSchedOrderCmd pointers + typedef std::list<SA_ResolveCLThreatCmd* > ResolveCLThreatCmdList; + + virtual TaskInstID get_first_task(void){return first;} + virtual TaskInstID get_second_task(void){return second;} + protected: /// WorkingPlan object that this command works on. SA_WorkingPlan *working_plan_; + CLThreat threat; + // The first task instance is scheduled before the second one + TaskInstID first,second; + Condition condition; + + std::map <TaskInstID, TaskInstSet> befores; + std::map <TaskInstID, TaskInstSet> afters; + std::map <TaskInstID, TaskInstSet> simuls; + std::map <TaskInstID, TaskInstSet> unrankeds; + + bool got_to_change_precedences; + + + // The min and the max times changed due to this command + SA_AdjustMinTimesCmd *adj_min_times_cmd_; + SA_AdjustMaxTimesCmd *adj_max_times_cmd_; + + // A list of all the scheduling orderings introduced by this command + // This may need to change for a list of ResolveThreatCmd List + ResolveCLThreatCmdList cmds_; }; /** @@ -718,6 +771,7 @@ namespace SA_POP { // The first task instance is scheduled before the second one TaskInstID first,second; + // The min and the max times changed due to this command SA_AdjustMinTimesCmd *adj_min_times_cmd_; SA_AdjustMaxTimesCmd *adj_max_times_cmd_; diff --git a/SA_PlanHeuristics.cpp b/SA_PlanHeuristics.cpp index 115690ad6bd..21e1c7be467 100644 --- a/SA_PlanHeuristics.cpp +++ b/SA_PlanHeuristics.cpp @@ -46,7 +46,7 @@ Condition SA_CondStrategy::choose_cond (const OpenCondMap &open_conds) } // If no data conditions, just return first condition. - return open_conds.begin ()->first; + return open_conds.front().first; }; @@ -69,6 +69,10 @@ TaskList SA_TaskStrategy::choose_task (Condition open_cond) { TaskSet tasks = this->planner_->get_satisfying_tasks (open_cond); + if(this->planner_->init_added){ + tasks.erase(20); + } + // Add tasks to map with EU (to sort). std::map<EUCalc, TaskID> task_map; task_map.clear (); diff --git a/SA_PlanStrategy.cpp b/SA_PlanStrategy.cpp index 6cc9024686e..38410b6997a 100644 --- a/SA_PlanStrategy.cpp +++ b/SA_PlanStrategy.cpp @@ -19,6 +19,7 @@ #include "Planner.h" #include "PlanHeuristics.h" #include "PlanCommands.h" +#include <fstream> using namespace SA_POP; @@ -42,7 +43,8 @@ add_threats_cmd_ (0), rmv_threats_cmd_ (0), add_task_cmd_ (0), assoc_impl_cmd_ (0), -resolve_threat_cmd_ (0) +resolve_threat_cmd_ (0), +open_conds_(SA_POP::FrontBack::BACK, SA_POP::FrontBack::FRONT) { this->add_conds_cmd_ = new SA_AddOpenCondsCmd (this); this->rmv_conds_cmd_ = new SA_RemoveOpenCondsCmd (this); @@ -167,27 +169,36 @@ bool SA_PlanStrategy::satisfy_open_conds (void) // Add preconditions of this task of we didn't reuse the task instance. CommandID add_preconds_cmd_id; - if(!add_task_cmd->inst_exists()) - { - preconds = this->planner_->get_unsat_preconds (this->cur_task_); - add_preconds_cmd_id = + if(!add_task_cmd->inst_exists()) + { + preconds = this->planner_->get_unsat_preconds (this->cur_task_); + add_preconds_cmd_id = this->add_open_conds (preconds, this->cur_task_inst_); - } + } + //Move this code to the threat resolution sequence // Set decision point and reset sequence number for commands. this->cur_decision_pt_ = SA_PlanStrategy::THREAT_DECISION; this->cur_seq_num_ = 1; + + + //Deal with threats + + //Actually build the list of threats + this->planner_->generate_all_threats(); + // Add causal link threats to open threats. - bool are_threats = !this->planner_->get_all_threats ().empty (); + bool are_threats = !(this->planner_->get_all_threats().empty ()); CommandID add_threats_cmd_id; if (are_threats) add_threats_cmd_id = this->add_open_threats (this->planner_->get_all_threats ()); // Try to satisfy threats and continue recursive planning. - if (this->satisfy_open_threats ()) + if (this->satisfy_everything()) return true; + SA_POP_DEBUG(SA_POP_DEBUG_NORMAL, "Backtracking from task addition..."); // Undo addition of causal link threats from this task. if (are_threats) @@ -213,6 +224,138 @@ bool SA_PlanStrategy::satisfy_open_conds (void) return false; }; +bool SA_PlanStrategy::satisfy_everything(){ + + //for each implementation + this->cur_decision_pt_ = SA_PlanStrategy::IMPL_DECISION; + this->cur_seq_num_ = 1; + + AssocTaskImplCmd *assoc_impl_cmd; + TaskImplList impl_list; + + // Choose a task implementation. + assoc_impl_cmd = + static_cast<AssocTaskImplCmd *> (this->assoc_impl_cmd_->clone ()); + if(!this->planner_->inst_exists(this->cur_task_inst_)) impl_list = this->impl_choice_->choose_impl (this->cur_task_inst_); + else impl_list.push_back(this->planner_->get_impl_id(this->cur_task_inst_)); + assoc_impl_cmd->set_id (this->get_next_cmd_id ()); + assoc_impl_cmd->set_assoc (this->cur_task_inst_, impl_list); + this->planner_->add_command (assoc_impl_cmd); + this->cur_task_inst_ = assoc_impl_cmd->get_task_inst (); + + while (this->planner_->try_next (assoc_impl_cmd->get_id ())) + { + if(this->get_next_threat_resolution()){ + return true; + } + else{ + //this->planner_->undo_command(assoc_impl_cmd->get_id()); + + this->cur_decision_pt_ = SA_PlanStrategy::IMPL_DECISION; + } + // assoc_impl_cmd = + // static_cast<AssocTaskImplCmd *> (this->assoc_impl_cmd_->clone ()); +// if(!this->planner_->inst_exists(this->cur_task_inst_)) impl_list = this->impl_choice_->choose_impl (this->cur_task_inst_); +// else impl_list.push_back(this->planner_->get_impl_id(this->cur_task_inst_)); +// assoc_impl_cmd->set_id (this->get_next_cmd_id ()); + // impl_list.pop_front(); +// assoc_impl_cmd->set_assoc (this->cur_task_inst_, impl_list); + // this->planner_->add_command (assoc_impl_cmd); +// this->cur_task_inst_ = assoc_impl_cmd->get_task_inst (); + + } + + //Undo the AssocImplCmd + + return false; +} +bool SA_PlanStrategy::satisfy_schedule(void){ + + + // Set decision point and reset sequence number for commands. + this->cur_decision_pt_ = SA_PlanStrategy::SCHEDULE_DECISION; + this->cur_seq_num_ = 1; + + // Try to schedule and recursively continue planning. + if (this->planner_->recurse_sched (this->cur_task_inst_)) + return true; + + + return false; +} + +bool SA_PlanStrategy::get_next_threat_resolution(){ + + //TODO this just takes the first threat resolution and gives up otherwise. fix soon + + // Choose an open threat to satisfy and remove from open threats. + + if(open_threats_.empty()){ + this->cur_decision_pt_ = SA_PlanStrategy::THREAT_DECISION; + return satisfy_schedule(); + } + this->cur_decision_pt_ = SA_PlanStrategy::THREAT_DECISION; + CLThreat threat = *this->open_threats_.begin (); + CommandID rmv_threat_cmd_id = this->rmv_open_threat (threat); + + //Should have been done in the command + //this->open_threats_.erase(threat); + + + this->cur_decision_pt_ = SA_PlanStrategy::THREAT_DECISION; + // this->cur_seq_num_ = 1; + + // Create threat resolution command and add to planner. + ResolveCLThreatCmd *resolve_threat_cmd = + static_cast<ResolveCLThreatCmd *> (this->resolve_threat_cmd_->clone ()); + resolve_threat_cmd->set_id (this->get_next_cmd_id ()); + resolve_threat_cmd->set_threat (threat); + this->planner_->add_command (resolve_threat_cmd); + + // if(this->planner_->try_next (resolve_threat_cmd->get_id ())) + + + + while(this->planner_->try_next(resolve_threat_cmd->get_id())){ + + + if (this->get_next_threat_resolution ()) + return true; + + } + + this->cur_decision_pt_ = SA_PlanStrategy::THREAT_DECISION; + this->planner_->undo_command (resolve_threat_cmd->get_id ()); + + // resolve_threat_cmd = + // static_cast<ResolveCLThreatCmd *> (this->resolve_threat_cmd_->clone ()); + // resolve_threat_cmd->set_id (this->get_next_cmd_id ()); + // resolve_threat_cmd->set_threat (threat); + // this->planner_->add_command (resolve_threat_cmd); + + // resolve_threat_cmd->choices = 1; + + //Second time will try reverse direection + // if(this->planner_->try_next (resolve_threat_cmd->get_id ())) + // if (this->get_next_threat_resolution ()) + // return true; + + + // Undo threat resolution. + // this->planner_->undo_command (resolve_threat_cmd->get_id ()); + + + + this->open_threats_.insert(threat); + + // // Undo removal of open threat. + this->planner_->undo_command (rmv_threat_cmd_id); + + return false; + +} + + // Get a PlanCommand prototype for adding open conditions, // which works on this strategy. AddOpenCondsCmd *SA_PlanStrategy::get_AddOpenCondsCmd (void) @@ -309,92 +452,60 @@ void SA_PlanStrategy::undo (SA_RemoveOpenCondsCmd *cmd) }; // Execute a command to add causal link threats to planning. -void SA_PlanStrategy::execute (SA_AddOpenThreatsCmd *) +void SA_PlanStrategy::execute (SA_AddOpenThreatsCmd *cmd) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + for (CLThreatSet::iterator iter = cmd->threats_.begin (); + iter != cmd->threats_.end (); iter++) + { + CLThreat threat = *iter; + if(this->closed_threats_.find(threat) == this->closed_threats_.end()) + { + this->open_threats_.insert (threat); + } + } }; // Undo a command to add causal link threats to planning. -void SA_PlanStrategy::undo (SA_AddOpenThreatsCmd *) +void SA_PlanStrategy::undo (SA_AddOpenThreatsCmd * cmd) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + // Remove open conditions mapped to the specified task instance. + std::cout<<"undoing open threats" <<std::endl; + for (CLThreatSet::iterator cond_iter = cmd->threats_.begin (); + cond_iter != cmd->threats_.end (); cond_iter++) + { + CLThreat threat = *cond_iter; + this->open_threats_.erase (threat); + } }; // Execute a command to remove causal link threats from planning. -void SA_PlanStrategy::execute (SA_RemoveOpenThreatsCmd *) +void SA_PlanStrategy::execute (SA_RemoveOpenThreatsCmd * cmd) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + // Remove open conditions mapped to the specified task instance. + std::cout<<"removing open threats" <<std::endl; + for (CLThreatSet::iterator cond_iter = cmd->threats_.begin (); + cond_iter != cmd->threats_.end (); cond_iter++) + { + CLThreat threat = *cond_iter; + this->open_threats_.erase (threat); + this->closed_threats_.insert (threat); + } }; // Undo a command to remove causal link threats from planning. -void SA_PlanStrategy::undo (SA_RemoveOpenThreatsCmd *) -{ - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -}; - -// Recursively satisfy all open causal link threats and continue planning. -bool SA_PlanStrategy::satisfy_open_threats (void) +void SA_PlanStrategy::undo (SA_RemoveOpenThreatsCmd * cmd) { - if (this->open_threats_.empty ()) { - // Set decision point and reset sequence number for commands. - this->cur_decision_pt_ = SA_PlanStrategy::IMPL_DECISION; - this->cur_seq_num_ = 1; - - // Choose a task implementation. - AssocTaskImplCmd *assoc_impl_cmd = - static_cast<AssocTaskImplCmd *> (this->assoc_impl_cmd_->clone ()); - TaskImplList impl_list; - if(!this->planner_->inst_exists(this->cur_task_inst_)) impl_list = this->impl_choice_->choose_impl (this->cur_task_inst_); - else impl_list.push_back(this->planner_->get_impl_id(this->cur_task_inst_)); - assoc_impl_cmd->set_id (this->get_next_cmd_id ()); - assoc_impl_cmd->set_assoc (this->cur_task_inst_, impl_list); - this->planner_->add_command (assoc_impl_cmd); - - // Try task implementations until one yields a complete plan or all have - // been tried. - while (this->planner_->try_next (assoc_impl_cmd->get_id ())) { - // Get the task instance. - this->cur_task_inst_ = assoc_impl_cmd->get_task_inst (); - // Set decision point and reset sequence number for commands. - this->cur_decision_pt_ = SA_PlanStrategy::SCHEDULE_DECISION; - this->cur_seq_num_ = 1; - - // Try to schedule and recursively continue planning. - if (this->planner_->recurse_sched (this->cur_task_inst_)) - return true; - } - - // No task implementation worked so return failure. - return false; - } - - // Choose an open threat to satisfy and remove from open threats. - CLThreat threat = *this->open_threats_.begin (); - CommandID rmv_threat_cmd_id = this->rmv_open_threat (threat); - - // Create threat resolution command and add to planner. - ResolveCLThreatCmd *resolve_threat_cmd = - static_cast<ResolveCLThreatCmd *> (this->resolve_threat_cmd_->clone ()); - resolve_threat_cmd->set_id (this->get_next_cmd_id ()); - resolve_threat_cmd->set_threat (threat); - this->planner_->add_command (resolve_threat_cmd); - - // Try threat resolutions until one yields a complete plan or all have been - // tried. - while (this->planner_->try_next (resolve_threat_cmd->get_id ())) { - if (this->satisfy_open_threats ()) - return true; + for (CLThreatSet::iterator iter = cmd->threats_.begin (); + iter != cmd->threats_.end (); iter++) + { + CLThreat threat = *iter; + this->closed_threats_.erase (threat); + this->open_threats_.insert (threat); } +}; - // Undo threat resolution. - this->planner_->undo_command (resolve_threat_cmd->get_id ()); - // Undo removal of open threat. - this->planner_->undo_command (rmv_threat_cmd_id); - // No threat resolution was successful so return failure. - return false; -}; // Satisfy an open condition with an appropriate task. AddTaskCmd *SA_PlanStrategy::satisfy_cond (Condition open_cond) diff --git a/SA_PlanStrategy.h b/SA_PlanStrategy.h index 3d4fcebede3..1ef51953a1e 100644 --- a/SA_PlanStrategy.h +++ b/SA_PlanStrategy.h @@ -183,6 +183,12 @@ namespace SA_POP { */ virtual void undo (SA_RemoveOpenThreatsCmd *cmd); + //TODO ben's crap + + virtual bool satisfy_everything(); + virtual bool satisfy_schedule(void); + virtual bool get_next_threat_resolution(); + protected: // ************************************************************************ // State information. @@ -198,6 +204,9 @@ namespace SA_POP { /// Set of open causal link threats. CLThreatSet open_threats_; + /// Set of open causal link threats. + CLThreatSet closed_threats_; + /// ID of current task being tried (to satisfy an open condition). TaskID cur_task_; @@ -281,8 +290,6 @@ namespace SA_POP { /// plan (with promotion or demotion). ResolveCLThreatCmd *resolve_threat_cmd_; - - // ************************************************************************ // Internal helper methods. // ************************************************************************ @@ -291,7 +298,7 @@ namespace SA_POP { /** * @return True if planning succeeded, false otherwise. */ - virtual bool satisfy_open_threats (void); +// virtual bool satisfy_open_threats (void); /// Satisfy an open condition with an appropriate task. /** diff --git a/SA_SchedStrategy.cpp b/SA_SchedStrategy.cpp index ed333eb1864..fd3f0c456bc 100644 --- a/SA_SchedStrategy.cpp +++ b/SA_SchedStrategy.cpp @@ -17,6 +17,7 @@ #include "Planner.h" #include <list> #include <set> +#include <fstream> using namespace SA_POP; // Constructor. @@ -91,11 +92,16 @@ bool SA_SchedStrategy::satisfy_sched (TaskInstID task_inst) this->cur_seq_num_=1; // Do the energy propogation for this task instance // This function automatically does this for the task instances before and after it. + + if(!this->energy_prop(task_inst)) { + + this->planner_->undo_through(cur_cmd_id); return false; } + // Do the balance propogation related to time windows for this task instance // and those unranked with respect to it if(!this->time_balance_prop(task_inst)) @@ -420,7 +426,7 @@ bool SA_SchedStrategy::time_balance_prop (TaskInstID task_inst) TimeWindow temp_end = this->planner_->get_end_window(*iter); if(temp_start.second!=NULL_TIME && temp_start.second<start_win.first) { - std::cout<<"THe consumer of "<<*iter<<" has to executed before"<<std::endl; + std::cout<<"THe consumer of "<<*iter<<" has to execute before"<<std::endl; //The consumer has to be executed before this task instance ResourceMap temp_rm = this->planner_->get_all_resources(this->planner_->get_task_impl_from_inst(*iter)); ResourceMap::iterator temp_rm_iter = temp_rm.find(rm_iter->first); diff --git a/SA_WorkingPlan.cpp b/SA_WorkingPlan.cpp index 4b3c66af188..505b2d48648 100644 --- a/SA_WorkingPlan.cpp +++ b/SA_WorkingPlan.cpp @@ -18,6 +18,8 @@ #include "PlanCommands.h" #include "SA_PlanCommands.h" +#include <stack> +#include <fstream> using namespace SA_POP; @@ -541,13 +543,165 @@ TaskImplID SA_WorkingPlan::get_task_impl_from_inst(TaskInstID inst_id) return this->task_impls_.find(inst_id)->second; } +void SA_WorkingPlan::generate_all_threats(void) +{ + threat_set.clear(); + + std::ostringstream debug_text; + + + + + + debug_text << "SA_WorkingPlan::generate_all_threats: All Causal Links: " << std::endl; + + + + for(CondToCLinksMap::iterator nit = causal_links_.begin(); nit != causal_links_.end(); nit++){ + + CausalLink causal_threatened = (*nit).second; + TaskID threatened_task = this->task_insts_.find(causal_threatened.first)->second; + if(causal_threatened.second != -1) + debug_text << " Task ("<<threatened_task<<") " << "Inst ("<<causal_threatened.first<<") -(" << causal_threatened.cond.id << ")-> Task ("<<this->task_insts_.find(causal_threatened.second)->second<< ") Inst ("<< causal_threatened.second << ")" << std::endl; + + } + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + +/* + + for(InstToImplMap::iterator iterator = this->task_impls_.begin(); iterator != this->task_impls_.end(); iterator++){ + TaskInstID threat_possibility = (*iterator).first; + TaskID threat_possibility_taskid = this->task_insts_.find(threat_possibility)->second; + + file_op<<" Task :"<<iterator->first<<"("<<threat_possibility_taskid<<")"<<std::endl; + } + + + for(InstToImplMap::iterator iterator = this->task_impls_.begin(); iterator != this->task_impls_.end(); iterator++){ + + TaskInstID threat_possibility = iterator->first; + TaskID threat_possibility_taskid = this->task_insts_.find(threat_possibility)->second; + CondSet set = this->planner_->get_effects(threat_possibility_taskid); + + for(CondSet::iterator arr = set.begin(); arr != set.end(); arr++){ + + Condition condition = (*arr); + + std::pair< + CondToCLinksMap::iterator, + CondToCLinksMap::iterator + > ret = this->causal_links_.equal_range(condition); + + + for(CondToCLinksMap::iterator nit = ret.first; nit != ret.second; nit++){ + + CausalLink causal_threatened = (*nit).second; + TaskID threatened_task = this->task_insts_.find(causal_threatened.first)->second; + + SANet::LinkWeight threat_effect = this->planner_->get_link(threat_possibility_taskid, condition.id); + SANet::LinkWeight causal_effect = this->planner_->get_link(threatened_task, causal_threatened.cond.id); + + + if((threat_effect > 0 && causal_effect < 0 )|| (threat_effect < 0 && causal_effect > 0)){ + + if(causal_threatened.first != threat_possibility && causal_threatened.second != threat_possibility) + { + TaskID threatened_task1 = this->task_insts_.find(causal_threatened.first)->second; + TaskID threatened_task2 = this->task_insts_.find(causal_threatened.second)->second; + + CLThreat new_threat; + new_threat.clink = causal_threatened; + new_threat.threat = threat_possibility; + threat_set.insert(new_threat); + + file_op<<" New threat: causal link from "<<causal_threatened.first<<" ("<<threatened_task1<<") to "<<causal_threatened.second<<" ("<<threatened_task2<<") using condition "<<causal_threatened.cond.id<<" threatened by "<<threat_possibility<<" ("<<threat_possibility_taskid<<")\n"; + + } + } + } + } + } + + file_op.close(); + + +*/ + + debug_text.clear(); + + debug_text << "SA_WorkingPlan::generate_all_threats: All Tasks Instances: " << std::endl; + + for(InstToTaskMap::iterator iterator = this->task_insts_.begin(); iterator != this->task_insts_.end(); iterator++){ + TaskInstID threat_possibility = iterator->first; + TaskID threat_possibility_taskid = iterator->second; + + debug_text <<" Task (" <<threat_possibility_taskid << ")"<< ": Inst (" <<iterator->first << ")" << std::endl; + } + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + debug_text << "SA_WorkingPlan::generate_all_threats: All Causal Threats: " << std::endl; + + + for(InstToTaskMap::iterator iterator = this->task_insts_.begin(); iterator != this->task_insts_.end(); iterator++){ + TaskInstID threat_possibility = iterator->first; + TaskID threat_possibility_taskid = iterator->second; + CondSet set = this->planner_->get_effects(threat_possibility_taskid); + + for(CondSet::iterator arr = set.begin(); arr != set.end(); arr++){ + + Condition condition = (*arr); + + std::pair< + CondToCLinksMap::iterator, + CondToCLinksMap::iterator + > ret = this->causal_links_.equal_range(condition); + + + for(CondToCLinksMap::iterator nit = ret.first; nit != ret.second; nit++){ + + CausalLink causal_threatened = (*nit).second; + TaskID threatened_task = this->task_insts_.find(causal_threatened.first)->second; + + SANet::LinkWeight threat_effect = this->planner_->get_link(threat_possibility_taskid, condition.id); + SANet::LinkWeight causal_effect = this->planner_->get_link(threatened_task, causal_threatened.cond.id); + + + if((threat_effect > 0 && causal_effect < 0 )|| (threat_effect < 0 && causal_effect > 0)){ + + if(causal_threatened.first != threat_possibility && causal_threatened.second != threat_possibility) + { + TaskID threatened_task1 = this->task_insts_.find(causal_threatened.first)->second; + TaskID threatened_task2 = this->task_insts_.find(causal_threatened.second)->second; + + CLThreat new_threat; + new_threat.clink = causal_threatened; + new_threat.threat = threat_possibility; + threat_set.insert(new_threat); + + debug_text <<" Causal link from Task ("<< threatened_task1 <<") Inst ("<< causal_threatened.first <<") to Task ("<< threatened_task2 <<") Inst ("<< causal_threatened.second <<") using condition "<<causal_threatened.cond.id<<" threatened by Task ("<< threat_possibility_taskid <<") Inst ("<<threat_possibility<<")" << std::endl; + + + } + } + } + } + } + + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + + +} + + // Get all current causal link threats. CLThreatSet SA_WorkingPlan::get_all_threats (void) -{ - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP - CLThreatSet temp; - temp.clear (); - return temp; +{ + return threat_set; }; //Get all the causal and data links TO the task instance @@ -637,29 +791,54 @@ void SA_WorkingPlan::execute (SA_AddTaskCmd *cmd) { // Get task ID, condition, and instance ID. TaskID task = cmd->tasks_.front (); + + + Condition cond = cmd->cond_; TaskInstID task_inst; + + // If trying a new task (i.e., not still trying task instances for same task), get existing task instances for that task from working plan. if(cmd->last_task_ != task) { - // The task instance is reused. + // Get existing task instances for current task. for(InstToTaskMap::iterator iter = this->task_insts_.begin(); iter!=this->task_insts_.end();iter++) if(iter->second==cmd->tasks_.front()) cmd->used_task_insts_.insert(iter->first); } + + // If there are no/no-more existing task instances to try, create new task instance. if(cmd->used_task_insts_.empty()) { + if(task == 20 && this->planner_->init_added) + { + throw "Reached SA_WorkingPlan::execute (SA_AddTaskCmd *cmd) for Special Initial Action after it was already existing instance tried"; + } + + this->planner_->init_added = true; + task_inst = this->get_next_inst_id (); // Add task instance. this->task_insts_.insert (std::make_pair (task_inst, task)); - // Remove task from command and update last task and instance. - cmd->tasks_.pop_front (); + // Remove this task from tasks still to try. + cmd->tasks_.pop_front (); + + + } - else + else { // Reuse the task instance task_inst = *cmd->used_task_insts_.begin(); this->reused_insts_.insert(task_inst); + + // NOTE: task instance removed from existing task instances to try in undo. } + + std::ostringstream debug_text; + debug_text << "SA_WorkingPlan::execute (SA_AddTaskCmd *cmd): Adding task (" << task << ") instance (" << task_inst << ") for condition (" << cond.id << ")."; + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + // Create causal links for each task instance depending on the condition. for (TaskInstSet::iterator inst_iter = cmd->task_insts_.begin (); inst_iter != cmd->task_insts_.end (); inst_iter++) @@ -669,21 +848,38 @@ void SA_WorkingPlan::execute (SA_AddTaskCmd *cmd) clink.first = task_inst; clink.second = *inst_iter; CondToCLinksMap::iterator links_iter; + + // Check whether this causal link already exists in working plan. for (links_iter = this->causal_links_.lower_bound (cond);links_iter != this->causal_links_.upper_bound (cond);links_iter++) if(links_iter->second == clink) break; + // If causal link not found in working plan, add it to causal links and ordering links. if(links_iter==this->causal_links_.upper_bound(cond)) { + std::ostringstream debug_text2; + debug_text2 << "SA_WorkingPlan::execute (SA_AddTaskCmd *cmd): Adding causal link (" << clink.first << " -" << clink.cond.id << "-> " << clink.second << ")"; + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text2.str ()); + this->causal_links_.insert (std::make_pair (cond, clink)); - cmd->added_links_.insert(clink); + + if(clink.second != GOAL_TASK_INST_ID){ + this->ordering_links.insert(std::pair<TaskInstID, TaskInstID>(clink.first, clink.second)); + this->reverse_ordering_links.insert(std::pair<TaskInstID, TaskInstID>(clink.second, clink.first)); + } + cmd->added_links_.insert(clink); } } + + // Set this task and task instance as last used by command. cmd->last_task_ = task; cmd->last_task_inst_ = task_inst; }; void SA_WorkingPlan::undo (SA_AddTaskCmd *cmd) { + + + if(cmd->used_task_insts_.empty()) { // Remove task instance. @@ -694,6 +890,12 @@ void SA_WorkingPlan::undo (SA_AddTaskCmd *cmd) cmd->used_task_insts_.erase(cmd->used_task_insts_.begin()); this->reused_insts_.erase(this->reused_insts_.find(cmd->last_task_inst_)); } + + + + + + // Remove causal links. for (SA_WorkingPlan::CondToCLinksMap::iterator cl_iter = this->causal_links_.lower_bound (cmd->cond_); @@ -707,8 +909,31 @@ void SA_WorkingPlan::undo (SA_AddTaskCmd *cmd) { if(prev_iter->second == *iter) { + + CausalLink clink = prev_iter->second; + this->causal_links_.erase (prev_iter); cmd->added_links_.erase(iter); + + + std::pair<SchedulingLinks::iterator, SchedulingLinks::iterator> ret = + ordering_links.equal_range(clink.first); + SchedulingLinks::iterator it; + for(it = ret.first; it != ret.second; it++){ + if(it->second == clink.second){ + break; + } + } + this->ordering_links.erase(it); + + ret = reverse_ordering_links.equal_range(clink.second); + for(it = ret.first; it != ret.second; it++){ + if(it->second == clink.first){ + break; + } + } + this->reverse_ordering_links.erase(it); + break; } } @@ -730,19 +955,109 @@ bool SA_WorkingPlan::execute (SA_AssocTaskImplCmd *cmd) cmd->last_impl_ = cmd->impls_.front (); cmd->impls_.pop_front (); // Update the precedence graph + + //Ben changed this. undo by returning that stuff + + cmd->got_to_scheduling = true; + + if(is_cycle_in_ordering()){ + cmd->got_to_scheduling = false; + return false; + } + +/* + + std::map<TaskInstID, bool> un_visited_map; + std::map<TaskInstID, bool> visited_map; + std::stack<TaskInstID> s; + + for(InstToTaskMap::iterator it = this->task_insts_.begin(); it != this->task_insts_.end(); it++){ + un_visited_map.insert(std::pair<TaskInstID, bool>(it->first, true)); + + } + + int total_num = un_visited_map.size(); + while(!(s.size()== total_num)){ + + std::map<TaskInstID, bool>::iterator some_it = un_visited_map.begin(); + TaskInstID next = some_it->first; + dfs_aux(next, s, visited_map, un_visited_map); + } + + visited_map.clear(); + + cmd->got_to_scheduling = true; + + while(!s.empty()){ + TaskInstID next = s.top(); + s.pop(); + + if(!dfs_aux2(next, visited_map)){ + cmd->got_to_scheduling = false; + return false; + } + } +*/ + return this->init_prec_insert(cmd->task_inst_,cmd); }; + +bool SA_WorkingPlan::is_cycle_in_ordering(){ + + std::ostringstream debug_text; + + + + std::map<TaskInstID, bool> un_visited_map; + std::map<TaskInstID, bool> visited_map; + std::stack<TaskInstID> s; + + for(InstToTaskMap::iterator it = this->task_insts_.begin(); it != this->task_insts_.end(); it++){ + un_visited_map.insert(std::pair<TaskInstID, bool>(it->first, true)); + + } + + int total_num = un_visited_map.size(); + while(!(s.size()== total_num)){ + + std::map<TaskInstID, bool>::iterator some_it = un_visited_map.begin(); + TaskInstID next = some_it->first; + dfs_aux(next, s, visited_map, un_visited_map); + } + + visited_map.clear(); + + + while(!s.empty()){ + TaskInstID next = s.top(); + s.pop(); + + if(!dfs_aux2(next, visited_map)){ + + + debug_text << "SA_WorkingPlan::is_cycle_in_ordering: FOUND A LOOP IN THREAT SCHEDULE" << std::endl; + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + return true; + } + } + + return false; +} + // Undo a command to associate an implementation with a // task instance in the plan. void SA_WorkingPlan::undo (SA_AssocTaskImplCmd *cmd) { // Undo the time window adjustments and the precedence graph updations. - this->undo(&cmd->max_adj_cmd); - this->undo(&cmd->min_adj_cmd); - this->prec_erase(cmd->task_inst_,cmd); - cmd->causal_insertions.clear(); - cmd->simul_insertions.clear(); + if(cmd->got_to_scheduling){ + this->undo(&cmd->max_adj_cmd); + this->undo(&cmd->min_adj_cmd); + this->prec_erase(cmd->task_inst_,cmd); + cmd->causal_insertions.clear(); + cmd->simul_insertions.clear(); + } + if(this->reused_insts_.find(cmd->task_inst_)==this->reused_insts_.end()) { this->durations.erase(this->durations.find(cmd->task_inst_)); @@ -752,17 +1067,518 @@ void SA_WorkingPlan::undo (SA_AssocTaskImplCmd *cmd) // Execute a command to resolve a causal link threat in the // plan (with promotion or demotion). -void SA_WorkingPlan::execute (SA_ResolveCLThreatCmd *) +bool SA_WorkingPlan::execute (SA_ResolveCLThreatCmd * cmd) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP + + std::ostringstream debug_text; + + + + + + TaskInstID first_task_inst = cmd->first; + TaskInstID second_task_inst = cmd->second; + Condition condition = cmd->condition; + + // CausalLink cl; + // cl.cond = condition; + // cl.first = first_task_inst; + // cl.second = second_task_inst; + + // this->causal_links_.insert(std::pair<Condition, CausalLink>(condition, cl)); + this->ordering_links.insert(std::pair<TaskInstID, TaskInstID>(first_task_inst, second_task_inst)); + this->reverse_ordering_links.insert(std::pair<TaskInstID, TaskInstID>(second_task_inst, first_task_inst)); + +/* + + + std::map<TaskInstID, bool> un_visited_map; + std::map<TaskInstID, bool> visited_map; + std::stack<TaskInstID> s; + + for(InstToTaskMap::iterator it = this->task_insts_.begin(); it != this->task_insts_.end(); it++){ + un_visited_map.insert(std::pair<TaskInstID, bool>(it->first, true)); + } + + int total_num = un_visited_map.size(); + + while(!(s.size()== total_num)){ + + std::map<TaskInstID, bool>::iterator some_it = un_visited_map.begin(); + TaskInstID next = some_it->first; + dfs_aux(next, s, visited_map, un_visited_map); + + } + + visited_map.clear(); + + while(!s.empty()){ + TaskInstID next = s.top(); + s.pop(); + + if(!dfs_aux2(next, visited_map)) + return false; + } + + */ + + cmd->got_to_change_precedences = true; + if(is_cycle_in_ordering()){ + + debug_text << "SA_WorkingPlan::execute (SA_ResolveCLThreatCmd * cmd): Cannot schedule task inst"<<cmd->first<<" before task inst"<<cmd->second<<std::endl; + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + + cmd->got_to_change_precedences = false; + return false; + } + + // this->init_prec_insert(this->associate_cmd->task_inst_,this->associate_cmd); + + // this->after_orderings + + PrecedenceSet* befores = &this->precedence_graph_.find(BEFORE)->second; + PrecedenceSet* afters = &this->precedence_graph_.find(AFTER)->second; + PrecedenceSet* simuls = &this->precedence_graph_.find(SIMUL)->second; + PrecedenceSet* unrankeds = &this->precedence_graph_.find(UNRANKED)->second; + + TaskInstSet *before_A = &befores->find(cmd->first)->second; + TaskInstSet *after_A = &afters->find(cmd->first)->second; + TaskInstSet *simul_A = &simuls->find(cmd->first)->second; + TaskInstSet *unranked_A = &unrankeds->find(cmd->first)->second; + + TaskInstSet *before_B = &befores->find(cmd->second)->second; + TaskInstSet *after_B = &afters->find(cmd->second)->second; + TaskInstSet *simul_B = &simuls->find(cmd->second)->second; + TaskInstSet *unranked_B = &unrankeds->find(cmd->second)->second; + + /* + cmd->before_A_old = *before_A; + cmd->after_A_old = *after_A; + cmd->simul_A_old = *simul_A; + cmd->unsched_A_old = *unranked_A; + + cmd->before_B_old = *before_B; + cmd->after_B_old = *after_B; + cmd->simul_B_old = *simul_B; + cmd->unsched_B_old = *unranked_B; +*/ + cmd->befores = *befores; + cmd->afters = *afters; + cmd->simuls = *simuls; + cmd->unrankeds = *unrankeds; + + TaskInstSet tmp; + + /* + for(TaskInstSet::iterator it = unranked_A->begin(); it != unranked_A->end(); it++){ + if(unranked_B->find(*it)!=unranked_B->end()){ + tmp.insert(*it); + } + } + + + unranked_A->clear(); + unranked_B->clear(); + for(TaskInstSet::iterator it = tmp.begin(); it != tmp.end(); it++){ + unranked_A->insert(*it); + unranked_B->insert(*it); + } + */ + + before_B->insert(cmd->first); + unranked_B->erase(cmd->first); + + for(TaskInstSet::iterator it = before_A->begin(); it != before_A->end(); it++){ + before_B->insert(*it); + unranked_B->erase(*it); + + afters->find(*it)->second.insert(cmd->second); + unrankeds->find(*it)->second.erase(cmd->second); + } + + after_A->insert(cmd->second); + unranked_A->erase(cmd->second); + + for(TaskInstSet::iterator it = after_B->begin(); it != after_B->end(); it++){ + after_A->insert(*it); + unranked_A->erase(*it); + + befores->find(*it)->second.insert(cmd->first); + unrankeds->find(*it)->second.erase(cmd->first); + } + + debug_text << "SA_WorkingPlan::execute (SA_ResolveCLThreatCmd * cmd): Now scheduling task "<<cmd->first<<" before "<<cmd->second<<std::endl; + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + + + + + + + + + + PrecedenceSet* new_befores = &this->precedence_graph_.find(BEFORE)->second; + PrecedenceSet* new_afters = &this->precedence_graph_.find(AFTER)->second; + PrecedenceSet* new_simuls = &this->precedence_graph_.find(SIMUL)->second; + PrecedenceSet* new_unrankeds = &this->precedence_graph_.find(UNRANKED)->second; + + debug_text << "SA_WorkingPlan::execute (SA_ResolveCLThreatCmd * cmd): Precendence Sets: BEFORE: " << std::endl; + for(PrecedenceSet::iterator it = new_befores->begin(); it != new_befores->end(); it++){ + debug_text<<" TaskInst ID "<<it->first<<" : "; + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + debug_text<<" "<<*jt; + } + debug_text<<std::endl; + } + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + debug_text << "SA_WorkingPlan::execute (SA_ResolveCLThreatCmd * cmd): Precendence Sets: AFTER: " << std::endl; + for(PrecedenceSet::iterator it = new_afters->begin(); it != new_afters->end(); it++){ + debug_text<<" TaskInst ID "<<it->first<<" : "; + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + debug_text<<" "<<*jt; + } + debug_text<<std::endl; + } + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + + debug_text << "SA_WorkingPlan::execute (SA_ResolveCLThreatCmd * cmd): Precendence Sets: SIMUL: " << std::endl; + for(PrecedenceSet::iterator it = new_simuls->begin(); it != new_simuls->end(); it++){ + debug_text<<" TaskInst ID "<<it->first<<" : "; + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + debug_text<<" "<<*jt; + } + debug_text<<std::endl; + } + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + debug_text << "SA_WorkingPlan::execute (SA_ResolveCLThreatCmd * cmd): Precendence Sets: UNRANKED: " << std::endl; + for(PrecedenceSet::iterator it = new_unrankeds->begin(); it != new_unrankeds->end(); it++){ + debug_text<<" TaskInst ID "<<it->first<<" : "; + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + debug_text<<" "<<*jt; + } + debug_text<<std::endl; + } + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + return true; + +/* + + TimeWindow first_start,second_start; + TimeWindow first_end,second_end; + first_start = this->get_start_window(first_task_inst); + first_end = this->get_end_window(first_task_inst); + second_start = this->get_start_window(second_task_inst); + second_end = this->get_end_window(second_task_inst); + + if(second_start.second!= NULL_TIME && (first_end.second==NULL_TIME || second_start.second<first_end.second)) + { + fstream file_op("outfile.txt",ios::app); + file_op<<"place 1"<<std::endl; + file_op.close(); + + + // There is a need to adjust the max times + cmd->adj_max_times_cmd_ = static_cast<SA_AdjustMaxTimesCmd *> (this->get_AdjustMaxTimesCmd()); + cmd->adj_max_times_cmd_->set_times(first_task_inst,second_start.second-this->get_duration(first_task_inst),second_start.second); + if(!this->execute(cmd->adj_max_times_cmd_)) + return false; + } + else cmd->adj_max_times_cmd_=NULL; + if(first_end.first!= NULL_TIME && second_start.first<first_end.first) + { + fstream file_op("outfile.txt",ios::app); + file_op<<"place 2"<<std::endl; + file_op.close(); + + + // There is a need to adjust the min times + cmd->adj_min_times_cmd_ = static_cast<SA_AdjustMinTimesCmd *> (this->get_AdjustMinTimesCmd()); + cmd->adj_min_times_cmd_->set_times(second_task_inst,first_end.first,first_end.first+this->get_duration(second_task_inst)); + if(!this->execute(cmd->adj_min_times_cmd_)) + return false; + } + else cmd->adj_min_times_cmd_=NULL; + + PrecedenceSet *before = &this->precedence_graph_.find(BEFORE)->second; + PrecedenceSet *after = &this->precedence_graph_.find(AFTER)->second; + PrecedenceSet *simul = &this->precedence_graph_.find(SIMUL)->second; + PrecedenceSet *unranked = &this->precedence_graph_.find(UNRANKED)->second; + + before->find(second_task_inst)->second.insert(first_task_inst); + after->find(first_task_inst)->second.insert(second_task_inst); + unranked->find(first_task_inst)->second.erase(unranked->find(first_task_inst)->second.find(second_task_inst)); + unranked->find(second_task_inst)->second.erase(unranked->find(second_task_inst)->second.find(first_task_inst)); + + TaskInstSet after_second = this->after_orderings(second_task_inst); + for(TaskInstSet::iterator iter=simul->find(second_task_inst)->second.begin();iter!=simul->find(second_task_inst)->second.end();iter++) + after_second.insert(*iter); + TaskInstSet before_first = this->before_orderings(first_task_inst); + for(TaskInstSet::iterator iter=simul->find(first_task_inst)->second.begin();iter!=simul->find(first_task_inst)->second.end();iter++) + before_first.insert(*iter); + // All the task instances after and simultaneous to the second task instance should + // be after all the task instances before and simultaneous to the first task instance + for(TaskInstSet::iterator iter=after_second.begin();iter!=after_second.end();iter++) + { + if(before->find(*iter)->second.find(first_task_inst)==before->find(*iter)->second.end()) + { + SA_ResolveCLThreatCmd *temp = static_cast<SA_ResolveCLThreatCmd *> (this->get_ResolveCLThreatCmd()); + temp->set_task_insts(first_task_inst,*iter); + cmd->cmds_.push_back(temp); + if(!this->execute(temp)) return false; + } + } + // All the task instances before and simultaneous to the first task instance should + // be before all the task instances after and simultaneous to the second task instance + for(TaskInstSet::iterator iter=before_first.begin();iter!=before_first.end();iter++) + { + if(after->find(*iter)->second.find(second_task_inst)==after->find(*iter)->second.end()) + { + SA_ResolveCLThreatCmd *temp = static_cast<SA_ResolveCLThreatCmd *> (this->get_ResolveCLThreatCmd()); + temp->set_task_insts(*iter,second_task_inst); + cmd->cmds_.push_back(temp); + if(!this->execute(temp)) return false; + } + } + return true; + + */ }; +void SA_WorkingPlan::dfs_aux(TaskInstID current, std::stack<TaskInstID>& s, std::map<TaskInstID, bool>& visited, std::map<TaskInstID, bool>& unvisited){ + + + + unvisited.erase(current); + visited.insert(std::pair<TaskInstID, bool>(current, true)); + + std::pair< + SchedulingLinks::iterator, + SchedulingLinks::iterator + > ret = this->ordering_links.equal_range(current); + + for(SchedulingLinks::iterator it = ret.first; it != ret.second; it++){ + TaskInstID hah = it->second; + if(visited.find(hah)==visited.end()){ + dfs_aux(it->second, s, visited, unvisited); + } + } + + s.push(current); + +} + +bool SA_WorkingPlan::dfs_aux2(TaskInstID current, std::map<TaskInstID, bool>& visited){ + + visited.insert(std::pair<TaskInstID, bool>(current, true)); + + std::pair< + SchedulingLinks::iterator, + SchedulingLinks::iterator + > ret = this->reverse_ordering_links.equal_range(current); + + for(SchedulingLinks::iterator it = ret.first; it != ret.second; it++){ + TaskInstID hah = it->second; + if(visited.find(hah)==visited.end()){ + return false; + } + } + + return true; +} + // Undo a command to resolve a causal link threat in the // plan (with promotion or demotion). -void SA_WorkingPlan::undo (SA_ResolveCLThreatCmd *) +void SA_WorkingPlan::undo (SA_ResolveCLThreatCmd * cmd) { - //****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP****TEMP -}; + + std::ostringstream debug_text; + + debug_text << "SA_WorkingPlan::undo (SA_ResolveCLThreatCmd * cmd): Undoing scheduling task "<<cmd->first<<" before "<<cmd->second<<std::endl; + + + SA_POP_DEBUG_STR (SA_POP_DEBUG_NORMAL, debug_text.str ()); + debug_text.clear(); + + if(cmd->got_to_change_precedences){ + + + PrecedenceSet* new_befores = &this->precedence_graph_.find(BEFORE)->second; + PrecedenceSet* new_afters = &this->precedence_graph_.find(AFTER)->second; + PrecedenceSet* new_simuls = &this->precedence_graph_.find(SIMUL)->second; + PrecedenceSet* new_unrankeds = &this->precedence_graph_.find(UNRANKED)->second; + + for(PrecedenceSet::iterator it = cmd->befores.begin(); it != cmd->befores.end(); it++){ + new_befores->find(it->first)->second.clear(); + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + new_befores->find(it->first)->second.insert(*jt); + } + } + + for(PrecedenceSet::iterator it = cmd->afters.begin(); it != cmd->afters.end(); it++){ + new_afters->find(it->first)->second.clear(); + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + new_afters->find(it->first)->second.insert(*jt); + } + } + + for(PrecedenceSet::iterator it = cmd->simuls.begin(); it != cmd->simuls.end(); it++){ + new_simuls->find(it->first)->second.clear(); + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + new_simuls->find(it->first)->second.insert(*jt); + } + } + + for(PrecedenceSet::iterator it = cmd->unrankeds.begin(); it != cmd->unrankeds.end(); it++){ + new_unrankeds->find(it->first)->second.clear(); + for(TaskInstSet::iterator jt = it->second.begin(); jt != it->second.end(); jt++){ + new_unrankeds->find(it->first)->second.insert(*jt); + } + } + +/* + + TaskInstSet *before_A = &this->precedence_graph_.find(BEFORE)->second.find(cmd->first)->second; + TaskInstSet *after_A = &this->precedence_graph_.find(AFTER)->second.find(cmd->first)->second; + TaskInstSet *simul_A = &this->precedence_graph_.find(SIMUL)->second.find(cmd->first)->second; + TaskInstSet *unranked_A = &this->precedence_graph_.find(UNRANKED)->second.find(cmd->first)->second; + + TaskInstSet *before_B = &this->precedence_graph_.find(BEFORE)->second.find(cmd->second)->second; + TaskInstSet *after_B = &this->precedence_graph_.find(AFTER)->second.find(cmd->second)->second; + TaskInstSet *simul_B = &this->precedence_graph_.find(SIMUL)->second.find(cmd->second)->second; + TaskInstSet *unranked_B = &this->precedence_graph_.find(UNRANKED)->second.find(cmd->second)->second; + + before_A->clear(); + for(TaskInstSet::iterator it = cmd->before_A_old.begin(); it != cmd->before_A_old.end(); it++){ + before_A->insert(*it); + } + + after_A->clear(); + for(TaskInstSet::iterator it = cmd->after_A_old.begin(); it != cmd->after_A_old.end(); it++){ + after_A->insert(*it); + } + + simul_A->clear(); + for(TaskInstSet::iterator it = cmd->simul_A_old.begin(); it != cmd->simul_A_old.end(); it++){ + simul_A->insert(*it); + } + + unranked_A->clear(); + for(TaskInstSet::iterator it = cmd->unsched_A_old.begin(); it != cmd->unsched_A_old.end(); it++){ + unranked_A->insert(*it); + } + + before_B->clear(); + for(TaskInstSet::iterator it = cmd->before_B_old.begin(); it != cmd->before_B_old.end(); it++){ + before_B->insert(*it); + } + + after_B->clear(); + for(TaskInstSet::iterator it = cmd->after_B_old.begin(); it != cmd->after_B_old.end(); it++){ + after_B->insert(*it); + } + + simul_B->clear(); + for(TaskInstSet::iterator it = cmd->simul_B_old.begin(); it != cmd->simul_B_old.end(); it++){ + simul_B->insert(*it); + } + + unranked_B->clear(); + for(TaskInstSet::iterator it = cmd->unsched_B_old.begin(); it != cmd->unsched_B_old.end(); it++){ + unranked_B->insert(*it); + } + + */ + } + + //before_A = cmd->before_A_old; + // after_A = cmd->after_A_old + //simul_A = cmd->simul_A_old; + //unranked_A = cmd->unsched_A_old; + + //before_B = cmd->before_B_old; + // after_B = cmd->after_B_old; + //simul_B = cmd->simul_B_old; + // unranked_B = cmd->unsched_B_old; + + //return; + +// std::pair<CondToCLinksMap::iterator, CondToCLinksMap::iterator> firstmap +// = this->causal_links_.equal_range(cmd->condition); + +// CondToCLinksMap::iterator firstit; + +// for(firstit = firstmap.first; firstit != firstmap.second; firstit++){ + // if(firstit->second.first == cmd->first && firstit->second.second == cmd->second) + // break; + // } + +// this->causal_links_.erase(firstit); + + std::pair< + std::multimap<TaskInstID, TaskInstID>::iterator, + std::multimap<TaskInstID, TaskInstID>::iterator + > ret = this->ordering_links.equal_range(cmd->first); + + std::multimap<TaskInstID, TaskInstID>::iterator it; + + for(it = ret.first; it != ret.second; it++){ + if(it->second == cmd->second) + break; + // this->ordering_links.erase(it); + } + + this->ordering_links.erase(it); + + ret = this->reverse_ordering_links.equal_range(cmd->second); + + for(it = ret.first; it != ret.second; it++){ + if(it->second == cmd->first) + break; + } + + this->reverse_ordering_links.erase(it); + + + /* + + for(std::list<SA_ResolveCLThreatCmd*>::reverse_iterator iter=cmd->cmds_.rbegin();iter!=cmd->cmds_.rend();iter++) + this->undo(*iter); + + TaskInstID first_task_inst = cmd->first; + TaskInstID second_task_inst = cmd->second; + + PrecedenceSet *before = &this->precedence_graph_.find(BEFORE)->second; + PrecedenceSet *after = &this->precedence_graph_.find(AFTER)->second; + PrecedenceSet *unranked = &this->precedence_graph_.find(UNRANKED)->second; + + before->find(second_task_inst)->second.erase(before->find(second_task_inst)->second.find(first_task_inst)); + after->find(first_task_inst)->second.erase(after->find(first_task_inst)->second.find(second_task_inst)); + unranked->find(first_task_inst)->second.insert(second_task_inst); + unranked->find(second_task_inst)->second.insert(first_task_inst); + if(cmd->adj_max_times_cmd_ != NULL) { + this->undo(cmd->adj_max_times_cmd_); + } + if(cmd->adj_min_times_cmd_ != NULL)this->undo(cmd->adj_min_times_cmd_); + + + */ + }; // Execute a command to resolve a scheduling conflict (i.e. // non-causal-link ordering constraint with promotion or demotion) @@ -907,7 +1723,10 @@ bool SA_WorkingPlan::execute (SA_AdjustMinTimesCmd *cmd) cmd->min_adjust_cmds.push_back(temp); } } + if(sched.empty()) std::cout<<"sched is empty"<<std::endl; + + for(TaskInstSet::iterator iter=sched.begin();iter!=sched.end();iter++) { TimeWindow temp_start = this->get_start_window(*iter); @@ -915,10 +1734,13 @@ bool SA_WorkingPlan::execute (SA_AdjustMinTimesCmd *cmd) if(end_win->first>temp_start.first) { SA_AdjustMinTimesCmd* temp = static_cast<SA_AdjustMinTimesCmd *> (this->get_AdjustMinTimesCmd()); - TimeValue dur = this->durations.find(*iter)->second; - if(dur!=0) temp->set_times(*iter,end_win->first,end_win->first+this->durations.find(*iter)->second); - else temp->set_times(*iter,end_win->first,temp_end.first); + TimeValue dur = this->durations.find(*iter)->second; + if(dur!=0) + temp->set_times(*iter,end_win->first,end_win->first+this->durations.find(*iter)->second); + else + temp->set_times(*iter,end_win->first,temp_end.first); cmd->min_adjust_cmds.push_back(temp); + } } // Do the same change for the task instances simultaneous to this one @@ -934,8 +1756,10 @@ bool SA_WorkingPlan::execute (SA_AdjustMinTimesCmd *cmd) } } + for(SA_AdjustMinTimesCmd::MinTimesAdjustList::iterator iter=cmd->min_adjust_cmds.begin();iter!=cmd->min_adjust_cmds.end();iter++) { + if(!this->execute(*iter)) return false; } std::cout<<"the task inst is "<<cmd->task_inst_<<std::endl; @@ -1026,6 +1850,10 @@ bool SA_WorkingPlan::execute (SA_AdjustMaxTimesCmd *cmd) cmd->max_adjust_cmds.push_back(temp); } } + + if(sched.empty()) std::cout<<"msched is empty ("<<cmd->task_inst_<<")"<<std::endl; + else + std::cout<<"mnot sched is empty ("<<cmd->task_inst_<<")"<<std::endl; for(TaskInstSet::iterator iter=sched.begin();iter!=sched.end();iter++) { @@ -1165,6 +1993,7 @@ TimeValue SA_WorkingPlan::get_duration(TaskInstID task_inst) /// Adds the sched order to the sched_links_ map by putting the first task instance before the second void SA_WorkingPlan::add_sched_link(TaskInstID first_task_inst, TaskInstID second_task_inst) { + std::cout<<"Adding sched link insert"<<std::endl; this->sched_links_.insert(std::make_pair(first_task_inst,second_task_inst)); } /// Removes the sched order from the sched_links_ map diff --git a/SA_WorkingPlan.h b/SA_WorkingPlan.h index 468bc451cb7..0d522d66dd5 100644 --- a/SA_WorkingPlan.h +++ b/SA_WorkingPlan.h @@ -20,7 +20,8 @@ #include "WorkingPlan.h" #include "PlanCommands.h" #include "SA_PlanCommands.h" - +#include <stack> +#include <map> namespace SA_POP { @@ -262,7 +263,7 @@ namespace SA_POP { /** * @param cmd Command object. */ - virtual void execute (SA_ResolveCLThreatCmd *cmd); + virtual bool execute (SA_ResolveCLThreatCmd *cmd); /// Undo a command to resolve a causal link threat in the /// plan (with promotion or demotion). @@ -327,11 +328,17 @@ namespace SA_POP { */ virtual void reset_plan (); + + virtual void generate_all_threats(void); + protected: // ************************************************************************ // State information. // ************************************************************************ + //Ben's. Kill if it breaks this + CLThreatSet threat_set; + /// Flag for whether command prototypes have been set. bool has_cmds_; @@ -395,6 +402,18 @@ namespace SA_POP { // The set of reused task instances std::multiset<TaskInstID> reused_insts_; + + //I can't use sched_links_ because something craps itself when it gets a link + //and the tasks don't have windows. I hate it too. + SchedulingLinks ordering_links; + //useful for doing the loop detection algorithm + SchedulingLinks reverse_ordering_links; + // SA_AssocTaskImplCmd* associate_cmd; + + bool is_cycle_in_ordering(void); + void dfs_aux(TaskInstID current, std::stack<TaskInstID>& s, std::map<TaskInstID, bool>& visited, std::map<TaskInstID, bool>& unvisited); + bool dfs_aux2(TaskInstID current, std::map<TaskInstID, bool>& visited); + /// Insert initially task by task in the precedence graph /** * @param task_inst The task instance to insert into the precedence graph diff --git a/WorkingPlan.h b/WorkingPlan.h index 3866bffc185..8cad6129149 100644 --- a/WorkingPlan.h +++ b/WorkingPlan.h @@ -194,6 +194,8 @@ namespace SA_POP { */ virtual AdjustMaxTimesCmd *get_AdjustMaxTimesCmd (void) = 0; + virtual void generate_all_threats(void) = 0; + protected: /// Pointer to Planner object. SA_POP::Planner *planner_; |