root/ggpa/Action.cpp

Revision 1, 5.7 kB (checked in by pantley2, 4 years ago)

GGPA code from the good old days of SIGART

Line 
1 /*
2  * Action.cpp
3  * Implementation of the Action class
4  */
5
6 #include <stdlib.h>
7 #include "Action.h"
8 #include <vector>
9 #include <iostream>
10 using namespace std;
11
12 Action::Action(vector<string> theValues, SentenceStructure* theStructure,
13                string theActor, const vector<string>& actors)
14     : Sentence(theValues, theStructure) {
15     if (!(theStructure->isAction())) {
16         cerr << "The structure does not correspond to an action." << endl;
17         exit(1);
18     }
19     actor = theActor;
20     possibleActors = actors;
21 }
22
23 Action::Action(const Action &action) : Sentence(action)
24 {
25     this->actor = action.actor;
26 }
27
28
29 /*
30  * getActor
31  * No parameters
32  * Return type: string
33  * Returns the name of the player that does this action
34  */
35 string Action::getActor() const {
36     return this->actor;
37 }
38
39 /*
40  * setActor
41  * Parameters: theActor - the name of the player that does this action
42  * Return type: none
43  * Sets the actor to be theActor
44  */
45 void Action::setActor(string theActor) {
46     this->actor = theActor;
47 }
48
49
50 /**
51  * getVariables
52  * No parameters.
53  * Return type: a map from variables to vectors of of their possible values
54  * Searches through the sentence and its dependencies, and returns all
55  * of the variables, along with their posible values
56  */
57 map<string, vector<string> > Action::getVariables() const {
58     return Sentence::getVariables();
59 }
60
61
62 vector<double> Action::toInputs() const {
63     // dummy implementation for compilation
64     vector<double> inputs(getStructure()->getNumParameters());
65     for (unsigned int parameter = 0;
66          parameter < getStructure()->getNumParameters(); parameter++) {
67         string value = (*this)[parameter];
68         int valueIndex = getStructure()->getValueIndex(parameter, value);
69
70         if (valueIndex == -1) {
71             cerr << "Invalid Action:  parameter " << value
72                  << " is either a variable, or not a valid value for parameter "
73                  << parameter << endl;
74             exit(1);
75         }
76
77         int numValues = getStructure()->getValues(parameter).size();
78         double ratio = valueIndex / (double) numValues;
79         inputs[parameter] = ratio;
80     }
81     return inputs;
82 }
83
84 /*
85  * inputSize
86  *
87  * Parameters: None
88  * Return value: int
89  * Returns the number of inputs required for a Action.  This is the same
90  * as the length of the array returned by the toInputs function.
91  */
92 int Action::inputSize() const {
93     // dummy implementation for compilation
94     return getStructure()->getNumParameters();
95 }
96
97 /*
98  * getVariables
99  * Parameters: variableMap: a map where we should add the new variables
100  * Return type: none
101  * Adds the variables and their set of possible values to the variableMap
102  */
103 void Action::getVariables(map<string, vector<string> >& variableMap) const {
104     Sentence::getVariables(variableMap);
105     if (isVariable(actor)) {
106         variableMap[actor] = possibleActors;
107     }
108 }
109
110 /**
111  * getAtomicSentence
112  * Parameters: variableMap - a mapping of the variables
113  * Return type: a new Sentence
114  * Creates a new sentence with any variables mapped by the parameter map
115  */
116 Sentence* Action::getAtomicSentence(map<string, string> variableMap) const {
117     return getAtomicAction(variableMap);
118 }
119
120 /**
121  * getAtomicAction
122  * Parameters: variableMap - a mapping of the variables
123  * Return type: a new Sentence
124  * Creates a new sentence with any variables mapped by the parameter map
125  */
126 Action* Action::getAtomicAction(map<string, string> variableMap) const {
127     Action* atomicSentence = new Action(*this);
128
129     if (isVariable(actor)) {
130         atomicSentence->actor = variableMap[actor];
131     }
132
133     for (unsigned int valueIndex = 0;
134          valueIndex < atomicSentence->values.size();
135          valueIndex++) {
136         string value = atomicSentence->values[valueIndex];
137         if (isVariable(value)) {
138             atomicSentence->values[valueIndex] = variableMap[value];
139         }
140     }
141
142     for (unsigned int dependencyIndex = 0;
143          dependencyIndex < dependencies.size();
144          dependencyIndex++) {
145         vector<Sentence*> dependency = dependencies[dependencyIndex];
146         for (unsigned int sentenceIndex = 0;
147              sentenceIndex < dependency.size();
148              sentenceIndex++) {
149             Sentence* sentence;
150             sentence = dependency[sentenceIndex]->getAtomicSentence(variableMap);
151             atomicSentence->dependencies[dependencyIndex][sentenceIndex] = sentence;
152             // NOTE:  Do we need to delete the old sentence?
153         }
154     }
155
156     return atomicSentence;
157 }
158
159 /*
160  * operator==
161  * Parameters: action - an action to compare to
162  * Return type: bool
163  * Returns true if the actions are identical, and false otherwise
164  */
165 bool Action::operator==(const Action &action) const {
166     if (!(((Sentence)(*this)) == ((Sentence)action))) {
167         return false;
168     }
169     if (this->actor != action.actor) {
170         return false;
171     }
172     return true;
173 }
174
175 /*
176  * print
177  * Parameters: output: a stream to output to.
178  *             indent: indent on each line
179  * No return type.
180  * Prints the sentence to the given stream
181  */
182 void Action::print(ostream& output, string indent) const {
183     output << indent << "Actor: " << actor << endl;
184     Sentence::print(output, indent);
185 }
186
187
188
189 /*
190  * mergeValues
191  * Parameters: variableMap:  a multimap from variables to their set of
192  *                           possible values
193  * Return type: none
194  * Merges the sets of values for the variables
195  */
196 void Action::mergeValues(multimap<string, PossibleValues*>& variableMap) {
197     // merge in the actor's values
198     mergeValue(actor, &possibleActors, variableMap);
199    
200     // do the rest of the merging
201     Sentence::mergeValues(variableMap);
202 }
203
204
205 /*
206  * mergeValues
207  * No parameters or return type.
208  * Merges possible values for variables in this sentence.
209  */
210 void Action::mergeValues() {
211     Sentence::mergeValues();
212 }
Note: See TracBrowser for help on using the browser.