From 199e80bb9e951a5df235e4c457f4b5800f1a04d2 Mon Sep 17 00:00:00 2001 From: ManApart Date: Thu, 18 Jan 2024 08:03:14 -0500 Subject: [PATCH] idea priority --- .../kotlin/core/ai/composableExp/AIPackage.kt | 15 ++++++++------- .../composableExp/AIPackageTemplateBuilder.kt | 18 ++++++++++++------ .../core/ai/composableExp/IdeaBuilder.kt | 9 ++++----- .../kotlin/core/ai/composableExp/TestSheet.kt | 3 ++- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/commonMain/kotlin/core/ai/composableExp/AIPackage.kt b/src/commonMain/kotlin/core/ai/composableExp/AIPackage.kt index d5ba7565..33934a56 100644 --- a/src/commonMain/kotlin/core/ai/composableExp/AIPackage.kt +++ b/src/commonMain/kotlin/core/ai/composableExp/AIPackage.kt @@ -3,22 +3,23 @@ package core.ai.composableExp import core.events.Event import core.thing.Thing -//How does priority work? Is it just based on order definition? Then what about sub modules -//If everything is order based, then all top level ideas happen before any children, and children are priority per order -//Could also provide priority override map of name to priority (string,Int) -//give a base priority, and then packages can override that priority, with the top package overriding the override of a child package //When evaluating which idea to go with, map all ideas by priority, and grab the first idea that matches at a given priority list (or random at that level?) -//TODO - make name unique by prefixing it with parant ai package? How do we prevent name collisions? -data class Idea(val name: String, val criteria: suspend (Thing) -> Boolean, val action: suspend (Thing) -> List) +data class Idea(val name: String, val priority: Int, val criteria: suspend (Thing) -> Boolean, val action: suspend (Thing) -> List) //TODO - we can validate string references before flattening -data class AIPackageTemplate(val name: String, val subPackages: List, val ideas: List) { +//Validate +// - package name unique +// - idea name unique +// - string references exist +data class AIPackageTemplate(val name: String, val subPackages: List, val priorityOverride: Map, val ideas: List) { fun flatten(reference: Map, flattenedReference: MutableMap): AIPackage { return flattenedReference[name] ?: let { val subIdeas = subPackages.mapNotNull { reference[it] }.flatMap { subPackage -> flattenedReference[subPackage.name]?.ideas ?: subPackage.flatten(reference, flattenedReference).also { flattenedReference[subPackage.name] = it }.ideas + }.map { idea -> + priorityOverride[it.name]?.let { idea.copy(priority = it) } ?: idea } AIPackage(name, ideas + subIdeas) } diff --git a/src/commonMain/kotlin/core/ai/composableExp/AIPackageTemplateBuilder.kt b/src/commonMain/kotlin/core/ai/composableExp/AIPackageTemplateBuilder.kt index d215ca94..39c2eab2 100644 --- a/src/commonMain/kotlin/core/ai/composableExp/AIPackageTemplateBuilder.kt +++ b/src/commonMain/kotlin/core/ai/composableExp/AIPackageTemplateBuilder.kt @@ -1,6 +1,5 @@ package core.ai.composableExp -import core.events.Event import core.thing.Thing //Build all the templates, but don't flatten them @@ -12,18 +11,25 @@ import core.thing.Thing class AIPackageTemplateBuilder(val name: String) { private val templates = mutableListOf() - private val ideas = mutableListOf() + private val ideas = mutableListOf() private val criteriaChildren = mutableMapOf Boolean, IdeasBuilder>() - + private val priorityOverride = mutableMapOf() fun build(): AIPackageTemplate { - return AIPackageTemplate(name, templates, ideas) + return AIPackageTemplate(name, templates, priorityOverride, ideas.map { it.build(name) }) } fun template(vararg names: String) = this.templates.addAll(names) - fun idea(name: String, initializer: IdeaBuilder.() -> Unit = {}) { - ideas.add(IdeaBuilder(name).apply(initializer).build()) + /** + Override the priority of an idea inherited from a template. + */ + fun priority(ideaName: String, newPriority: Int){ + + } + + fun idea(name: String, priority: Int = 20, initializer: IdeaBuilder.() -> Unit = {}) { + ideas.add(IdeaBuilder(name, priority).apply(initializer)) } fun criteria(criteria: suspend (Thing) -> Boolean, initializer: IdeasBuilder.() -> Unit){ diff --git a/src/commonMain/kotlin/core/ai/composableExp/IdeaBuilder.kt b/src/commonMain/kotlin/core/ai/composableExp/IdeaBuilder.kt index 0aea46c8..441fb31a 100644 --- a/src/commonMain/kotlin/core/ai/composableExp/IdeaBuilder.kt +++ b/src/commonMain/kotlin/core/ai/composableExp/IdeaBuilder.kt @@ -4,12 +4,11 @@ import core.events.Event import core.thing.Thing import kotlinx.coroutines.NonCancellable.children -class IdeaBuilder(val name: String) { +class IdeaBuilder(val name: String, val priority: Int) { internal var criteria: suspend (Thing) -> Boolean = { true } private var action: suspend (Thing) -> List = { listOf() } - - fun build(packageName: String) = Idea("$packageName-$name", criteria, action) + fun build(packageName: String) = Idea("$packageName-$name", priority, criteria, action) fun criteria(criteria: suspend (Thing) -> Boolean) { this.criteria = criteria @@ -41,8 +40,8 @@ class IdeasBuilder { children.add(item) } - fun idea(name: String, initializer: IdeaBuilder.() -> Unit) { - children.add(IdeaBuilder(name).apply(initializer)) + fun idea(name: String, priority: Int = 20, initializer: IdeaBuilder.() -> Unit) { + children.add(IdeaBuilder(name, priority).apply(initializer)) } fun criteria(criteria: suspend (Thing) -> Boolean, initializer: IdeasBuilder.() -> Unit) { diff --git a/src/commonMain/kotlin/core/ai/composableExp/TestSheet.kt b/src/commonMain/kotlin/core/ai/composableExp/TestSheet.kt index 8d101959..0834c1f5 100644 --- a/src/commonMain/kotlin/core/ai/composableExp/TestSheet.kt +++ b/src/commonMain/kotlin/core/ai/composableExp/TestSheet.kt @@ -8,7 +8,7 @@ fun testAiPackages() { aiPackage("animal") { criteria({ !it.isSafe() }) { criteria({ !it.isSafe() }) { - idea("eat") { + idea("eat", 10) { criteria { !it.isSafe() } action { EatFoodEvent(it, it) } } @@ -20,6 +20,7 @@ fun testAiPackages() { } } aiPackage("predator") { + priority("eat", 15) template("animal") idea("hunt") { criteria { it.soul.getConditions().isEmpty() }