Skip to content

Instantly share code, notes, and snippets.

@aryairani
Forked from anonymous/gist:c12b146fc8a0aaef1a5d
Last active August 29, 2015 14:06
Show Gist options
  • Save aryairani/6a795c62f4862b06324d to your computer and use it in GitHub Desktop.
Save aryairani/6a795c62f4862b06324d to your computer and use it in GitHub Desktop.
package edu.gatech.dt87.scalaverse
package object planner {
/**
* An ActionElement is either an Event or a Goal.
*
* @tparam S a state type
*/
sealed trait ActionElement[S]
/**
* An Action is a sequence of ActionElements.
*
* @param actionElementSequence a sequence of ActionElements.
* @tparam S a state type
*/
case class Action[S](actionElementSequence: ActionElement[S]*)
/**
* An Event is a transition from a state to either Some state or no state.
*
* @param transition a transition
* @tparam S a state type
*/
case class Event[S](transition: S => Option[S]) extends ActionElement[S]
/**
* A Goal is a set of Actions, any one of which may satisfy the Goal.
*
* @param actionSet a set of Actions, any one of which may satisfy the Goal.
* @tparam S a state.
*/
case class Goal[S](actionSet: Set[Action[S]]) extends ActionElement[S] // store it in the format you want to retrieve (set)
object Goal {
def apply[S](actions: Action[S]*) = new Goal[S](actions.toSet) // optional helper syntax for writing
}
/**
* A Planner
*
* @param goalSet
* @tparam S
*/
case class Planner[S](goalSet: Goal[S]*) {
def satisfiableGoalSet(state : S): Set[Goal[S]] = {
Set.empty
}
/** All actionElements must succeed, in order. */
def satisfyAction(state: S, action: Action[S]): Option[S] = {
action.actionElementSequence.foldLeft[Option[S]](Some(state)) {
case (Some(previous), actionElement) => satisfyActionElement(previous, actionElement)
case _ => None
}
}
/** Try to satisfy one ActionElement */
def satisfyActionElement(state: S, a: ActionElement[S]): Option[S] =
a match {
/* Events are satisfied (or not) by one-shot transition function */
case Event(transition) =>
transition(state)
/* The first action to succeed satisfies this Goal */
case Goal(actionSet) =>
scala.util.Random.shuffle(actionSet).foldLeft[Option[S]](None) {
(previous, action) => previous orElse satisfyAction(state, action)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment