-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAction.java
More file actions
167 lines (147 loc) · 4.88 KB
/
Copy pathAction.java
File metadata and controls
167 lines (147 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package actions;
import java.util.ArrayList;
import storygenerator.WorldProperty;
/**
*
* @author etienne
*/
public abstract class Action {
protected ArrayList<WorldProperty> aPreTrue; //These properties must be in the world's state in order to execute the action
protected ArrayList<WorldProperty> aPreFalse; //These properties must NOT be in the world state
protected ArrayList<WorldProperty> aPostTrue; //These are added to the world's state as a result of the action
protected ArrayList<WorldProperty> aPostFalse; //These are removed from the world's state as a result of the action
protected String aNarrativeForm; //To show the text of the action
/**
* Constructor. Initializes the condition lists.
*/
protected Action() {
aPreTrue = new ArrayList<WorldProperty>();
aPreFalse = new ArrayList<WorldProperty>();
aPostTrue = new ArrayList<WorldProperty>();
aPostFalse = new ArrayList<WorldProperty>();
}
/**
* Applies the postconditions of this Action to the world state, but only
* if the preconditions are verified.
* @param pWorldState
* @return
*/
public boolean execute(ArrayList<WorldProperty> pWorldState) {
if (!verifyPreconditions(pWorldState)) {
return false;
}
applyPostconditions(pWorldState);
return true;
}
/**
* Returns true if all of this Actions's true preconditions are found in
* the world's state, and all of this Action's false preconditions are NOT
* found in the world's state.
* @param pWorld
* @return
*/
public boolean verifyPreconditions(ArrayList<WorldProperty> pWorldState) {
//Check that all preconditions that must be true are found in the world state
for (WorldProperty tp : aPreTrue) {
if (!pWorldState.contains(tp)) {
return false;
}
}
//Check that all preconditions that must be false are found in the world state
for (WorldProperty fp : aPreFalse) {
if (pWorldState.contains(fp)) {
return false;
}
}
//If all true preconditions were found and all false preconditions were
//not found, then we can execute this action.
return true;
}
/**
* Adds the true postconditions to, and removes the false postconditions
* from the world state.
* @param pWorldState
*/
public void applyPostconditions(ArrayList<WorldProperty> pWorldState) {
for (WorldProperty tp : aPostTrue) {
pWorldState.add(tp);
}
for (WorldProperty fp : aPostFalse) {
pWorldState.remove(fp);
}
}
/**
* Returns true if all of the postconditions of the action are already
* present (or absent if it's false) from the world state.
* @param pWorldState
*/
public boolean uselessAction(ArrayList<WorldProperty> pWorldState) {
for (WorldProperty tp : aPostTrue) {
if (!pWorldState.contains(tp)) {
return false;
}
}
for (WorldProperty fp : aPostFalse) {
if (pWorldState.contains(fp)) {
return false;
}
}
//If all the true postconditions are already there, and all the
//false preconditions already absent, then the action is useless.
return true;
}
/**
* Determines whether the positive effects of this actions include
* property p.
* @param p
* @return
*/
public boolean postTrueContains(WorldProperty p) {
return aPostTrue.contains(p);
}
/**
* Determines whether the negative effects of this actions include
* property p.
* @param p
* @return
*/
public boolean postFalseContains(WorldProperty p) {
return aPostFalse.contains(p);
}
/*
* Getters
*/
public ArrayList<WorldProperty> getPreTrue() {
return aPreTrue;
}
public ArrayList<WorldProperty> getPreFalse() {
return aPreFalse;
}
public ArrayList<WorldProperty> getPostTrue() {
return aPostTrue;
}
public ArrayList<WorldProperty> getPostFalse() {
return aPostFalse;
}
@Override
public String toString() {
return aNarrativeForm;
}
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (o == this) return true;
if (!(o instanceof Action)) return false;
Action a = (Action) o;
if (a.aNarrativeForm.equals(this.aNarrativeForm)){
return true;
}
else return false;
}
@Override
public int hashCode() {
int hash = 3;
hash = 71 * hash + (this.aNarrativeForm != null ? this.aNarrativeForm.hashCode() : 0);
return hash;
}
}