forked from ggp-org/ggp-base
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added TromboterMachineState (extends MachineState) containing int nodeScore (initialized as -1) added TromboterProverStateMachine same behaviour as ProverStateMachine but will use TromboterMachineState instead
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/org/ggp/base/util/statemachine/TromboterMachineState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.ggp.base.util.statemachine; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.ggp.base.util.gdl.grammar.GdlSentence; | ||
|
||
public class TromboterMachineState extends MachineState { | ||
|
||
public int nodeScore; | ||
|
||
public TromboterMachineState() { | ||
this.contents = null; | ||
nodeScore = -1; // -1 == not explored yet | ||
} | ||
|
||
public TromboterMachineState(Set<GdlSentence> contents) | ||
{ | ||
this.contents = contents; | ||
nodeScore = -1; // -1 == not explored yet | ||
} | ||
|
||
} |
123 changes: 123 additions & 0 deletions
123
src/org/ggp/base/util/statemachine/implementation/prover/TromboterProverStateMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package org.ggp.base.util.statemachine.implementation.prover; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
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.logging.GamerLogger; | ||
import org.ggp.base.util.prover.Prover; | ||
import org.ggp.base.util.prover.aima.AimaProver; | ||
import org.ggp.base.util.statemachine.TromboterMachineState; | ||
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; | ||
import org.ggp.base.util.statemachine.implementation.prover.result.ProverResultParser; | ||
|
||
|
||
public class TromboterProverStateMachine extends StateMachine | ||
{ | ||
private TromboterMachineState initialState; | ||
private Prover prover; | ||
private List<Role> roles; | ||
|
||
/** | ||
* Initialize must be called before using the StateMachine | ||
*/ | ||
public ProverStateMachine() | ||
{ | ||
|
||
} | ||
|
||
@Override | ||
public void initialize(List<Gdl> description) | ||
{ | ||
prover = new AimaProver(description); | ||
roles = Role.computeRoles(description); | ||
initialState = computeInitialState(); | ||
} | ||
|
||
private TromboterMachineState computeInitialState() | ||
{ | ||
Set<GdlSentence> results = prover.askAll(ProverQueryBuilder.getInitQuery(), new HashSet<GdlSentence>()); | ||
return new ProverResultParser().toState(results); | ||
} | ||
|
||
@Override | ||
public int getGoal(TromboterMachineState state, Role role) throws GoalDefinitionException | ||
{ | ||
Set<GdlSentence> results = prover.askAll(ProverQueryBuilder.getGoalQuery(role), ProverQueryBuilder.getContext(state)); | ||
|
||
if (results.size() != 1) | ||
{ | ||
GamerLogger.logError("StateMachine", "Got goal results of size: " + results.size() + " when expecting size one."); | ||
throw new GoalDefinitionException(state, role); | ||
} | ||
|
||
try | ||
{ | ||
GdlRelation relation = (GdlRelation) results.iterator().next(); | ||
GdlConstant constant = (GdlConstant) relation.get(1); | ||
|
||
return Integer.parseInt(constant.toString()); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new GoalDefinitionException(state, role); | ||
} | ||
} | ||
|
||
@Override | ||
public TromboterMachineState getInitialState() | ||
{ | ||
return initialState; | ||
} | ||
|
||
@Override | ||
public List<Move> getLegalMoves(TromboterMachineState state, Role role) throws MoveDefinitionException | ||
{ | ||
Set<GdlSentence> results = prover.askAll(ProverQueryBuilder.getLegalQuery(role), ProverQueryBuilder.getContext(state)); | ||
|
||
if (results.size() == 0) | ||
{ | ||
throw new MoveDefinitionException(state, role); | ||
} | ||
|
||
return new ProverResultParser().toMoves(results); | ||
} | ||
|
||
@Override | ||
public TromboterMachineState getNextState(TromboterMachineState state, List<Move> moves) throws TransitionDefinitionException | ||
{ | ||
Set<GdlSentence> results = prover.askAll(ProverQueryBuilder.getNextQuery(), ProverQueryBuilder.getContext(state, getRoles(), moves)); | ||
|
||
for (GdlSentence sentence : results) | ||
{ | ||
if (!sentence.isGround()) | ||
{ | ||
throw new TransitionDefinitionException(state, moves); | ||
} | ||
} | ||
|
||
return new ProverResultParser().toState(results); | ||
} | ||
|
||
@Override | ||
public List<Role> getRoles() | ||
{ | ||
return roles; | ||
} | ||
|
||
@Override | ||
public boolean isTerminal(TromboterMachineState state) | ||
{ | ||
return prover.prove(ProverQueryBuilder.getTerminalQuery(), ProverQueryBuilder.getContext(state)); | ||
} | ||
} |