root/ggpa/Sentence.h
| Revision 1, 8.3 kB (checked in by pantley2, 4 years ago) |
|---|
| Line | |
|---|---|
| 1 | /* |
| 2 | * Sentence.h |
| 3 | * Header file for Sentence class |
| 4 | */ |
| 5 | |
| 6 | #ifndef SENTENCE_H |
| 7 | #define SENTENCE_H |
| 8 | |
| 9 | // defined in C++ libraries |
| 10 | //#define BIG_ENDIAN 0 |
| 11 | //#define LITTLE_ENDIAN 1 |
| 12 | |
| 13 | #define ENDIAN LITTLE_ENDIAN |
| 14 | #define DISTINCT_NAME "distinct" |
| 15 | |
| 16 | |
| 17 | #include "SentenceStructure.h" |
| 18 | #include "Utils.h" |
| 19 | #include <vector> |
| 20 | #include <string> |
| 21 | #include <map> |
| 22 | #include <ostream> |
| 23 | #include <iostream> |
| 24 | using namespace std; |
| 25 | |
| 26 | /* |
| 27 | * Class to represent sentences in the rules, like (cell %x 2 x). These |
| 28 | * sentences can include literals and variables, and Sentences will also |
| 29 | * be used in some cases to hold the dependencies for a sentence |
| 30 | */ |
| 31 | class Sentence { |
| 32 | |
| 33 | protected: |
| 34 | // the values for this sentence. Variables will start with % |
| 35 | vector<string> values; |
| 36 | |
| 37 | // the subvectors must all be true (AND) to imply this sentence. |
| 38 | // The subvectors represent OR relations - if there is only one, |
| 39 | // then it is just a single literal |
| 40 | vector<vector<Sentence*> > dependencies; |
| 41 | |
| 42 | // A dependency flag is true if the dependency is negative - meaning |
| 43 | // that in the dependency declaration, the dependency was surrounded |
| 44 | // by a NOT |
| 45 | vector<bool> dependencyFlags; |
| 46 | |
| 47 | // A reference to the underlying SentenceStructure |
| 48 | SentenceStructure* structure; |
| 49 | |
| 50 | class PossibleValues { |
| 51 | protected: |
| 52 | vector<string>* values; |
| 53 | |
| 54 | public: |
| 55 | PossibleValues() : values(NULL) {} |
| 56 | |
| 57 | PossibleValues(vector<string>* possibleValues); |
| 58 | |
| 59 | virtual ~PossibleValues() {} |
| 60 | |
| 61 | /* |
| 62 | * merge |
| 63 | * Parameters: otherValues: the values to add to the current |
| 64 | * No return type |
| 65 | * Adds the otherValues to these values, avoiding duplication. |
| 66 | */ |
| 67 | void merge(PossibleValues* otherValues); |
| 68 | |
| 69 | /* |
| 70 | * getValues |
| 71 | * no parameters |
| 72 | * return type: vector<string>* |
| 73 | * gets the values for this object. |
| 74 | */ |
| 75 | virtual vector<string>* getValues(); |
| 76 | }; |
| 77 | |
| 78 | class DistinctPossibleValues : public PossibleValues { |
| 79 | public: |
| 80 | |
| 81 | DistinctPossibleValues(vector<string>* possibleValues); |
| 82 | |
| 83 | virtual ~DistinctPossibleValues () {} |
| 84 | |
| 85 | /* |
| 86 | * getValues |
| 87 | * no parameters |
| 88 | * return type: vector<string>* |
| 89 | * gets the values for this object. |
| 90 | */ |
| 91 | vector<string>* getValues(); |
| 92 | }; |
| 93 | |
| 94 | public: |
| 95 | |
| 96 | /* |
| 97 | * Constructor for Sentence |
| 98 | * Parameters: theValues - a vector of values for the Sentence |
| 99 | * theStructure - a pointer to a SentenceStructure |
| 100 | * Return type: none |
| 101 | * Sets the values of the sentence equal to a copy of theValues, |
| 102 | * checking that the values are all in the SentenceStructure. |
| 103 | */ |
| 104 | Sentence(vector<string> theValues, SentenceStructure* theStructure); |
| 105 | |
| 106 | /* |
| 107 | * Destructor for Sentence class |
| 108 | */ |
| 109 | virtual ~Sentence(); |
| 110 | |
| 111 | /* |
| 112 | * operator[] |
| 113 | * Parameters: index - an int representing the value that we wish to |
| 114 | * retrieve |
| 115 | * Return type: A const reference to a string |
| 116 | * Returns the value at the index. This should be used to access the |
| 117 | * values in a Sentence. |
| 118 | */ |
| 119 | const string& operator[](int index) const; |
| 120 | |
| 121 | /* |
| 122 | * getNumValues |
| 123 | * No parameters |
| 124 | * Return type: int |
| 125 | * Returns the number of values for this sentence. |
| 126 | */ |
| 127 | unsigned int getNumValues() const; |
| 128 | |
| 129 | /* |
| 130 | * getName |
| 131 | * No parameters |
| 132 | * Return type: string |
| 133 | * Returns the name of this sentence (from its SentenceStructure) |
| 134 | */ |
| 135 | string getName() const; |
| 136 | |
| 137 | /* |
| 138 | * addAndDependency |
| 139 | * Parameters: dependency: a Sentence |
| 140 | * negated: a bool, true if the dependency is negative |
| 141 | * Return value: none |
| 142 | * Adds the dependency to the dependency list |
| 143 | */ |
| 144 | void addAndDependency(Sentence* dependency, bool negated); |
| 145 | |
| 146 | /* |
| 147 | * addOrDependency |
| 148 | * Parameters: theSentences: a vector of Sentences to be ORed |
| 149 | * negated: a bool, true if the dependencyis negative |
| 150 | * Adds the OR dependency to the dependency list |
| 151 | */ |
| 152 | void addOrDependency(const vector<Sentence*>& dependency, bool negated); |
| 153 | |
| 154 | /* |
| 155 | * getDependencies |
| 156 | * No parameters |
| 157 | * Return type: a const reference to a vector of vectors of sentences |
| 158 | * Returns the dependency list for this sentence. |
| 159 | */ |
| 160 | const vector<vector<Sentence*> >& getDependencies() const; |
| 161 | |
| 162 | /* |
| 163 | * isDependencyNegated |
| 164 | * Parameters: index - the index of the dependency in the dependency list |
| 165 | * Return type: bool |
| 166 | * Returns true if the dependency is negated, and false otherwise |
| 167 | */ |
| 168 | bool isDependencyNegated(int index) const; |
| 169 | |
| 170 | /* |
| 171 | * getIndex |
| 172 | * No parameters |
| 173 | * Return type: int |
| 174 | * Returns the index of this Sentence in the State object. If this sentence |
| 175 | * has variables, then the function will return -1. In this case, you |
| 176 | * should call getAtomicSentences and then do getIndex on each of those. |
| 177 | */ |
| 178 | int getIndex() const; |
| 179 | |
| 180 | /* |
| 181 | * getAtomicSentences |
| 182 | * Parameters: none |
| 183 | * Return type: a vector of sentences |
| 184 | * Returns all possible atomic sentences that can be derived from this |
| 185 | * Sentence. These sentences will be returned in increasing order of |
| 186 | * index within State. |
| 187 | */ |
| 188 | vector<Sentence*> getAtomicSentences() const; |
| 189 | |
| 190 | /** |
| 191 | * enumerateWithBinding |
| 192 | * Parameters: none |
| 193 | * Return type: a vector of sentence* |
| 194 | * This works similarly to getAtomicSentences, except that it also does |
| 195 | * binding within the dependencies. |
| 196 | */ |
| 197 | vector<Sentence*> enumerateWithBinding() const; |
| 198 | |
| 199 | /** |
| 200 | * getVariables |
| 201 | * No parameters. |
| 202 | * Return type: a map from variables to vectors of of their possible values |
| 203 | * Searches through the sentence and its dependencies, and returns all |
| 204 | * of the variables, along with their posible values |
| 205 | */ |
| 206 | virtual map<string, vector<string> > getVariables() const; |
| 207 | |
| 208 | /** |
| 209 | * getAtomicSentence |
| 210 | * Parameters: variableMap - a mapping of the variables |
| 211 | * Return type: a new Sentence |
| 212 | * Creates a new sentence with any variables mapped by the parameter map |
| 213 | */ |
| 214 | virtual Sentence* getAtomicSentence(map<string, string> variableMap) const; |
| 215 | |
| 216 | /* |
| 217 | * getStructure |
| 218 | * Parameters: none |
| 219 | * Return type: a pointer to a SentenceStructure |
| 220 | * Returns the SentenceStructure for this Sentence |
| 221 | */ |
| 222 | SentenceStructure* getStructure() const; |
| 223 | |
| 224 | /* |
| 225 | * operator== |
| 226 | * Parameters: s2 - another Sentence for comparison |
| 227 | * Return type: bool |
| 228 | * Returns true if the two sentences are equal, and false otherwise |
| 229 | */ |
| 230 | bool operator==(const Sentence& s2) const; |
| 231 | |
| 232 | /* |
| 233 | * clearDependencies |
| 234 | * No parameters or return type. |
| 235 | * Removes all of the dependencies |
| 236 | */ |
| 237 | void clearDependencies(bool recursiveDelete); |
| 238 | |
| 239 | /* |
| 240 | |
| 241 | * Parameters: output: a stream to output to. |
| 242 | * indent: indent on each line |
| 243 | * No return type. |
| 244 | * Prints the sentence to the given stream |
| 245 | */ |
| 246 | virtual void print(ostream& output = cout, string indent = "") const; |
| 247 | |
| 248 | /* |
| 249 | * mergeValues |
| 250 | * No parameters or return type. |
| 251 | * Merges possible values for variables in this sentence. |
| 252 | */ |
| 253 | virtual void mergeValues(); |
| 254 | |
| 255 | private: |
| 256 | |
| 257 | /* |
| 258 | * enumerateWithBinding |
| 259 | * Parameters: valueMap - a map of variables to values |
| 260 | * Return type: vector of sentence pointers |
| 261 | * Returns the set of sentences that can be generated from this sentence. |
| 262 | * The supplied variable values will be used, but if no values exist, |
| 263 | * we will branch on this value. |
| 264 | */ |
| 265 | vector<Sentence*> enumerateWithBinding(map<string,string>& valueMap) const; |
| 266 | |
| 267 | protected: |
| 268 | |
| 269 | /* |
| 270 | * getVariables |
| 271 | * Parameters: variableMap: a map where we should add the new variables |
| 272 | * Return type: none |
| 273 | * Adds the variables and their set of possible values to the variableMap |
| 274 | */ |
| 275 | virtual void getVariables(map<string, vector<string> >& variableMap) const; |
| 276 | |
| 277 | /* |
| 278 | * mergeValues |
| 279 | * Parameters: variableMap: a multimap from variables to their set of |
| 280 | * possible values |
| 281 | * Return type: none |
| 282 | * Merges the sets of values for the variables |
| 283 | */ |
| 284 | virtual void mergeValues(multimap<string, PossibleValues*>& variableMap); |
| 285 | |
| 286 | /* |
| 287 | * mergeValue |
| 288 | * Parameters: value - the value to merge in |
| 289 | * valueSet - the set of values for that variable |
| 290 | * variableMap - the mapping from variable name to value set |
| 291 | * Return type: none |
| 292 | * Merges the value with the valueMap. |
| 293 | */ |
| 294 | void mergeValue(string value, vector<string>* valueSet, |
| 295 | multimap<string, PossibleValues*>& variableMap); |
| 296 | |
| 297 | }; |
| 298 | |
| 299 | #endif |
| 300 |
Note: See TracBrowser for help on using the browser.
