Skip to content

Commit

Permalink
Merge branch 'feature/libraries' into feature/testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Aug 31, 2020
2 parents d4c2a39 + 0ecd735 commit d189754
Show file tree
Hide file tree
Showing 20 changed files with 790 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package it.unibo.tuprolog.solve.systemtest

import it.unibo.tuprolog.solve.*
import it.unibo.tuprolog.solve.channel.InputChannel
import it.unibo.tuprolog.solve.channel.OutputChannel
import it.unibo.tuprolog.solve.exception.PrologWarning
import it.unibo.tuprolog.solve.library.AliasedLibrary
import it.unibo.tuprolog.solve.library.Libraries
import it.unibo.tuprolog.solve.stdlib.DefaultBuiltins
import it.unibo.tuprolog.theory.Theory
import kotlin.test.Ignore
import kotlin.test.Test

class TestClassicSolver : TestSolver, SolverFactory by ClassicSolverFactory {
Expand Down Expand Up @@ -294,4 +288,63 @@ class TestClassicSolver : TestSolver, SolverFactory by ClassicSolverFactory {
override fun testAppend() {
prototype.testAppend()
}

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

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

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

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

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

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

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

@Test
@Ignore
override fun testAtomLength() {
prototype.testAtomLength()
}

@Test
@Ignore
override fun testCharCode() {
prototype.testCharCode()
}

@Test
@Ignore
override fun testAtomCodes() {
prototype.testAtomCodes()
}

@Test
@Ignore
override fun testAtomConcat() {
prototype.testAtomConcat()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,64 @@ class TestStreamsSolver : SolverFactory, TestSolver {
override fun testAppend() {
prototype.testAppend()
}

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

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

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

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

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

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

@Test
@Ignore
override fun testAtomChars() {
prototype.testAtomChars()
}

@Test
@Ignore
override fun testAtomLength() {
prototype.testAtomLength()
}

@Test
@Ignore
override fun testCharCode() {
prototype.testCharCode()
}

@Test
@Ignore
override fun testAtomCodes() {
prototype.testAtomCodes()
}

@Test
@Ignore
override fun testAtomConcat() {
prototype.testAtomConcat()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ class TypeError(

// TODO: 16/01/2020 test factories

fun forArgumentList(
context: ExecutionContext,
procedure: Signature,
expectedType: Expected,
actualValue: Term,
index: Int? = null
) = TypeError(
message = "Argument ${index
?: ""} of `${procedure.toIndicator()}` should be a list of `$expectedType`, but `$actualValue` has been provided instead",
context = context,
expectedType = expectedType,
actualValue = actualValue,
extraData = actualValue
)

fun forArgument(
context: ExecutionContext,
procedure: Signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import it.unibo.tuprolog.solve.exception.error.DomainError
import it.unibo.tuprolog.solve.exception.error.InstantiationError
import it.unibo.tuprolog.solve.exception.error.TypeError
import org.gciatto.kt.math.BigInteger
import it.unibo.tuprolog.core.List as LogicList

/**
* Wrapper class for [Primitive] implementation
Expand Down Expand Up @@ -142,6 +143,12 @@ abstract class PrimitiveWrapper<C : ExecutionContext> : AbstractWrapper<Primitiv
else -> this
}

fun <C : ExecutionContext> Solve.Request<C>.ensuringArgumentIsList(index: Int): Solve.Request<C> =
when (val arg = arguments[index]) {
!is LogicList -> throw TypeError.forArgument(context, signature, TypeError.Expected.LIST, arg, index)
else -> this
}

fun <C : ExecutionContext> Solve.Request<C>.ensuringArgumentIsNonNegativeInteger(index: Int): Solve.Request<C> =
arguments[index].let { arg ->
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ object CommonBuiltins : AliasedLibrary by Library.aliased(
AssertA,
AssertZ,
Atom,
AtomChars,
Atomic,
Between,
Callable,
Expand Down Expand Up @@ -52,6 +53,8 @@ object CommonBuiltins : AliasedLibrary by Library.aliased(
TermLowerThan,
TermLowerThanOrEqualTo,
TermNotIdentical,
TermNotSame,
TermSame,
UnifiesWith,
Univ,
Var,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.exception.error.TypeError
import it.unibo.tuprolog.solve.primitive.BinaryRelation
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith
import it.unibo.tuprolog.core.List as LogicList

object AtomChars : BinaryRelation.Functional<ExecutionContext>("atom_chars") {
override fun Solve.Request<ExecutionContext>.computeOneSubstitution(first: Term, second: Term): Substitution =
when {
first is Var && second is Var -> {
ensuringAllArgumentsAreInstantiated()
Substitution.failed()
}
first is Var -> {
ensuringArgumentIsList(1)
val chars = second as LogicList
val atom: Atom = Atom.of(
chars.toSequence().map {
if (it is Atom) {
it.value[0]
} else {
throw TypeError.forArgumentList(context, signature, TypeError.Expected.CHARACTER, it, 1)
}
}.joinToString("")
)
Substitution.of(first, atom)
}
second is Var -> {
ensuringArgumentIsAtom(0)
val charArray = (first as Atom).value.toCharArray()
val chars = LogicList.of(charArray.map { Atom.of("" + it) })
Substitution.of(second, chars)
}
else -> {
ensuringArgumentIsAtom(0)
ensuringArgumentIsList(1)
val chars = LogicList.of(
(first as Atom).value.toCharArray().map { Atom.of("" + it) }
)
chars mguWith 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.primitive.Solve
import it.unibo.tuprolog.solve.primitive.BinaryRelation

/** Implementation of '=='/2 predicate */
object TermNotSame : BinaryRelation.Predicative<ExecutionContext>("\\=@=") {
override fun Solve.Request<ExecutionContext>.compute(first: Term, second: Term): Boolean {
return first.compareTo(second) != 0
}
}
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.primitive.Solve
import it.unibo.tuprolog.solve.primitive.BinaryRelation

/** Implementation of '=='/2 predicate */
object TermSame : BinaryRelation.Predicative<ExecutionContext>("=@=") {
override fun Solve.Request<ExecutionContext>.compute(first: Term, second: Term): Boolean {
return first.compareTo(second) == 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,27 @@ interface TestSolver : SolverTest {
fun testUniv()
fun testAppend()
fun testRetractAll()

fun testTermGreaterThan()
fun testTermGreaterThanOrEqual()
fun testTermSame()
fun testTermNotSame()
fun testTermLowerThan()
fun testTermLowerThanOrEqual()

/** atom_chars/2 test */
fun testAtomChars()

/** atom_length test */
fun testAtomLength()

/** char_code/2 test */
fun testCharCode()

/** atom_codes/2 test */
fun testAtomCodes()

/** atom_concat/3 test */
fun testAtomConcat()
}

Loading

0 comments on commit d189754

Please sign in to comment.