Fawkes API Fawkes Development Version
domain_action.cpp
1
2/***************************************************************************
3 * domain_action.cpp - stn-generator
4 *
5 * Created: Sat May 6 20:16:21 2017
6 * Copyright 2017 Matthias Loebach
7 ****************************************************************************/
8
9/* This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Library General Public License for more details.
18 *
19 * Read the full text in the LICENSE.GPL file in the doc directory.
20 */
21
22#include "domain_action.h"
23
24namespace fawkes {
25namespace stn {
26
27/** @class DomainAction "domain_action.h"
28 * A representation of an action used by the STN generator.
29 */
30
31/** Constructor.
32 * @param name The name of the action
33 * @param params The list of parameters of the action
34 * @param preconds A list of preconditions, each precondition is a Predicate
35 * @param effects A list of effects, each effect is a Predicate
36 * @param duration The duration of the action
37 * @param cond_breakups A list of conditional breakups
38 * @param temp_breakups A list of temporal breakups
39 */
40DomainAction::DomainAction(const std::string & name,
41 const std::vector<std::string> &params,
42 const std::vector<Predicate> & preconds,
43 const std::vector<Predicate> & effects,
44 int duration,
45 const std::vector<std::string> &cond_breakups,
46 const std::vector<std::string> &temp_breakups)
47: name_(name),
48 params_(params),
49 preconds_(preconds),
50 effects_(effects),
51 duration_(duration),
52 cond_breakups_(cond_breakups),
53 temp_breakups_(temp_breakups)
54{
55}
56
57/** Print a DomainAction.
58 * This prints all relevant facts about a DomainAction including its name,
59 * preconditions, effects.
60 * @param strm The std::ostream to pass the information to.
61 * @param a The DomainAction to print.
62 */
63std::ostream &
64operator<<(std::ostream &strm, const DomainAction &a)
65{
66 strm << "DomainAction: " << a.name_ << std::endl;
67 strm << "\tParams:";
68 for (auto &param : a.params_) {
69 strm << " " << param;
70 }
71 strm << std::endl << "\tPreconditions:" << std::endl;
72 for (auto &pred : a.preconds_) {
73 strm << "\t\t" << pred;
74 }
75 strm << std::endl << "\tEffects:" << std::endl;
76 for (auto &eff : a.effects_) {
77 strm << "\t\t" << eff;
78 }
79 strm << std::endl << "\tDuration: " << std::to_string(a.duration_) << std::endl;
80 strm << "\tConditional Breakups:";
81 for (auto &breakup : a.cond_breakups_) {
82 strm << " " << breakup;
83 }
84 strm << std::endl << "\tTemporal Breakups:";
85 for (auto &breakup : a.temp_breakups_) {
86 strm << " " << breakup;
87 }
88 return strm;
89}
90
91/** Get the name of the action.
92 * @return The name as string.
93 */
94const std::string
96{
97 return name_;
98}
99
100/** Get the list of parameters of the action.
101 * @return A vector of parameters as strings.
102 */
103const std::vector<std::string>
105{
106 return params_;
107}
108
109/** Generate an StnAction from the DomainAction.
110 * @param name The name of the resulting StnAction.
111 * @param params The parameters of the resulting StnAction.
112 * @return The generated StnAction.
113 */
115DomainAction::generateStnAction(const std::string &name, const std::string &params)
116{
117 std::cout << "Generating StnAction " << name << " with params " << params << std::endl;
118
119 std::vector<Predicate> preconds;
120 std::vector<Predicate> effects;
121
122 std::istringstream iss(params);
123 std::vector<std::string> params_vec{std::istream_iterator<std::string>{iss},
124 std::istream_iterator<std::string>{}};
125 std::map<std::string, std::string> params_map;
126 if (params_vec.size() != params_.size()) {
127 std::cout << "Param counts differ for DomainAction (" << std::to_string(params_.size()) << ") "
128 << name_ << " and StnAction (" << params << ") " << name << std::endl;
129 }
130 for (size_t i = 0; i < params_vec.size(); i++) {
131 // insert with additional question mark, since it is lost during parsing
132 params_map.insert(std::pair<std::string, std::string>("?" + params_.at(i), params_vec.at(i)));
133 std::cout << "Inserting "
134 << "?" + params_.at(i) << " with " << params_vec.at(i) << std::endl;
135 }
136 std::cout << "Applying precods" << std::endl;
137 for (Predicate pred : preconds_) {
138 std::vector<std::string> precond_attr;
139 for (std::string attr : pred.attrs()) {
140 std::string opt;
141 // check if attribute is a constant or variable (latter starting with ?)
142 if (attr[0] == '?') {
143 if (params_map.find(attr) == params_map.end()) {
144 std::cout << "err, could not find attribute for precondition: " << attr << std::endl;
145 }
146 opt = params_map.find(attr)->second;
147 } else {
148 opt = attr;
149 }
150 precond_attr.push_back(opt);
151 }
152 preconds.push_back(Predicate(pred.name(), pred.condition(), precond_attr));
153 }
154 std::cout << "Applying effects" << std::endl;
155 for (Predicate pred : effects_) {
156 std::vector<std::string> effect_attr;
157 for (auto attr : pred.attrs()) {
158 std::string opt;
159 // check if attribute is a constant or variable (latter starting with ?)
160 if (attr.find("?") == 0) {
161 if (params_map.find(attr) == params_map.end()) {
162 std::cout << "err, could not find attribute for effect: " << attr << std::endl;
163 }
164 opt = params_map.find(attr)->second;
165 } else {
166 opt = attr;
167 }
168 effect_attr.push_back(opt);
169 }
170 effects.push_back(Predicate(pred.name(), pred.condition(), effect_attr));
171 }
172 std::cout << "StnAction built" << std::endl;
173
174 return StnAction(name_, preconds, effects, params, duration_, cond_breakups_, temp_breakups_);
175}
176
177} // namespace stn
178} // namespace fawkes
A representation of an action used by the STN generator.
Definition: domain_action.h:40
const std::vector< std::string > params()
Get the list of parameters of the action.
StnAction generateStnAction(const std::string &name, const std::string &params)
Generate an StnAction from the DomainAction.
DomainAction(const std::string &name, const std::vector< std::string > &params, const std::vector< Predicate > &preconds, const std::vector< Predicate > &effects, int duration=0, const std::vector< std::string > &cond_breakups={}, const std::vector< std::string > &temp_breakups={})
Constructor.
const std::string getName()
Get the name of the action.
A representation of a Predicate in the STN.
Definition: predicate.h:33
An action representation within an STN.
Definition: stn_action.h:41
Fawkes library namespace.