-
Notifications
You must be signed in to change notification settings - Fork 153
/
SamplePropNetStateMachine.java
212 lines (189 loc) · 7.14 KB
/
SamplePropNetStateMachine.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package org.ggp.base.util.statemachine.implementation.propnet;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.ggp.base.util.gdl.grammar.Gdl;
import org.ggp.base.util.gdl.grammar.GdlConstant;
import org.ggp.base.util.gdl.grammar.GdlRelation;
import org.ggp.base.util.gdl.grammar.GdlSentence;
import org.ggp.base.util.propnet.architecture.Component;
import org.ggp.base.util.propnet.architecture.PropNet;
import org.ggp.base.util.propnet.architecture.components.Proposition;
import org.ggp.base.util.propnet.factory.OptimizingPropNetFactory;
import org.ggp.base.util.statemachine.MachineState;
import org.ggp.base.util.statemachine.Move;
import org.ggp.base.util.statemachine.Role;
import org.ggp.base.util.statemachine.StateMachine;
import org.ggp.base.util.statemachine.exceptions.GoalDefinitionException;
import org.ggp.base.util.statemachine.exceptions.MoveDefinitionException;
import org.ggp.base.util.statemachine.exceptions.TransitionDefinitionException;
import org.ggp.base.util.statemachine.implementation.prover.query.ProverQueryBuilder;
@SuppressWarnings("unused")
public class SamplePropNetStateMachine extends StateMachine {
/** The underlying proposition network */
private PropNet propNet;
/** The topological ordering of the propositions */
private List<Proposition> ordering;
/** The player roles */
private List<Role> roles;
/**
* Initializes the PropNetStateMachine. You should compute the topological
* ordering here. Additionally you may compute the initial state here, at
* your discretion.
*/
@Override
public void initialize(List<Gdl> description) {
try {
propNet = OptimizingPropNetFactory.create(description);
roles = propNet.getRoles();
ordering = getOrdering();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* Computes if the state is terminal. Should return the value
* of the terminal proposition for the state.
*/
@Override
public boolean isTerminal(MachineState state) {
// TODO: Compute whether the MachineState is terminal.
return false;
}
/**
* Computes the goal for a role in the current state.
* Should return the value of the goal proposition that
* is true for that role. If there is not exactly one goal
* proposition true for that role, then you should throw a
* GoalDefinitionException because the goal is ill-defined.
*/
@Override
public int getGoal(MachineState state, Role role)
throws GoalDefinitionException {
// TODO: Compute the goal for role in state.
return -1;
}
/**
* Returns the initial state. The initial state can be computed
* by only setting the truth value of the INIT proposition to true,
* and then computing the resulting state.
*/
@Override
public MachineState getInitialState() {
// TODO: Compute the initial state.
return null;
}
/**
* Computes the legal moves for role in state.
*/
@Override
public List<Move> getLegalMoves(MachineState state, Role role)
throws MoveDefinitionException {
// TODO: Compute legal moves.
return null;
}
/**
* Computes the next state given state and the list of moves.
*/
@Override
public MachineState getNextState(MachineState state, List<Move> moves)
throws TransitionDefinitionException {
// TODO: Compute the next state.
return null;
}
/**
* This should compute the topological ordering of propositions.
* Each component is either a proposition, logical gate, or transition.
* Logical gates and transitions only have propositions as inputs.
*
* The base propositions and input propositions should always be exempt
* from this ordering.
*
* The base propositions values are set from the MachineState that
* operations are performed on and the input propositions are set from
* the Moves that operations are performed on as well (if any).
*
* @return The order in which the truth values of propositions need to be set.
*/
public List<Proposition> getOrdering()
{
// List to contain the topological ordering.
List<Proposition> order = new LinkedList<Proposition>();
// All of the components in the PropNet
List<Component> components = new ArrayList<Component>(propNet.getComponents());
// All of the propositions in the PropNet.
List<Proposition> propositions = new ArrayList<Proposition>(propNet.getPropositions());
// TODO: Compute the topological ordering.
return order;
}
/* Already implemented for you */
@Override
public List<Role> getRoles() {
return roles;
}
/* Helper methods */
/**
* The Input propositions are indexed by (does ?player ?action).
*
* This translates a list of Moves (backed by a sentence that is simply ?action)
* into GdlSentences that can be used to get Propositions from inputPropositions.
* and accordingly set their values etc. This is a naive implementation when coupled with
* setting input values, feel free to change this for a more efficient implementation.
*
* @param moves
* @return
*/
private List<GdlSentence> toDoes(List<Move> moves)
{
List<GdlSentence> doeses = new ArrayList<GdlSentence>(moves.size());
Map<Role, Integer> roleIndices = getRoleIndices();
for (int i = 0; i < roles.size(); i++)
{
int index = roleIndices.get(roles.get(i));
doeses.add(ProverQueryBuilder.toDoes(roles.get(i), moves.get(index)));
}
return doeses;
}
/**
* Takes in a Legal Proposition and returns the appropriate corresponding Move
* @param p
* @return a PropNetMove
*/
public static Move getMoveFromProposition(Proposition p)
{
return new Move(p.getName().get(1));
}
/**
* Helper method for parsing the value of a goal proposition
* @param goalProposition
* @return the integer value of the goal proposition
*/
private int getGoalValue(Proposition goalProposition)
{
GdlRelation relation = (GdlRelation) goalProposition.getName();
GdlConstant constant = (GdlConstant) relation.get(1);
return Integer.parseInt(constant.toString());
}
/**
* A Naive implementation that computes a PropNetMachineState
* from the true BasePropositions. This is correct but slower than more advanced implementations
* You need not use this method!
* @return PropNetMachineState
*/
public MachineState getStateFromBase()
{
Set<GdlSentence> contents = new HashSet<GdlSentence>();
for (Proposition p : propNet.getBasePropositions().values())
{
p.setValue(p.getSingleInput().getValue());
if (p.getValue())
{
contents.add(p.getName());
}
}
return new MachineState(contents);
}
}