Skip to content

Commit

Permalink
Merge branch 'feature/arg2p-integration' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 25, 2020
2 parents 3930756 + 852da4e commit 5ed81d5
Show file tree
Hide file tree
Showing 27 changed files with 511 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,14 @@ class ClassicSolverSystemTesting : SolverFactory, SolverTest {
override fun testUniv() {
prototype.testUniv()
}

@Test
override fun testRetractAll() {
prototype.testRetractAll()
}

@Test
override fun testAppend() {
prototype.testAppend()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,14 @@ class StreamsSolverSystemTesting : SolverFactory, SolverTest {
override fun testFunctor() {
prototype.testFunctor()
}

@Test
override fun testRetractAll() {
prototype.testRetractAll()
}

@Test
override fun testAppend() {
prototype.testAppend()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ object CommonBuiltins : AliasedLibrary by Library.of(
Number,
Op,
Retract,
RetractAll,
Sleep,
TermGreaterThan,
TermGreaterThanOrEqualTo,
TermIdentical,
TermLowerThan,
TermLowerThanOrEqualTo,
TermNotIdentical,
UnifiesWith,
Univ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package it.unibo.tuprolog.solve.stdlib

import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.rule.RuleWrapper
import it.unibo.tuprolog.solve.stdlib.rule.Arrow
import it.unibo.tuprolog.solve.stdlib.rule.Member
import it.unibo.tuprolog.solve.stdlib.rule.Not
import it.unibo.tuprolog.solve.stdlib.rule.Semicolon
import it.unibo.tuprolog.solve.stdlib.rule.*
import it.unibo.tuprolog.theory.Theory

object CommonRules {
Expand All @@ -17,7 +14,9 @@ object CommonRules {
Semicolon.Or.Left,
Semicolon.Or.Right,
Member.Base,
Member.Recursive
Member.Recursive,
Append.Base,
Append.Recursive
)

val theory: Theory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.primitive.UnaryPredicate
import it.unibo.tuprolog.theory.RetractResult
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith

object RetractAll : UnaryPredicate.NonBacktrackable<ExecutionContext>("retractall") {
override fun Solve.Request<ExecutionContext>.computeOne(first: Term): Solve.Response {
ensuringArgumentIsStruct(0)
val clause = if (first is Clause) first else Rule.of(first as Struct, Var.anonymous())
val dynamicKb = context.dynamicKb
val result = dynamicKb.retractAll(clause)
return replySuccess(dynamicKb = result.theory)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.primitive.BinaryRelation

/** Implementation of '@>'/2 predicate */
object TermGreaterThan : BinaryRelation.Predicative<ExecutionContext>("@>") {
override fun Solve.Request<ExecutionContext>.compute(first: Term, second: Term): Boolean {
return first > second
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.primitive.BinaryRelation

/** Implementation of '@>='/2 predicate */
object TermGreaterThanOrEqualTo : BinaryRelation.Predicative<ExecutionContext>("@>=") {
override fun Solve.Request<ExecutionContext>.compute(first: Term, second: Term): Boolean {
return first >= second
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.primitive.BinaryRelation

/** Implementation of '@<'/2 predicate */
object TermLowerThan : BinaryRelation.Predicative<ExecutionContext>("@<") {
override fun Solve.Request<ExecutionContext>.compute(first: Term, second: Term): Boolean {
return first < second
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.primitive.BinaryRelation

/** Implementation of '@=<'/2 predicate */
object TermLowerThanOrEqualTo : BinaryRelation.Predicative<ExecutionContext>("@=<") {
override fun Solve.Request<ExecutionContext>.compute(first: Term, second: Term): Boolean {
return first <= second
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package it.unibo.tuprolog.solve.stdlib.rule

import it.unibo.tuprolog.core.Scope
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Signature
import it.unibo.tuprolog.solve.rule.RuleWrapper
import kotlin.collections.List as KtList
import kotlin.collections.listOf as ktListOf

sealed class Append : RuleWrapper<ExecutionContext>(FUNCTOR, ARITY) {

abstract override val Scope.head: KtList<Term>

object Base : Append() {
override val Scope.head: KtList<Term>
get() = ktListOf(
emptyList(),
varOf("X"),
varOf("X")
)
}

object Recursive : Append() {
override val Scope.head: KtList<Term>
get() = ktListOf(
consOf(varOf("X"), varOf("Y")),
varOf("Z"),
consOf(varOf("X"), varOf("W"))
)

override val Scope.body: Term
get() = structOf("append", varOf("Y"), varOf("Z"), varOf("W"))
}

companion object {
const val FUNCTOR: String = "append"
const val ARITY: Int = 3
val SIGNATURE: Signature
get() = Signature(FUNCTOR, ARITY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,7 @@ interface SolverTest {
fun testNatural()
fun testFunctor()
fun testUniv()
fun testAppend()
fun testRetractAll()
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ import it.unibo.tuprolog.solve.exception.TimeOutException
import it.unibo.tuprolog.solve.exception.error.InstantiationError
import it.unibo.tuprolog.solve.exception.error.TypeError
import it.unibo.tuprolog.solve.stdlib.primitive.*
import it.unibo.tuprolog.solve.stdlib.rule.Arrow
import it.unibo.tuprolog.solve.stdlib.rule.Member
import it.unibo.tuprolog.solve.stdlib.rule.Not
import it.unibo.tuprolog.solve.stdlib.rule.Semicolon
import it.unibo.tuprolog.solve.stdlib.rule.*
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
Expand All @@ -79,6 +76,7 @@ internal class SolverTestImpl(private val solverFactory: SolverFactory) : Solver
assertHasPredicateInAPI(Member.SIGNATURE)
assertHasPredicateInAPI(Not)
assertHasPredicateInAPI(Semicolon.SIGNATURE)
assertHasPredicateInAPI(Append.SIGNATURE)
assertHasPredicateInAPI(ArithmeticEqual)
assertHasPredicateInAPI(ArithmeticGreaterThan)
assertHasPredicateInAPI(ArithmeticGreaterThanOrEqualTo)
Expand All @@ -105,8 +103,13 @@ internal class SolverTestImpl(private val solverFactory: SolverFactory) : Solver
assertHasPredicateInAPI(NotUnifiableWith)
assertHasPredicateInAPI(Number)
assertHasPredicateInAPI(Retract)
assertHasPredicateInAPI(RetractAll)
assertHasPredicateInAPI(Sleep)
assertHasPredicateInAPI(TermGreaterThan)
assertHasPredicateInAPI(TermGreaterThanOrEqualTo)
assertHasPredicateInAPI(TermIdentical)
assertHasPredicateInAPI(TermLowerThan)
assertHasPredicateInAPI(TermLowerThanOrEqualTo)
assertHasPredicateInAPI(TermNotIdentical)
assertHasPredicateInAPI(UnifiesWith)
assertHasPredicateInAPI(Univ)
Expand Down Expand Up @@ -1045,4 +1048,89 @@ internal class SolverTestImpl(private val solverFactory: SolverFactory) : Solver
)
}
}

override fun testRetractAll() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins(
dynamicKb = theoryOf(
factOf(structOf("f", numOf(1))),
ruleOf(structOf("f", numOf(2)), atomOf("false"))
)
)

var query = "retractall"("f"("X"))

var solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
ktListOf(
query.yes()
),
solutions
)

assertEquals(
ktListOf(),
solver.dynamicKb.toList()
)
assertEquals(0L, solver.dynamicKb.size)

query = "retractall"("f"("X"))
solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
ktListOf(
query.yes()
),
solutions
)
}
}

override fun testAppend() {
prolog {
val solver = solverFactory.solverWithDefaultBuiltins()

var query = "append"(listOf(1, 2, 3), listOf(4, 5, 6), "X")
var solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
ktListOf(query.yes("X" to listOf(1, 2, 3, 4, 5, 6))),
solutions
)

query = "append"(listOf(1, 2, 3), "X", listOf(1, 2, 3, 4, 5, 6))
solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
ktListOf(query.yes("X" to listOf(4, 5, 6))),
solutions
)

query = "append"("X", "X", listOf(1, 2, 3, 4, 5, 6))
solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
ktListOf(query.no()),
solutions
)

query = "append"("X", "Y", listOf(1, 2, 3, 4, 5, 6))
solutions = solver.solve(query, mediumDuration).toList()

assertSolutionEquals(
ktListOf(
query.yes("X" to emptyList(), "Y" to listOf(1, 2, 3, 4, 5, 6)),
query.yes("X" to listOf(1), "Y" to listOf(2, 3, 4, 5, 6)),
query.yes("X" to listOf(1, 2), "Y" to listOf(3, 4, 5, 6)),
query.yes("X" to listOf(1, 2, 3), "Y" to listOf(4, 5, 6)),
query.yes("X" to listOf(1, 2, 3, 4), "Y" to listOf(5, 6)),
query.yes("X" to listOf(1, 2, 3, 4, 5), "Y" to listOf(6)),
query.yes("X" to listOf(1, 2, 3, 4, 5, 6), "Y" to emptyList())
),
solutions
)

}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.BinaryRelationUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.equalQueryToResult
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.BinaryRelationUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.greaterOrEqualQueryToResult
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.BinaryRelationUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.greaterQueryToResult
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.BinaryRelationUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.lowerOrEqualQueryToResult
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.BinaryRelationUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.lowerQueryToResult
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.BinaryRelationUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.ArithmeticUtils.notEqualQueryToResult
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.solve.stdlib.primitive.testutils.BinaryRelationUtils.assertCorrectResponse
import it.unibo.tuprolog.solve.stdlib.primitive.testutils.TermUtils.greaterOrEqualQueryToResult
import kotlin.test.Test

/**
* Test class for [TermGreaterThanOrEqualTo]
*
*/
internal class TermGreaterThanOrEqualToTest {

@Test
fun computesCorrectResult() {
greaterOrEqualQueryToResult.forEach { (input, result) ->
assertCorrectResponse(TermGreaterThanOrEqualTo, input, result)
}
}
}
Loading

0 comments on commit 5ed81d5

Please sign in to comment.