-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YangDR <[email protected]>
- Loading branch information
Showing
6 changed files
with
75 additions
and
30 deletions.
There are no files selected for viewing
6 changes: 0 additions & 6 deletions
6
src/main/kotlin/org/mechdancer/dataflow/external/stateMachine/MachineState.kt
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/org/mechdancer/dataflow/external/stateMachine/core/MachineSnapshot.kt
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,6 @@ | ||
package org.mechdancer.dataflow.external.stateMachine.core | ||
|
||
data class MachineSnapshot<T>( | ||
val current: StateMember<T>, | ||
val value: T | ||
) |
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
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
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/org/mechdancer/dataflow/external/stateMachine/machines/LinearStateMachine.kt
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,47 @@ | ||
package org.mechdancer.dataflow.external.stateMachine.machines | ||
|
||
import org.mechdancer.dataflow.core.link | ||
import org.mechdancer.dataflow.core.post | ||
import org.mechdancer.dataflow.external.stateMachine.core.StateMachine | ||
import org.mechdancer.dataflow.external.stateMachine.core.StateMember | ||
|
||
class StateList<T> { | ||
private val list = mutableListOf<Pair<Int, (T) -> T>>() | ||
|
||
fun once(action: (T) -> T) { | ||
list.add(1 to action) | ||
} | ||
|
||
fun loop(times: Int, action: (T) -> T) { | ||
assert(times > 0) | ||
list.add(times to action) | ||
} | ||
|
||
fun forever(action: (T) -> T) { | ||
list.add(Int.MAX_VALUE to action) | ||
} | ||
|
||
private infix fun Pair<Int, (T) -> T>.buildIn( | ||
machine: StateMachine<Pair<Int, T>> | ||
) = StateMember(machine, first > 1) { | ||
it.first + 1 to second(it.second) | ||
} | ||
|
||
fun build(): (T) -> Unit { | ||
val machine = StateMachine<Pair<Int, T>>("linearStateMachine") | ||
val stateList = mutableListOf(list[0] buildIn machine) | ||
var stepCount = list[0].first | ||
for (i in 1..list.lastIndex) { | ||
stateList.add(list[i] buildIn machine) | ||
val x = stepCount | ||
link(stateList[i - 1], stateList[i]) { it.first >= x } | ||
stepCount = (stepCount + list[i].first).takeIf { it > 0 } ?: Int.MAX_VALUE | ||
} | ||
return { | ||
stateList.first() post (0 to it) | ||
} | ||
} | ||
} | ||
|
||
fun <T> linearStateMachine(block: StateList<T>.() -> Unit) = | ||
StateList<T>().apply(block).build() |
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 |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import org.mechdancer.dataflow.core.link | ||
import org.mechdancer.dataflow.core.post | ||
import org.mechdancer.dataflow.external.stateMachine.State | ||
import org.mechdancer.dataflow.external.stateMachine.StateMachine | ||
import org.mechdancer.dataflow.external.stateMachine.machines.linearStateMachine | ||
|
||
fun main(args: Array<String>) { | ||
val machine = StateMachine<Int>("for") | ||
val add = State(machine) { x -> x + 1 } | ||
val print = State(machine) { x -> x.also { println(it) } } | ||
link(add, print) { it < 1000 } | ||
link(add, machine.ending) { it >= 1000 } | ||
link(print, add) | ||
print post 0 | ||
while (!machine.running); | ||
val startWith = linearStateMachine<Int> { | ||
once { println(it); it + 1 } | ||
loop(3) { println(it); it * 2 } | ||
once { println(it); it } | ||
forever { it } | ||
} | ||
|
||
startWith(3) | ||
while (true); | ||
} |