First real program working
This commit is contained in:
parent
49ebcab10d
commit
20948a859c
|
|
@ -40,7 +40,7 @@ class Handler {
|
|||
stored.filter { co -> constraint.matches(co) && acceptable(co) }
|
||||
}
|
||||
|
||||
val match = matcher.lookupMatches(active).find { pm -> pm.rule.guard().all { prd -> askPredicate(prd) } }
|
||||
val match = matcher.lookupMatches(active).find { pm -> pm.rule.checkGuard(pm.logicalContext()) }
|
||||
|
||||
if (match != null) {
|
||||
for ((cst, occ) in match.discarded) {
|
||||
|
|
@ -68,17 +68,14 @@ class Handler {
|
|||
}
|
||||
}
|
||||
|
||||
private fun askPredicate(predicate: Predicate): Boolean =
|
||||
sessionSolver.ask(predicate.symbol(), * predicate.arguments().toTypedArray())
|
||||
private fun Rule.checkGuard(logicalContext: LogicalContext): Boolean =
|
||||
guard().all { prd -> askPredicate(prd.invocation(logicalContext)) }
|
||||
|
||||
private fun askPredicate(invocation: PredicateInvocation): Boolean =
|
||||
sessionSolver.ask(invocation.predicate().symbol(), * invocation.arguments().toTypedArray())
|
||||
|
||||
private fun tellPredicate(invocation: PredicateInvocation) {
|
||||
sessionSolver.tell(invocation.predicate().symbol(), * invocation.arguments().toTypedArray())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface HandlingContext {
|
||||
|
||||
fun substitute(logicalPattern: LogicalPattern<*>) : Any
|
||||
|
||||
}
|
||||
|
|
@ -30,8 +30,11 @@ inline fun <reified T: Any> logicalPattern(name1: String, name2: String, name3:
|
|||
|
||||
fun <T: Any> Logical<T>.get(): T = findRoot().value()
|
||||
|
||||
fun <T: Any> TestLogical<T>.set(t: T) {
|
||||
find().value = t
|
||||
fun <T: Any> Logical<T>.set(t: T) {
|
||||
if (this is TestLogical<T>)
|
||||
find().value = t
|
||||
else
|
||||
throw IllegalStateException("unexpected receiver $this")
|
||||
}
|
||||
|
||||
data class TestLogical<T>(val name: String, var value: T?, var parent: TestLogical<T>?) : Logical<T> {
|
||||
|
|
@ -69,6 +72,8 @@ data class TestLogical<T>(val name: String, var value: T?, var parent: TestLogic
|
|||
fun union(other: TestLogical<T>) {
|
||||
if (find() != other.find()) find().parent = other
|
||||
}
|
||||
|
||||
override fun toString(): String = "$name(^${parent?.name ?: null})=$value"
|
||||
}
|
||||
|
||||
data class TestLogicalPattern<T>(val name: String, val type: Class<T>) : LogicalPattern<T> {
|
||||
|
|
|
|||
|
|
@ -150,6 +150,6 @@ private data class TestConstraintOccurrence(val constraint: Constraint, val argu
|
|||
|
||||
override fun arguments(): Collection<Any> = arguments
|
||||
|
||||
override fun toString(): String = "#${constraint().symbol()}(${arguments().joinToString()})#${id}"
|
||||
override fun toString(): String = "${constraint().symbol()}(${arguments().joinToString()})"
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,10 +80,47 @@ class TestProgram {
|
|||
)
|
||||
).session("logicalValue").run {
|
||||
assertEquals(setOf(ConstraintSymbol("foo", 1), ConstraintSymbol("bar", 1)), constraintSymbols())
|
||||
assertEquals(2, constraintOccurrences().count())
|
||||
val yval = constraintOccurrences(ConstraintSymbol("bar", 1)).first().arguments().first()
|
||||
assertEquals(66, (yval as Logical<Int>).get())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun simpleProgram() {
|
||||
val (X, Y) = logicalPattern<Int>("X", "Y")
|
||||
|
||||
program(
|
||||
rule("main",
|
||||
headReplaced(
|
||||
constraint("main")
|
||||
),
|
||||
body(
|
||||
statement({ x -> x.set(5) }, X),
|
||||
constraint("val", X)
|
||||
)
|
||||
),
|
||||
rule("dec",
|
||||
headReplaced(
|
||||
constraint("val", X)
|
||||
),
|
||||
guard(
|
||||
expression({ x -> x.get() > 0 }, X)
|
||||
),
|
||||
body(
|
||||
constraint("trail", X),
|
||||
statement({ x, y -> y.set(x.get() - 1)}, X, Y),
|
||||
constraint("val", Y)
|
||||
)
|
||||
)
|
||||
).session("dec").run {
|
||||
assertEquals(setOf(ConstraintSymbol("val", 1), ConstraintSymbol("trail", 1)), constraintSymbols())
|
||||
assertEquals(1, constraintOccurrences(ConstraintSymbol.symbol("val", 1)).count())
|
||||
val a = constraintOccurrences(ConstraintSymbol.symbol("val", 1)).first().arguments().first()
|
||||
assertEquals(0, (a as Logical<Int>).get())
|
||||
assertEquals(5, constraintOccurrences(ConstraintSymbol.symbol("trail", 1)).count())
|
||||
println(constraintOccurrences(ConstraintSymbol.symbol("trail", 1)))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue