root/ggpa/SentenceStructure.cpp
| Revision 1, 7.5 kB (checked in by pantley2, 4 years ago) |
|---|
| Line | |
|---|---|
| 1 | /* |
| 2 | * SentenceStructure.cpp |
| 3 | * Implementation of SentenceStructure class |
| 4 | */ |
| 5 | |
| 6 | #include "SentenceStructure.h" |
| 7 | |
| 8 | /* |
| 9 | * Two argument constructor for SentenceStructure |
| 10 | * Parameters: theName - a string containing the name of the new |
| 11 | * SentenceStructure |
| 12 | * numParameters - int containing the number of parameters |
| 13 | * for this SentenceStructure |
| 14 | * isActionValue - a boolean representing whether this is an |
| 15 | * action |
| 16 | * Sets name to be the parameter name, and creates a new possibleValues |
| 17 | * vector of size specified by numParameters |
| 18 | */ |
| 19 | SentenceStructure::SentenceStructure(string theName, int numParameters, |
| 20 | bool isActionValue) { |
| 21 | this->name = theName; |
| 22 | possibleValues.resize(numParameters); |
| 23 | //possibleValues = new vector<vector<string>* >(numParameters); |
| 24 | for (int i = 0; i < numParameters; i++) { |
| 25 | possibleValues[i] = new vector<string>(); |
| 26 | } |
| 27 | |
| 28 | // set the references vector to be NULL |
| 29 | //references.resize(numParameters, NULL); |
| 30 | |
| 31 | this->action = isActionValue; |
| 32 | offset = -1; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * getName |
| 37 | * Parameters: None |
| 38 | * Return type: string |
| 39 | * Returns the name of this SentenceStructure |
| 40 | */ |
| 41 | string SentenceStructure::getName() const { |
| 42 | return name; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * getNumParameters |
| 47 | * No parameters |
| 48 | * Return type: int |
| 49 | * Returns the number of parameters for this SentenceStructure |
| 50 | */ |
| 51 | unsigned int SentenceStructure::getNumParameters() const {// When different SentenceStructures have parameters that must have the |
| 52 | // same values, this is recorded here. |
| 53 | vector<vector<SentenceStructure*> > counterparts; |
| 54 | return possibleValues.size(); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * getValues |
| 59 | * Parameters: num - an int representing the index of the variable we |
| 60 | * are interested in |
| 61 | * Return type: a vector of strings representing the set of possible values |
| 62 | * Gets the set of possible values for the variable at index num. |
| 63 | */ |
| 64 | vector<string>& SentenceStructure::getValues(int num) const { |
| 65 | return *(possibleValues[num]); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * getValuesPointer |
| 70 | * Parameters: num - an int representing the index of the variable we |
| 71 | * are interested in |
| 72 | * Return type: a vector of strings representing the set of possible values |
| 73 | * Gets the set of possible values for the variable at index num. |
| 74 | */ |
| 75 | vector<string>* SentenceStructure::getValuesPointer(int num) const { |
| 76 | return possibleValues[num]; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * getValueIndex |
| 81 | * Parameters: num - an int representing the index of the variable we |
| 82 | * are interested in |
| 83 | * value - a string representing the value we are searching |
| 84 | * for |
| 85 | * Return type: int |
| 86 | * Returns the index of the value in the value set of the numth parameter, |
| 87 | * or -1 if it is not present. |
| 88 | */ |
| 89 | int SentenceStructure::getValueIndex(int num, string value) const { |
| 90 | vector<string>::iterator findItr; |
| 91 | int i = 0; |
| 92 | for (findItr = possibleValues[num]->begin(); |
| 93 | findItr != possibleValues[num]->end(); findItr++, i++) { |
| 94 | if (*findItr == value) { |
| 95 | return i; |
| 96 | } |
| 97 | } |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * addValue |
| 103 | * Parameters: num - an int representing the index of the variable we |
| 104 | * are interested in |
| 105 | * value - a string containing the new value to add for |
| 106 | * that variable |
| 107 | * No return type |
| 108 | * Adds value to the set of possible values for the variable at index num |
| 109 | */ |
| 110 | void SentenceStructure::addValue(int num, string value) { |
| 111 | vector<string>::iterator searchItr = possibleValues[num]->begin(); |
| 112 | for (; searchItr != possibleValues[num]->end(); searchItr++) { |
| 113 | if (*searchItr == value) { |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | possibleValues[num]->push_back(value); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * mergeValues |
| 124 | * Parameters: counterpart - another structure with which to merge the |
| 125 | * values for a certain parameter |
| 126 | * thisIndex - the index of the parameter in this |
| 127 | * thatIndex - the index of the parameter in counterpart |
| 128 | * Return type: none |
| 129 | * This function should be used when there is a shared reference between |
| 130 | * two sentences of different SentenceStructures. These two parameters |
| 131 | * must thus have the same values, so we want to make it so that they will |
| 132 | * have the same value set. |
| 133 | */ |
| 134 | /* |
| 135 | void mergeValues(SentenceStructure* counterpart, int thisIndex, |
| 136 | int thatIndex) { |
| 137 | vector<string>* thisVector = this->possibleValues[thisIndex]; |
| 138 | vector<string>* thatVector = counterpart->possibleValues[thatIndex]; |
| 139 | |
| 140 | // if they are already merged, return |
| 141 | if (thisVector == thatVector) { |
| 142 | return |
| 143 | } |
| 144 | |
| 145 | // if thisVector is a reference |
| 146 | if (this->references[thisIndex] != NULL) { |
| 147 | |
| 148 | // merge the two vectors into thisVector |
| 149 | |
| 150 | // check if thatVector is a reference |
| 151 | if (counterpart->references[thatIndex] != NULL) { |
| 152 | // propogate the change |
| 153 | |
| 154 | } |
| 155 | |
| 156 | } else { |
| 157 | |
| 158 | // merge the two vectors into thatVector |
| 159 | |
| 160 | |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | return; |
| 166 | } |
| 167 | */ |
| 168 | |
| 169 | |
| 170 | /* |
| 171 | * getOffset |
| 172 | * No parameters |
| 173 | * Return type: int |
| 174 | * Returns the offset for this sentence type for State |
| 175 | */ |
| 176 | int SentenceStructure::getOffset() const { |
| 177 | return offset; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * setOffset |
| 182 | * Parameters: theOffset - int containing the new offset for this sentence |
| 183 | * type in the state |
| 184 | * No return value |
| 185 | * Sets the offset to the parameter value |
| 186 | */ |
| 187 | void SentenceStructure::setOffset(int theOffset) { |
| 188 | this->offset = theOffset; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * isAction |
| 194 | * No parameters |
| 195 | * Return type: bool |
| 196 | * Returns true if this sentence is an Action, and false otherwise |
| 197 | */ |
| 198 | bool SentenceStructure::isAction() const { |
| 199 | return action; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * operator == |
| 204 | * Parameters: s2 - another SentenceStructure for comparison |
| 205 | * Return type: bool |
| 206 | * Returns true if the two SentenceStructures are equal, and false |
| 207 | * otherwise. |
| 208 | */ |
| 209 | bool SentenceStructure::operator==(const SentenceStructure& s2) const { |
| 210 | if (this->name != s2.name) { |
| 211 | return false; |
| 212 | } |
| 213 | if (this->offset != s2.offset) { |
| 214 | return false; |
| 215 | } |
| 216 | if (this->action != s2.action) { |
| 217 | return false; |
| 218 | } |
| 219 | if (this->possibleValues.size() != s2.possibleValues.size()){ |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | for (unsigned int i = 0; i < this->possibleValues.size(); i++) { |
| 224 | if (this->possibleValues[i] != s2.possibleValues[i]) { |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return true; |
| 230 | |
| 231 | |
| 232 | |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * numPossibleSentences |
| 237 | * No parameters |
| 238 | * Returns the number of sentences that could be built on this sentence |
| 239 | * structure |
| 240 | */ |
| 241 | int SentenceStructure::numPossibleSentences() { |
| 242 | int total = 1; |
| 243 | for (unsigned int value = 0; value < possibleValues.size(); value++) { |
| 244 | total *= possibleValues[value]->size(); |
| 245 | } |
| 246 | return total; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | |
| 251 | * Parameters: output: a stream to output to. |
| 252 | * indent: indent on each line |
| 253 | * No return type. |
| 254 | * Prints the sentence to the given stream |
| 255 | */ |
| 256 | void SentenceStructure::print(ostream& output, string indent) const { |
| 257 | output << indent << "(" << name; |
| 258 | indent = indent + "\t"; |
| 259 | for (unsigned int parameter = 0; parameter < possibleValues.size(); |
| 260 | parameter++) { |
| 261 | output << endl; |
| 262 | const vector<string> values = *(possibleValues[parameter]); |
| 263 | output << indent << "("; |
| 264 | for (unsigned int index = 0; index < values.size(); index++) { |
| 265 | output << values[index]; |
| 266 | if (index != values.size() - 1) { |
| 267 | output << ", "; |
| 268 | } |
| 269 | } |
| 270 | output << ")"; |
| 271 | } |
| 272 | output << ")" << endl; |
| 273 | } |
Note: See TracBrowser for help on using the browser.
