-
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.
add model classes for engine,node and queue and implement the basic g…
…eneric algorithm using them
- Loading branch information
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
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,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 | ||
} |
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,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 | ||
) |
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,4 @@ | ||
package io.dkozak.search.model | ||
|
||
interface Node { | ||
} |
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,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)) | ||
} |