Example #1
0
 public void MultiSolutionQueryTest()
 {
     var v = new Variable("v");
     CollectionAssert.AreEqual(new object[] { "Mary", "Kelly" }, (KB.Query + "John" + "loves" + v).FindAll(v));
     v = new Variable("v");
     CollectionAssert.AreEqual(new object[] { "John", "Kelly" }, (KB.Query + v + "loves" + "Mary").FindAll(v));
 }
Example #2
0
 public void RetractAllEndingWildcardTest()
 {
     var x = new Variable("x");
     KB.RetractAll(KB.Query + "John" + "loves" + x);
     Assert.IsFalse(KB.Query + "John" + "loves" + "Mary");
     Assert.IsFalse(KB.Query + "John" + "loves" + "Kelly");
     Assert.IsTrue(KB.Query + "John" + "loves");
     Assert.IsTrue(KB.Query + "John");
 }
Example #3
0
 public void ConjunctiveQueryTest()
 {
     var x = new Variable("x");
     var y = new Variable("y");
     var z = new Variable("z");
     CollectionAssert.AreEqual(
         new object[] {"Mary"},
         (KB.Query + x + "loves" + y & KB.Query+z+"loves"+y & x != z).FindAllUnique(y));
 }
Example #4
0
 public void RetractAllPatternTest()
 {
     var x = new Variable("x");
     KB.RetractAll(KB.Query + x + "loves" + x);
     Assert.IsTrue(KB.Query + "John" + "loves" + "Mary");
     Assert.IsTrue(KB.Query + "Kelly" + "loves" + "Mary");
     Assert.IsTrue(KB.Query + "John" + "loves" + "Kelly");
     Assert.IsTrue(KB.Query + "John" + "loves");
     Assert.IsTrue(KB.Query + "John");
     Assert.IsFalse(KB.Query+"Newt" + "loves" + "Newt");
 }
Example #5
0
        public void SimpleRuleTest()
        {
            // Update: Mary loves everyone who loves her.
            var v = new Variable("v");
            new RuleSet("test ruleset",
                new Rule(
                    "Mary loves those who love her",
                    KB.Query + v + "loves" + "Mary",
                    () => KB.Assert(KB.Store + "Mary" + "loves" + v))).Run();

            var v2 = new Variable("v2");
            CollectionAssert.AreEqual(new object[] { "John", "Kelly" }, (KB.Query + "Mary" + "loves" + v2).FindAll(v2));
        }
Example #6
0
        public void StoreVariableTest()
        {
            KB.Assert(KB.Store + "John" + "loves" + "Mary");
            KB.Assert(KB.Store + "John" + "loves" + "Kelly");
            KB.Assert(KB.Store + "John" + "loves" + "Mary");
            KB.Assert(KB.Store + "Kelly" + "loves" + "Mary");

            // Update: Mary loves everyone who loves her.
            var v = new Variable("v");
            (KB.Query + v + "loves" + "Mary").DoAll(
                () => KB.Assert(KB.Store+"Mary"+"loves"+v)
                );

            var v2 = new Variable("v2");
            CollectionAssert.AreEqual(new object[] { "John", "Kelly"},
                (KB.Query + "Mary" + "loves" + v2).FindAll(v2));
        }
Example #7
0
 /// <summary>
 /// Store a non-exclusive child inside this node.
 /// </summary>
 /// <param name="v">Bound variable holding the key for the child</param>
 /// <returns>The child node</returns>
 /// <exception cref="KBExclusionException">If an exclusive child has already been written.</exception>
 public KnowledgeBaseEntry StoreNonExclusive(Variable v)
 {
     return StoreNonExclusive(v.Value);
 }
Example #8
0
 /// <summary>
 /// Store an exclusive child inside this node.
 /// </summary>
 /// <param name="v">Bound variable holding the key for the child</param>
 /// <param name="overwrite">If true, this will overwrite any existing child with a different value.</param>
 /// <returns>The child node</returns>
 /// <exception cref="KBExclusionException">If a non-exclusive child has already been written.</exception>
 public KnowledgeBaseEntry StoreExclusive(Variable v, bool overwrite)
 {
     return StoreExclusive(v.Value, overwrite);
 }
Example #9
0
 /// <summary>
 /// Returns value of variable in first result of query
 /// </summary>
 /// <param name="v">Variable to find values of</param>
 public object Find(Variable v)
 {
     this.Reset();
     if (this.TryNext())
         return v.Value;
     throw new NoSolutionException("No solution to query");
 }
Example #10
0
 public UnboundVariableQuery(PrimitiveQuery parent, Variable variable)
 {
     this.parent = parent;
     this.variable = variable;
 }
Example #11
0
 /// <summary>
 /// Returns values of variable from all solutions of query, omitting duplicates.
 /// </summary>
 /// <param name="v">Variable to find values of</param>
 public List<object> FindAllUnique(Variable v)
 {
     var result = new List<object>();
     this.Reset();
     while (this.TryNext())
         if (!result.Contains(v.Value))
             result.Add(v.Value);
     return result;
 }
Example #12
0
 /// <summary>
 /// Returns values of variable from all solutions of query, retaining duplicates.
 /// </summary>
 /// <param name="v">Variable to find values of</param>
 public List<object> FindAll(Variable v)
 {
     var result = new List<object>();
     this.Reset();
     while (this.TryNext())
         result.Add(v.Value);
     return result;
 }
Example #13
0
 public void VariableUsedTwiceQueryTest()
 {
     var v = new Variable("v");
     CollectionAssert.AreEqual(new object[] { "Newt" }, (KB.Query + v + "loves" + v).FindAll(v));
 }
Example #14
0
 public void SimpleQueryTest()
 {
     var v = new Variable("v");
     Assert.AreEqual("John", (KB.Query+v+"loves"+"Kelly").Find(v));
     CollectionAssert.AreEqual(new object[] {"John"}, (KB.Query + v + "loves" + "Kelly").FindAll(v));
 }
Example #15
0
 public void QueryNameTest()
 {
     var x = new Variable("x");
     Assert.AreEqual("John+loves+x", (KB.Query + "John" + "loves" + x).ToString());
 }