root/ggpa/State.cpp

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

GGPA code from the good old days of SIGART

Line 
1 /*
2  * State.cpp
3  * Implementation of State class
4  */
5
6 #include <stdlib.h>
7 #include <iostream>
8 #include "State.h"
9 #include "Sentence.h"
10 #include "SentenceStructure.h"
11
12 State::State()
13 {
14     values.clear();
15 }
16
17 State::State(int size)
18 {
19     values.clear();
20     values.resize(size, ZERO_VALUE);
21 }
22
23 State::State(const State &state)
24 {
25     values = state.values;
26 }
27
28 int State::operator=(const State &state)
29 {
30     values = state.values;
31     return 1;
32 }
33
34 bool State::operator==(const State &state) const
35 {
36     return (values == state.values);
37 }
38
39 bool State::operator<(const State& state) const {
40     return (values < state.values);
41 }
42
43 /******************************************************************
44  * add and remove functions
45  */
46
47 /*
48  * addSentence
49  * Parameters: sentence - the sentence to add
50  * No return type
51  * Adds the parameter sentence to the State.  This means that the sentence
52  * is true in the current state of the game.  If the sentence is an action
53  * it can not be added to a State, and thus this function will terminate
54  * with an error message
55  */
56 void State::addSentence(const Sentence* sentence) {
57     if (sentence->getStructure()->isAction()) {
58         cerr << "An action may not be an attribute in a state" << endl;
59         exit(1);
60     }
61     int index = sentence->getIndex();
62     values[index] = ONE_VALUE;
63 }
64
65 const vector<double>& State::toInputs() const {
66     return values;
67 }
68
69
70 /*
71  * containsSentence
72  * Parameters: sentence - a const Sentence* to look for in the state
73  * Return type: bool
74  * Returns true if the Sentence is set in this state, and false otherwise
75  */
76 bool State::containsSentence(const Sentence* sentence) const {
77     int index = sentence->getIndex();
78     //cout << index << ": " << values[index] << endl;
79     return (values[index] == ONE_VALUE);
80 }
81
82 /*
83  * getSize
84  * No parameters
85  * returns the number of values in the state
86  */
87 unsigned int State::getSize() const {
88     return values.size();
89 }
90
91
92 /*
93  * Prints the values for this state.
94  *
95  */
96 void State::print() const {
97     for (unsigned int i = 0; i < values.size(); i++) {
98         cout << values[i];
99     }
100     cout << endl;
101 }
Note: See TracBrowser for help on using the browser.