Skip to content

Instantly share code, notes, and snippets.

Created September 11, 2014 04:10
Show Gist options
  • Save anonymous/c12b146fc8a0aaef1a5d to your computer and use it in GitHub Desktop.
Save anonymous/c12b146fc8a0aaef1a5d 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: Action[S]*) extends ActionElement[S]
/**
* A Planner
*
* @param goalSet
* @tparam S
*/
case class Planner[S](goalSet: Goal[S]*) {
def satisfiableGoalSet(state : S): Set[Goal[S]] = {
Set[Goal[S]]()
}
def satisfyGoal(state : S, goal : Goal[S]): Option[S] = {
scala.util.Random.shuffle(goal.actionSet.toSet).foldLeft(None : Option[S])((next : Option[S], action : Action[S]) => {
next match {
case Some(_) => next
case None => satisfyAction(state, action)
}
})
}
def satisfyAction(state : S, action : Action[S]): Option[S] = {
action.actionElementSequence.foldLeft(Some(state) : Option[S])((state : Option[S], actionElement : ActionElement[S]) => {
(state, actionElement) match {
case (Some(previous), event : Event[S]) => satisfyEvent(previous, event)
case (Some(previous), goal : Goal[S]) => satisfyGoal(previous, goal)
case (None, actionElement) => None
}
})
}
def satisfyEvent(previous : S, event : Event[S]): Option[S] = {
event.transition(previous)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment