Verified Commit eeae0583 authored by Marcin Moskal's avatar Marcin Moskal
Browse files

Handle queries with all-constant functors

parent 556ffe75
......@@ -15,6 +15,9 @@ object BindingSet {
case Seq(_: Const[A]) =>
BindingSetX(Map.empty)
case Seq(_: Const[A], _: Const[A]) =>
BindingSetX(Map.empty)
case Seq(v1: Variable[A], v2: Variable[A]) =>
BindingSetX(Map(
v1.name.get -> kb.facts2.map(_._1),
......@@ -26,7 +29,6 @@ object BindingSet {
case class BindingSetX[A](m: Map[String, Set[A]]) {
def expand(bs: BindingSetX[A]): BindingSetX[A] = {
// tutaj zbiór jest tworzony zbyt duży
BindingSetX(m ++ bs.m.map { case (k, v) => k -> (v intersect m.getOrElse(k, v)) })
}
}
......@@ -25,7 +25,7 @@ trait FactSelector[A] {
// calculate all possible variable configurations
crossJoin(getBindingSetVars(bindingSet)).map(_.toSet)
// bind all variables in query
// bind all variables in query (adds all-constant facts from query too)
.map(bind).toSet
// build facts from bound query and remove facts without evidence in kb
.filter(buildableToKnownFact)
......
......@@ -162,9 +162,39 @@ class KnowledgeBaseSpec extends FlatSpec with Matchers {
)
}
"Query ((x 1) (y 2))" should "be unified" ignore {
"Query ((x) (y) (z z))" should "be unified" in {
val kb: KnowledgeBase[Int] = new KnowledgeBase[Int](Set(
Tuple1(6), (3, 4), (4, 4), Tuple1(3), (6, 6)
))
val x = Variable[Int]("x")
val y = Variable[Int]("y")
val z = Variable[Int]("z")
kb.queryAll(Functor(x), Functor(y), Functor(z, z)).toSeq should contain theSameElementsAs Set(
Set(Variable("x", 6), Variable("y", 6), Variable("z", 4)),
Set(Variable("x", 6), Variable("y", 3), Variable("z", 4)),
Set(Variable("x", 3), Variable("y", 6), Variable("z", 4)),
Set(Variable("x", 3), Variable("y", 3), Variable("z", 4)),
Set(Variable("x", 6), Variable("y", 6), Variable("z", 6)),
Set(Variable("x", 6), Variable("y", 3), Variable("z", 6)),
Set(Variable("x", 3), Variable("y", 6), Variable("z", 6)),
Set(Variable("x", 3), Variable("y", 3), Variable("z", 6))
)
}
"Query ((x) (3))" should "fail to unify on nonexistent unary fact" in {
val kb: KnowledgeBase[Int] = new KnowledgeBase[Int](Set(
Tuple1(5), (1, 3)
))
kb.queryAll(Functor(Variable("x")), Functor(Const(3))) should be(empty)
}
"Query ((x) (y) (z z))" should "be unified" ignore {
"Query ((x) (4 5))" should "fail to unify on nonexistent binary fact" in {
val kb: KnowledgeBase[Int] = new KnowledgeBase[Int](Set(
Tuple1(1), (2, 5), Tuple1(4)
))
kb.queryAll(Functor(Variable("x")), Functor(Const(4), Const(5))) should be(empty)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment