Skip to content

Commit

Permalink
Started making changes to subsystem model
Browse files Browse the repository at this point in the history
  • Loading branch information
camearle20 committed May 13, 2018
1 parent cdc5940 commit 428d364
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 104 deletions.
37 changes: 5 additions & 32 deletions SnakeSkin-Core/src/main/kotlin/org/snakeskin/dsl/Subsystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,10 @@ import org.snakeskin.subsystem.Subsystem
* @version 7/4/17
*/

fun Subsystem.machine(machine: String) = getStateMachine(machine)
fun Subsystem.machine() = getDefaultStateMachine()

fun buildSubsystem(name: String = "UNNAMED", setup: SubsystemBuilder.() -> Unit): Subsystem {
val builder = SubsystemBuilder(name)
fun Subsystem.stateMachine(setup: StateMachineBuilder.() -> Unit): StateMachine {
val builder = StateMachineBuilder()
builder.setup()
return builder.build()
}

class SubsystemBuilder(private val name: String): Builder<Subsystem> {
private val builder = Subsystem(name)

fun setup(action: () -> Unit) {
builder.addSetupTask(action)
}

fun on(event: String, action: Parameters.() -> Unit) {
builder.addEventHandler(event, action)
}

fun stateMachine(machine: String, setup: StateMachineBuilder.() -> Unit): StateMachine {
val sBuilder = StateMachineBuilder()
sBuilder.setup()
val instance = sBuilder.build()
builder.addStateMachine(machine, instance)
return instance
}

fun test(name: String, body: () -> Boolean) {
builder.addTest(name, body)
}

override fun build() = builder
val machine = builder.build()
addStateMachine(machine)
return machine
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,16 @@ import org.snakeskin.logic.Parameters
* @version 7/4/17
*/
object EventRouter {
private val priorityHandlers = hashMapOf<String, ArrayList<(Parameters) -> Unit>>()
private val handlers = hashMapOf<String, ArrayList<(Parameters) -> Unit>>()
private val executor = ExecutorFactory.getExecutor("Event Router")

fun registerHandler(event: String, handler: (Parameters) -> Unit) {
handlers.putIfAbsent(event, arrayListOf())
handlers[event]!!.add(handler)
}
internal fun registerPriority(event: String, handler: (Parameters) -> Unit) {
priorityHandlers.putIfAbsent(event, arrayListOf())
priorityHandlers[event]!!.add(handler)
}


@Synchronized @JvmStatic @JvmOverloads fun fireEvent(event: String, parameters: MutableParameters = MutableParameters()) {
priorityHandlers[event]?.forEach {
it(parameters.toParameters())
}
handlers[event]?.forEach {
executor.submit { it(parameters.toParameters()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ object Subsystems: Registry<Subsystem>() {

@JvmStatic fun testAll() {
registry.forEach {
it.runTests()
//Disabled for now
//TODO
//it.runTests()
}
}
}
77 changes: 19 additions & 58 deletions SnakeSkin-Core/src/main/kotlin/org/snakeskin/subsystem/Subsystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.snakeskin.exception.ItemNotFoundException
import org.snakeskin.factory.ExecutorFactory
import org.snakeskin.state.StateMachine
import java.lang.reflect.InvocationTargetException
import java.util.concurrent.TimeUnit

/*
* SnakeSkin - Created on 7/4/17
Expand All @@ -22,75 +23,35 @@ import java.lang.reflect.InvocationTargetException
* @version 7/4/17
*/

open class Subsystem(val name: String = "UNNAMED") {
open class Subsystem(val name: String, private val loopRate: Long = 20L) {
//Executor, for running subsystem actions
private val executor = ExecutorFactory.getExecutor("Subsystem")

//<editor-fold desc="State Handler">
private val stateMachines = hashMapOf<String, StateMachine>() //The states that a subsystem can be in
private val stateMachines = arrayListOf<StateMachine>()
fun addStateMachine(machine: StateMachine = StateMachine()) = stateMachines.add(machine)

fun addStateMachine(machine: String, instance: StateMachine = StateMachine()) = stateMachines.put(machine, instance)
/**
* Setup tasks for this subsystem
*/
open fun setup() {}

fun getStateMachine(machine: String): StateMachine {
if (stateMachines.contains(machine)) {
return stateMachines[machine]!!
} else {
throw ItemNotFoundException("Could not find state machine $machine")
}
}

fun getDefaultStateMachine(): StateMachine {
val firstKey = stateMachines.keys.firstOrNull()
if (firstKey != null) {
return stateMachines[firstKey]!!
} else {
throw ItemNotFoundException("Subsystem doesn't have any state machines")
}
}


//</editor-fold>

//<editor-fold desc="Setup Tasks">
private val setupTasks = arrayListOf<() -> Unit>()
fun addSetupTask(task: () -> Unit) = setupTasks.add(task)
//</editor-fold>

//<editor-fold desk="Tests">
private val tests = hashMapOf<String, () -> Boolean>()
fun addTest(name: String, test: () -> Boolean) = tests.put(name, test)
fun runTests() {
tests.forEach {
name, test ->
println("TEST $name\tSTARTING")
val result = test()
if (result)
println("TEST $name\tPASSED")
else
println("TEST $name\tFAILED")
}
}
//</editor-fold>
/**
* Looping actions for this subsystem
*/
open fun action() {}

internal fun init() {
setupTasks.forEach {
try {
it()
} catch (e: Exception) {
throw InitException("Error occurred while running setup tasks for subsystem '$name'", e)
}
try {
setup()
} catch (e: Exception) {
throw InitException("Error occured while running setup tasks for subsystem '$name", e)
}
stateMachines.forEach {
_, machine ->
EventRouter.registerPriority(Events.DISABLED) {
machine ->
EventRouter.registerHandler(Events.DISABLED) {
machine.setState(States.DISABLED)
}
}
}

fun addEventHandler(event: String, action: (Parameters) -> Unit) = EventRouter.registerPriority(event) {
executor.submit {
action(it)
}
executor.scheduleAtFixedRate(this::action, 0L, loopRate, TimeUnit.MILLISECONDS)
}
}
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ subprojects {
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
}

task wrapper(type: Wrapper) {
gradleVersion = '4.7'
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Sat Dec 09 22:50:28 EST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
6 changes: 3 additions & 3 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
warn () {
echo "$*"
}

die ( ) {
die () {
echo
echo "$*"
echo
Expand Down Expand Up @@ -155,7 +155,7 @@ if $cygwin ; then
fi

# Escape application args
save ( ) {
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
Expand Down

0 comments on commit 428d364

Please sign in to comment.