Skip to content

Commit

Permalink
support locale-independent term ordering on the jvm
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Oct 23, 2020
1 parent a72f328 commit fb21809
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@file:JvmName("ComparisonUtils")

package it.unibo.tuprolog.core

import kotlin.jvm.JvmName

expect fun compareStringsLocaleIndependently(string1: String, string2: String): Int
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package it.unibo.tuprolog.core
interface TermComparator<T : Term> : Comparator<T> {
object AtomComparator : TermComparator<Atom> {
override fun compare(a: Atom, b: Atom): Int =
a.value.compareTo(b.value)
compareStringsLocaleIndependently(a.value, b.value)
}

object VarComparator : TermComparator<Var> {
override fun compare(a: Var, b: Var): Int =
a.completeName.compareTo(b.completeName)
compareStringsLocaleIndependently(a.completeName, b.completeName)
}

object RealComparator : TermComparator<Real> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,9 @@ import kotlin.test.Test
import kotlin.test.assertEquals

class TermComparisonTest {
val ordered = listOf<Term>(
Var.of("A"),
Var.of("A"),
Var.of("B"),
Var.of("C"),
Var.anonymous(),
Var.anonymous(),
Real.of(1.1),
Real.of(2.2),
Integer.of(1),
Integer.of(2),
Atom.of(""),
Atom.of("a"),
Atom.of("a b"),
Atom.of("b"),
Struct.of("f", Var.of("A")),
Struct.of("f", Var.of("A")),
Struct.of("f", Var.of("B")),
Struct.of("f", Var.of("C")),
Struct.of("f", Var.anonymous()),
Struct.of("f", Real.of(1.1)),
Struct.of("f", Real.of(2.2)),
Struct.of("f", Integer.of(1)),
Struct.of("f", Integer.of(2)),
Struct.of("f", Atom.of("")),
Struct.of("f", Atom.of("a")),
Struct.of("f", Atom.of("a b")),
Struct.of("f", Atom.of("b")),
Struct.of("g", Var.of("A")),
Struct.of("g", Var.of("A")),
Struct.of("g", Var.of("B")),
Struct.of("g", Var.of("C")),
Struct.of("g", Var.anonymous()),
Struct.of("g", Real.of(1.1)),
Struct.of("g", Real.of(2.2)),
Struct.of("g", Integer.of(1)),
Struct.of("g", Integer.of(2)),
Struct.of("g", Atom.of("")),
Struct.of("g", Atom.of("a")),
Struct.of("g", Atom.of("a b")),
Struct.of("g", Atom.of("b"))
)
private val ordered = orderedTerms

val unordered = ordered.shuffled()
private val unordered = ordered.shuffled()

@Test
fun testCompareTo() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package it.unibo.tuprolog.core

import kotlin.collections.List

expect val orderedTerms: List<Term>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package it.unibo.tuprolog.core

actual fun compareStringsLocaleIndependently(string1: String, string2: String): Int =
string1.compareTo(string2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package it.unibo.tuprolog.core

import kotlin.collections.List

actual val orderedTerms: List<Term> = listOf(
Var.of("A"),
Var.of("A"),
Var.of("B"),
Var.of("C"),
Var.anonymous(),
Var.anonymous(),
Real.of(1.1),
Real.of(2.2),
Integer.of(1),
Integer.of(2),
Atom.of(""),
Atom.of("a"),
Atom.of("a b"),
Atom.of("b"),
Struct.of("f", Var.of("A")),
Struct.of("f", Var.of("A")),
Struct.of("f", Var.of("B")),
Struct.of("f", Var.of("C")),
Struct.of("f", Var.anonymous()),
Struct.of("f", Real.of(1.1)),
Struct.of("f", Real.of(2.2)),
Struct.of("f", Integer.of(1)),
Struct.of("f", Integer.of(2)),
Struct.of("f", Atom.of("")),
Struct.of("f", Atom.of("a")),
Struct.of("f", Atom.of("a b")),
Struct.of("f", Atom.of("b")),
Struct.of("g", Var.of("A")),
Struct.of("g", Var.of("A")),
Struct.of("g", Var.of("B")),
Struct.of("g", Var.of("C")),
Struct.of("g", Var.anonymous()),
Struct.of("g", Real.of(1.1)),
Struct.of("g", Real.of(2.2)),
Struct.of("g", Integer.of(1)),
Struct.of("g", Integer.of(2)),
Struct.of("g", Atom.of("")),
Struct.of("g", Atom.of("a")),
Struct.of("g", Atom.of("a b")),
Struct.of("g", Atom.of("b"))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package it.unibo.tuprolog.core

import java.text.Collator

actual fun compareStringsLocaleIndependently(string1: String, string2: String): Int =
Collator.getInstance().compare(string1, string2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package it.unibo.tuprolog.core

import kotlin.collections.List

actual val orderedTerms: List<Term> = listOf(
Var.anonymous(),
Var.anonymous(),
Var.of("A"),
Var.of("A"),
Var.of("B"),
Var.of("C"),
Real.of(1.1),
Real.of(2.2),
Integer.of(1),
Integer.of(2),
Atom.of(""),
Atom.of("a"),
Atom.of("a b"),
Atom.of("b"),
Struct.of("f", Var.anonymous()),
Struct.of("f", Var.of("A")),
Struct.of("f", Var.of("A")),
Struct.of("f", Var.of("B")),
Struct.of("f", Var.of("C")),
Struct.of("f", Real.of(1.1)),
Struct.of("f", Real.of(2.2)),
Struct.of("f", Integer.of(1)),
Struct.of("f", Integer.of(2)),
Struct.of("f", Atom.of("")),
Struct.of("f", Atom.of("a")),
Struct.of("f", Atom.of("a b")),
Struct.of("f", Atom.of("b")),
Struct.of("g", Var.anonymous()),
Struct.of("g", Var.of("A")),
Struct.of("g", Var.of("A")),
Struct.of("g", Var.of("B")),
Struct.of("g", Var.of("C")),
Struct.of("g", Real.of(1.1)),
Struct.of("g", Real.of(2.2)),
Struct.of("g", Integer.of(1)),
Struct.of("g", Integer.of(2)),
Struct.of("g", Atom.of("")),
Struct.of("g", Atom.of("a")),
Struct.of("g", Atom.of("a b")),
Struct.of("g", Atom.of("b"))
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class AbstractInvoke(suffix: String) : TernaryRelation.Functional<Execu
actuallyInvoke(first, method, third)
}
is Struct -> {
val ref = findRefFromAlias(first as Struct)
val ref = findRefFromAlias(first)
actuallyInvoke(ref, method, third)
}
else -> Substitution.failed()
Expand Down

0 comments on commit fb21809

Please sign in to comment.