Skip to content

Commit

Permalink
move things in TheoryUtils.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 20, 2020
1 parent 5cf72be commit b2de378
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,22 @@ import it.unibo.tuprolog.collections.ClauseCollection
import it.unibo.tuprolog.collections.PrototypeClauseCollectionTest
import it.unibo.tuprolog.collections.RetrieveResult
import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.List as LogicList
import it.unibo.tuprolog.testutils.ClauseAssertionUtils.assertClausesHaveSameLengthAndContent
import it.unibo.tuprolog.testutils.ClauseAssertionUtils.assertTermsAreEqual
import kotlin.test.*
import it.unibo.tuprolog.testutils.TheoryUtils.deepClause
import it.unibo.tuprolog.testutils.TheoryUtils.deepQueries
import it.unibo.tuprolog.testutils.TheoryUtils.member
import it.unibo.tuprolog.testutils.TheoryUtils.memberClause
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import it.unibo.tuprolog.core.List as LogicList

internal abstract class PrototypeClauseCollectionTestImpl(
private val emptyGenerator: () -> ClauseCollection,
private val collectionGenerator: (Iterable<Clause>) -> ClauseCollection
) : PrototypeClauseCollectionTest {

private val member = Scope.empty {
factOf(structOf("member", varOf("H"), consOf(varOf("H"), anonymous())))
}

private val deep =
Fact.of(
LogicList.of(
LogicList.of(
LogicList.of(
Atom.of("a"),
Atom.of("b")
),
Atom.of("c")
),
Atom.of("d")
)
)

private val deepQueries = sequenceOf(
LogicList.of(Var.of("ABC"), Var.of("D")),
LogicList.of(LogicList.of(Var.of("AB"), Var.of("C")), Var.of("D")),
LogicList.of(LogicList.of(LogicList.of(Var.of("A"), Var.of("B")), Var.of("C")), Var.of("D"))
).map { Fact.of(it) }.toList()

private val fFamilySelector =
Fact.of(Struct.of("f", Var.anonymous()))

Expand Down Expand Up @@ -66,18 +48,15 @@ internal abstract class PrototypeClauseCollectionTestImpl(

private val emptyCollection = emptyGenerator()

private fun member(first: Term, second: Term): Fact =
Fact.of(Struct.of("member", first, second))

protected abstract fun getClauses(collection: ClauseCollection, query: Clause): Sequence<Clause>

protected abstract fun retractClauses(collection: ClauseCollection, query: Clause): Sequence<Clause>

override fun getTakesUnificationIntoAccount() {
val collection = collectionGenerator(listOf(member))
val collection = collectionGenerator(memberClause)

assertClausesHaveSameLengthAndContent(
sequenceOf(member),
memberClause.asSequence(),
getClauses(collection, Scope.empty {
member(
listOf(structOf("a", varOf("X"))),
Expand All @@ -86,7 +65,7 @@ internal abstract class PrototypeClauseCollectionTestImpl(
})
)
assertClausesHaveSameLengthAndContent(
sequenceOf(member),
memberClause.asSequence(),
getClauses(collection, member(Atom.of("a"), LogicList.of(Atom.of("a"))))
)
assertClausesHaveSameLengthAndContent(
Expand All @@ -105,10 +84,10 @@ internal abstract class PrototypeClauseCollectionTestImpl(
}

override fun retractTakesUnificationIntoAccount() {
var collection = collectionGenerator(listOf(member))
var collection = collectionGenerator(memberClause)

assertClausesHaveSameLengthAndContent(
sequenceOf(member),
memberClause.asSequence(),
retractClauses(collection, Scope.empty {
member(
listOf(structOf("a", varOf("X"))),
Expand All @@ -117,14 +96,14 @@ internal abstract class PrototypeClauseCollectionTestImpl(
})
)

collection = collectionGenerator(listOf(member))
collection = collectionGenerator(memberClause)

assertClausesHaveSameLengthAndContent(
sequenceOf(member),
memberClause.asSequence(),
retractClauses(collection, member(Atom.of("a"), LogicList.of(Atom.of("a"))))
)

collection = collectionGenerator(listOf(member))
collection = collectionGenerator(memberClause)

assertClausesHaveSameLengthAndContent(
sequenceOf(),
Expand All @@ -136,7 +115,7 @@ internal abstract class PrototypeClauseCollectionTestImpl(
})
)

collection = collectionGenerator(listOf(member))
collection = collectionGenerator(memberClause)

assertClausesHaveSameLengthAndContent(
sequenceOf(),
Expand All @@ -145,17 +124,17 @@ internal abstract class PrototypeClauseCollectionTestImpl(
}

override fun nestedGetWorksAtSeveralDepthLevels() {
val collection = collectionGenerator(listOf(deep))
val collection = collectionGenerator(deepClause)

for (query in deepQueries) {
assertClausesHaveSameLengthAndContent(sequenceOf(deep), getClauses(collection, query))
assertClausesHaveSameLengthAndContent(deepClause.asSequence(), getClauses(collection, query))
}
}

override fun nestedRetractWorksAtSeveralDepthLevels() {
for (query in deepQueries) {
val collection = collectionGenerator(listOf(deep))
assertClausesHaveSameLengthAndContent(sequenceOf(deep), retractClauses(collection, query))
val collection = collectionGenerator(deepClause)
assertClausesHaveSameLengthAndContent(deepClause.asSequence(), retractClauses(collection, query))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.unibo.tuprolog.testutils

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.List as LogicList
import it.unibo.tuprolog.theory.Theory

/**
Expand Down Expand Up @@ -40,4 +41,33 @@ internal object TheoryUtils {
Rule.of(Struct.of("f2", Atom.of("a")), Atom.of("do_something"), Numeric.of(1.5f))
)

internal val memberClause = listOf(
Scope.empty {
factOf(structOf("member", varOf("H"), consOf(varOf("H"), anonymous())))
}
)

internal fun member(first: Term, second: Term): Fact =
Fact.of(Struct.of("member", first, second))

internal val deepClause = listOf(
Fact.of(
LogicList.of(
LogicList.of(
LogicList.of(
Atom.of("a"),
Atom.of("b")
),
Atom.of("c")
),
Atom.of("d")
)
)
)

internal val deepQueries = sequenceOf(
LogicList.of(Var.of("ABC"), Var.of("D")),
LogicList.of(LogicList.of(Var.of("AB"), Var.of("C")), Var.of("D")),
LogicList.of(LogicList.of(LogicList.of(Var.of("A"), Var.of("B")), Var.of("C")), Var.of("D"))
).map { Fact.of(it) }.toList()
}

0 comments on commit b2de378

Please sign in to comment.