Skip to content

Commit

Permalink
add model classes for engine,node and queue and implement the basic g…
Browse files Browse the repository at this point in the history
…eneric algorithm using them
  • Loading branch information
d-kozak committed Nov 5, 2018
1 parent 022f08a commit c949932
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/io/dkozak/search/engine/engine.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.dkozak.search.engine

import io.dkozak.search.model.EngineConfiguration
import io.dkozak.search.model.Node


fun search(engineConfiguration: EngineConfiguration, logger: (String) -> Unit = {}) = engineConfiguration.executeSearch(logger)

private fun EngineConfiguration.executeSearch(logger: (String) -> Unit): Node? {
val queue = searchQueue
queue.addNode(initialState)
while (!queue.isEmpty()) {
val nextNode = queue.next()
logger("Exploring state $nextNode")
if (isFinalState(nextNode)) {
logger("$nextNode is a final state, returning")
return nextNode
}
val children = expandNode(nextNode)
logger("Expanded node and got these children $children")
val filtered = children.filter(nodeFilter)
logger("Children after filtering $filtered")
queue.addNodes(filtered)
}
logger("Whole state space explored and no final node found, returning null")
return null
}
10 changes: 10 additions & 0 deletions src/main/io/dkozak/search/model/EngineConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.dkozak.search.model

class EngineConfiguration
(
val initialState: Node,
val searchQueue: SearchQueue,
val expandNode: (Node) -> List<Node>,
val nodeFilter: (Node) -> Boolean,
val isFinalState: (Node) -> Boolean
)
4 changes: 4 additions & 0 deletions src/main/io/dkozak/search/model/Node.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.dkozak.search.model

interface Node {
}
9 changes: 9 additions & 0 deletions src/main/io/dkozak/search/model/SearchQueue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.dkozak.search.model

interface SearchQueue {
fun next(): Node
fun addNodes(nodes: List<Node>)
fun isEmpty(): Boolean

fun addNode(node: Node) = addNodes(listOf(node))
}

0 comments on commit c949932

Please sign in to comment.