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)); }
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"); }
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)); }
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"); }
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)); }
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)); }
/// <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); }
/// <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); }
/// <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"); }
public UnboundVariableQuery(PrimitiveQuery parent, Variable variable) { this.parent = parent; this.variable = variable; }
/// <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; }
/// <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; }
public void VariableUsedTwiceQueryTest() { var v = new Variable("v"); CollectionAssert.AreEqual(new object[] { "Newt" }, (KB.Query + v + "loves" + v).FindAll(v)); }
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)); }
public void QueryNameTest() { var x = new Variable("x"); Assert.AreEqual("John+loves+x", (KB.Query + "John" + "loves" + x).ToString()); }