Checkpoint
This commit is contained in:
parent
1b3280a947
commit
095659cf30
4 changed files with 70 additions and 15 deletions
|
@ -6,4 +6,5 @@ parent(mary, jimmy).
|
|||
father(X, Y) :- parent(X, Y), male(X).
|
||||
mother(X, Y) :- parent(X, Y), female(X).
|
||||
|
||||
kan_goed_koken(miriam).
|
||||
foo(0).
|
||||
foo(X) :- X > 0, Y is X - 1, foo(Y).
|
||||
|
|
|
@ -3,15 +3,14 @@ package prolog
|
|||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import prolog.ast.arithmetic.Integer
|
||||
import prolog.ast.logic.Fact
|
||||
import prolog.ast.logic.Rule
|
||||
import prolog.builtins.Conjunction
|
||||
import prolog.builtins.Disjunction
|
||||
import prolog.builtins.Query
|
||||
import prolog.logic.equivalent
|
||||
import prolog.ast.terms.Atom
|
||||
import prolog.ast.terms.Structure
|
||||
import prolog.ast.terms.Variable
|
||||
import prolog.builtins.*
|
||||
|
||||
class EvaluationTests {
|
||||
@BeforeEach
|
||||
|
@ -215,4 +214,33 @@ class EvaluationTests {
|
|||
assertTrue(expectedResults[i].all { actualResults[i].getOrNull()!![it.key]?.let { it1 -> equivalent(it.value, it1, emptyMap()) } ?: false }, "Substitution values should match")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
foo(0).
|
||||
foo(X) :- X > 0, Y is X - 1, foo(Y).
|
||||
*/
|
||||
@Test
|
||||
fun recursive_query() {
|
||||
val fact = Fact(Structure(Atom("foo"), listOf(Integer(0))))
|
||||
val rule = Rule(
|
||||
Structure(Atom("foo"), listOf(Variable("X"))),
|
||||
Conjunction(
|
||||
GreaterThan(Variable("X"), Integer(0)),
|
||||
Conjunction(
|
||||
Is(Variable("Y"), Subtract(Variable("X"), Integer(1))),
|
||||
Structure(Atom("foo"), listOf(Variable("Y")))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
Program.load(listOf(fact, rule))
|
||||
|
||||
val result = Program.query(Structure(Atom("foo"), listOf(Integer(0)))).toList()
|
||||
|
||||
val result5 = Program.query(Structure(Atom("foo"), listOf(Integer(5)))).toList()
|
||||
|
||||
assertTrue(Program.query(Structure(Atom("foo"), listOf(Atom("1")))).any())
|
||||
assertTrue(Program.query(Structure(Atom("foo"), listOf(Atom("2")))).any())
|
||||
assertFalse(Program.query(Structure(Atom("foo"), listOf(Atom("-1")))).any())
|
||||
}
|
||||
}
|
Reference in a new issue