Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
更新 kotlin 版本
Browse files Browse the repository at this point in the history
  • Loading branch information
berberman committed Nov 26, 2018
1 parent 6d56d04 commit 08c821b
Show file tree
Hide file tree
Showing 23 changed files with 327 additions and 446 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,5 @@ fabric.properties
# Editor-based Rest Client
.idea/httpRequests

.idea

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

81 changes: 0 additions & 81 deletions .idea/misc.xml

This file was deleted.

28 changes: 0 additions & 28 deletions .idea/modules/statemachine.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.+'
ext.kotlin_version = '1.3.10'

repositories {
mavenCentral()
Expand All @@ -13,7 +13,7 @@ buildscript {
}

group 'org.mechdancer'
version '0.1.2-SNAPSHOT'
version '0.1.3'

apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.dokka'
Expand All @@ -40,12 +40,12 @@ publish {
userOrg = 'mechdancer'
groupId = 'org.mechdancer'
artifactId = 'statemachine'
publishVersion = '0.1.2'
publishVersion = version.toString()
desc = 'statemachine kotlin framework'
website = 'https://github.com/MechDancer/statemachine'
}

dokka{
dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions src/main/kotlin/org/mechdancer/statemachine/Shortcut.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ typealias Event = () -> Boolean

/** 阻塞并执行状态机,直到结束 */
fun IInvokable<*>.run() {
while (!isCompleted) this()
while (!isCompleted) this()
}

/** 构造基础版本状态机 */
fun <T : IState> stateMachine(block: StandardMachine<T>.() -> Unit) =
StandardMachine<T>().apply(block)
StandardMachine<T>().apply(block)
20 changes: 10 additions & 10 deletions src/main/kotlin/org/mechdancer/statemachine/builder/Delay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import java.util.concurrent.TimeUnit.MILLISECONDS

/** 延时缓存 */
data class DelayBuilderDsl(
var time: Long = 0,
var unit: TimeUnit = MILLISECONDS
var time: Long = 0,
var unit: TimeUnit = MILLISECONDS
) {
val nano get() = unit.toNanos(time)
val nano get() = unit.toNanos(time)
}

/** dsl 构造延时状态 */
fun delay(block: DelayBuilderDsl.() -> Unit) =
state {
var start = 0L
loop = true
before = { start = System.nanoTime(); ACCEPT }
after = DelayBuilderDsl().apply(block).nano
.let { { (System.nanoTime() - start) > it } }
}
state {
var start = 0L
loop = true
before = { start = System.nanoTime(); ACCEPT }
after = DelayBuilderDsl().apply(block).nano
.let { { (System.nanoTime() - start) > it } }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,86 +8,86 @@ import java.util.concurrent.atomic.AtomicInteger

/** 线性状态机缓存 */
class LinearStateMachineBuilderDsl {
private val machine = StandardMachine<LinearState>()
private var last: LinearState? = null
private fun LinearState.add() = also {
machine.startFrom(it)
last?.let { last -> machine register (last to it) }
last = it
}
private val machine = StandardMachine<LinearState>()
private var last: LinearState? = null
private fun LinearState.add() = also {
machine.startFrom(it)
last?.let { last -> machine register (last to it) }
last = it
}

/**
* 构造状态,循环 time 次
* 第一个构造的状态视作初始状态
*/
fun call(times: Int, block: LinearStateBuilderDsl.() -> Unit) =
LinearStateBuilderDsl()
.apply(block)
.let {
object : LinearState(times) {
override val loop = true
/**
* 构造状态,循环 time 次
* 第一个构造的状态视作初始状态
*/
fun call(times: Int, block: LinearStateBuilderDsl.() -> Unit) =
LinearStateBuilderDsl()
.apply(block)
.let {
object : LinearState(times) {
override val loop = true

override fun before(): Boolean {
ttl.set(times)
return ACCEPT
}
override fun before(): Boolean {
ttl.set(times)
return ACCEPT
}

override fun doing() {
ttl.decrementAndGet()
it.todo()
}
override fun doing() {
ttl.decrementAndGet()
it.todo()
}

override fun after() =
ttl.get().let { rest -> rest <= 0 || it.until(times - rest) }
}
}.add()
override fun after() =
ttl.get().let { rest -> rest <= 0 || it.until(times - rest) }
}
}.add()

/** 构造状态,只循环 1 次 */
fun once(block: LinearStateBuilderDsl.() -> Unit) =
call(1, block)
/** 构造状态,只循环 1 次 */
fun once(block: LinearStateBuilderDsl.() -> Unit) =
call(1, block)

/** 构造状态,无限循环 */
fun forever(block: LinearStateBuilderDsl.() -> Unit) =
call(Int.MAX_VALUE, block)
/** 构造状态,无限循环 */
fun forever(block: LinearStateBuilderDsl.() -> Unit) =
call(Int.MAX_VALUE, block)

/** 延时 */
fun delay(block: DelayBuilderDsl.() -> Unit) =
object : LinearState(Int.MAX_VALUE) {
val limit = DelayBuilderDsl().apply(block).nano
var start = 0L
override val loop = true
override fun doing() = Unit
override fun before() = run { start = System.nanoTime(); ACCEPT }
override fun after() = run { (System.nanoTime() - start) > limit }
}.add()
/** 延时 */
fun delay(block: DelayBuilderDsl.() -> Unit) =
object : LinearState(Int.MAX_VALUE) {
val limit = DelayBuilderDsl().apply(block).nano
var start = 0L
override val loop = true
override fun doing() = Unit
override fun before() = run { start = System.nanoTime(); ACCEPT }
override fun after() = run { (System.nanoTime() - start) > limit }
}.add()

/** 获取时添加末状态以便退出 */
fun build(): IExternalAutoTransferable<LinearState> {
object : LinearState(0) {
override val loop = false
override fun before() = true
override fun doing() = Unit
override fun after() = true
}.add()
return machine
}
/** 获取时添加末状态以便退出 */
fun build(): IExternalAutoTransferable<LinearState> {
object : LinearState(0) {
override val loop = false
override fun before() = true
override fun doing() = Unit
override fun after() = true
}.add()
return machine
}

/** 基于次数执行的线性状态 */
abstract class LinearState(val times: Int) : IState {
/** 剩余次数 */
var ttl = AtomicInteger(times)
}
/** 基于次数执行的线性状态 */
abstract class LinearState(val times: Int) : IState {
/** 剩余次数 */
var ttl = AtomicInteger(times)
}

/** 用于线性状态机的状态配置 */
data class LinearStateBuilderDsl(
/** 要执行的操作 */
var todo: () -> Unit = {},
/** 用于线性状态机的状态配置 */
data class LinearStateBuilderDsl(
/** 要执行的操作 */
var todo: () -> Unit = {},

/** 跳出条件 := 执行次数 -> 能否跳出 */
var until: (Int) -> Boolean = { true }
)
/** 跳出条件 := 执行次数 -> 能否跳出 */
var until: (Int) -> Boolean = { true }
)
}

/** 构造线性状态机 */
fun linearStateMachine(block: LinearStateMachineBuilderDsl.() -> Unit) =
LinearStateMachineBuilderDsl().apply(block).build()
LinearStateMachineBuilderDsl().apply(block).build()
Loading

0 comments on commit 08c821b

Please sign in to comment.