org.ggp.base.util.ruleengine.RuleEngine Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of alloy-ggp-base Show documentation
Show all versions of alloy-ggp-base Show documentation
A modified version of the GGP-Base library for Alloy.
The newest version!
package org.ggp.base.util.ruleengine;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.ggp.base.util.GoalTuplePool;
import org.ggp.base.util.GoalTuplePool.GoalTuplePoolNode;
import org.ggp.base.util.ImmutableIntArray;
import org.ggp.base.util.gdl.grammar.GdlSentence;
import org.ggp.base.util.gdl.grammar.GdlTerm;
import org.ggp.base.util.statemachine.Role;
import com.google.common.collect.Lists;
//Intended as a replacement for StateMachine.
public interface RuleEngine> {
State getInitialState();
int getNumRoles();
List getRoles();
boolean isTerminal(State state);
int getGoal(State state, int roleIndex) throws GameDescriptionException;
default ImmutableIntArray getGoals(State state) throws GameDescriptionException {
GoalTuplePoolNode curNode = GoalTuplePool.getInitialNode();
for (int r = 0; r < getNumRoles(); r++) {
curNode = curNode.get(getGoal(state, r));
}
return curNode.getArray();
}
List getLegalMoves(State state, int roleIndex) throws GameDescriptionException;
State getNextState(State state, List jointMoves) throws GameDescriptionException;
Translator getTranslator();
@SuppressWarnings("unchecked") //relies on assumptions about Translators
default State toNativeState(RuleEngineState, ?> otherState) {
if (otherState.getTranslator() == getTranslator()) {
return (State) otherState;
}
Set gdlState = otherState.toGdlState();
return getTranslator().getNativeState(gdlState);
}
//TODO: Make this private somehow
static final Random RANDOM = new Random();
default List getRandomJointMove(State state) throws GameDescriptionException {
int numRoles = getNumRoles();
List jointMove = Lists.newArrayListWithCapacity(numRoles);
for (int r = 0; r < numRoles; r++) {
List legalMoves = getLegalMoves(state, r);
if (legalMoves.size() == 1) {
jointMove.add(legalMoves.get(0));
} else {
int chosenIndex = RANDOM.nextInt(legalMoves.size());
jointMove.add(legalMoves.get(chosenIndex));
}
}
return jointMove;
}
default List> getLegalMovesByRole(State state) throws GameDescriptionException {
List> legalMovesByRole = Lists.newArrayListWithCapacity(getNumRoles());
for (int r = 0; r < getNumRoles(); r++) {
legalMovesByRole.add(getLegalMoves(state, r));
}
return legalMovesByRole;
}
default List> getGdlLegalMovesByRole(State state) throws GameDescriptionException {
List> legalMovesByRole = Lists.newArrayListWithCapacity(getNumRoles());
for (int r = 0; r < getNumRoles(); r++) {
legalMovesByRole.add(getTranslator().getGdlMoves(getLegalMoves(state, r)));
}
return legalMovesByRole;
}
default ImmutableIntArray doRandomPlayout(State state) throws GameDescriptionException {
while (!isTerminal(state)) {
state = getRandomNextState(state);
}
return getGoals(state);
}
default State getRandomNextState(State state) throws GameDescriptionException {
List jointMove = getRandomJointMove(state);
return getNextState(state, jointMove);
}
/**
* Marks a state as being a "base" state that will often be used as a source of
* playouts or future states. This is useful for e.g. a new root node of a game
* tree.
*/
default void markAsBaseState(State state) {
// Do nothing by default
}
// default DepthChargeBatchResult performDepthChargeBatch(State state)
// throws GameDescriptionException {
// ImmutableIntArray result = doRandomPlayout(state);
// return new FlatDepthChargeResult(result, 1);
// }
}