/// <summary> /// Private temporal AND function /// </summary> private static Tbool And(Tbool t1, Tbool t2) { // Short circuit: If any input is eternally false, return false if (t1.IsFalse || t2.IsFalse) return new Tbool(false); // Else, apply the AND function to the inputs Tbool result = new Tbool(); foreach(KeyValuePair<DateTime,List<Hval>> slice in TimePointValues(t1,t2)) { result.AddState(slice.Key, CoreAnd(slice.Value)); } return result.Lean; }
/// <summary> /// Represents a nested if-then statement within in a boolean expression. /// </summary> public static Tbool IfThen(Tbool tb1, Tbool tb2) { return(!tb1 || tb2); }
/// <summary> /// Represents a nested if-then statement within in a boolean expression. /// </summary> public static Tbool IfThen(Tbool tb1, Tbool tb2) { return !tb1 || tb2; }
public Tnum RoundToNearest(Tnum multiple, Tbool breakTieByRoundingDown) { Tnum diff = this % multiple; return Switch<Tnum>(() => diff > multiple / 2, () => this - diff + multiple, ()=> diff < multiple / 2 || breakTieByRoundingDown, ()=> this - diff, () => true, () => this - diff + multiple); }
public void TestTvar10() { Tbool t2 = new Tbool(true); Assert.AreEqual(true, t2.Out); }
/// <summary> /// Scans the assumption table, looking for forward-chaining inferences. /// </summary> public static void TriggerInferences(string rel, Thing e1, Thing e2, Thing e3, Tvar val) { foreach (Pair p in Pairs) { // If A, then B if (p.LeftHandPoint.Relationship == rel) { // TODO: Currently only handles expressions that are eternally true if (Tvar.EqualTo(p.LeftHandPoint.Value, val)) { // For each rightPoint.Arg number, get the corresponding Thing Thing[] args = new Thing[3] { e1, e2, e3 }; int a1 = p.RightHandPoint.Arg1 - 1; // -1 b/c array is base-zero int a2 = p.RightHandPoint.Arg2 - 1; int a3 = p.RightHandPoint.Arg3 - 1; Thing t1 = a1 >= 0 ? args[a1] : null; Thing t2 = a2 >= 0 ? args[a2] : null; Thing t3 = a3 >= 0 ? args[a3] : null; Facts.Assert(t1, p.RightHandPoint.Relationship, t2, t3, p.RightHandPoint.Value); } } // If -B, then -A else if (p.RightHandPoint.Relationship == rel) { // If right-hand expression is always false... if (Tvar.EqualTo(p.RightHandPoint.Value, val).IsFalse) { // If the left-hand side is a boolean (non-booleans can't be negated)... if (Tvar.EqualTo(p.LeftHandPoint.Value, new Tbool(true)) || Tvar.EqualTo(p.LeftHandPoint.Value, new Tbool(false))) { // For each leftPoint.Arg number, get the corresponding Thing int r1 = p.RightHandPoint.Arg1; int r2 = p.RightHandPoint.Arg2; int r3 = p.RightHandPoint.Arg3; // I hope no one sees how ugly this is Thing t1, t2, t3; if (r1 == 1) { t1 = e1; } else if (r2 == 1) { t1 = e2; } else { t1 = e3; } if (r1 == 2) { t2 = e1; } else if (r2 == 2) { t2 = e2; } else { t2 = e3; } if (r1 == 3) { t3 = e1; } else if (r2 == 3) { t3 = e2; } else { t3 = e3; } // Assert -A Tbool leftVal = (Tbool)p.LeftHandPoint.Value; Facts.Assert(t1, p.LeftHandPoint.Relationship, t2, t3, !leftVal); } } } } }
/// <summary> /// Asserts a given fact (of the proper Tvar type) /// </summary> private static void AssertFact(Factoid f) { // Instantiate relevant Things Thing t1 = f.Arg1.ToString() != "" ? Facts.AddThing(f.Arg1.ToString()) : null; Thing t2 = f.Arg2.ToString() != "" ? Facts.AddThing(f.Arg2.ToString()) : null; Thing t3 = f.Arg3.ToString() != "" ? Facts.AddThing(f.Arg3.ToString()) : null; // Sometimes I have my doubts about static typing... if (f.FactType == "Tbool") { Tbool val = new Tbool(); foreach (TemporalValue v in f.Timeline) { val.AddState(v.Date, new Hval(v.Value)); } Facts.Assert(t1, f.Relationship, t2, t3, val); } else if (f.FactType == "Tnum") { Tnum val = new Tnum(); foreach (TemporalValue v in f.Timeline) { val.AddState(v.Date, new Hval(v.Value)); } Facts.Assert(t1, f.Relationship, t2, t3, val); } else if (f.FactType == "Tstr") { Tstr val = new Tstr(); foreach (TemporalValue v in f.Timeline) { val.AddState(v.Date, new Hval(v.Value)); } Facts.Assert(t1, f.Relationship, t2, t3, val); } else if (f.FactType == "Tdate") { Tdate val = new Tdate(); foreach (TemporalValue v in f.Timeline) { val.AddState(v.Date, new Hval(v.Value)); } Facts.Assert(t1, f.Relationship, t2, t3, val); } else if (f.FactType == "Tset") { Tset val = new Tset(); foreach (TemporalValue v in f.Timeline) { val.AddState(v.Date, new Hval(v.Value)); } Facts.Assert(t1, f.Relationship, t2, t3, val); } }
/// <summary> /// Returns a Tbool that's true up to a specified DateTime, and false /// at and after it. /// </summary> public static Tbool IsBefore(Tdate dt) { // Handle unknowns if (!dt.FirstValue.IsKnown) { return new Tbool(dt.FirstValue); } // Create boolean Tbool result = new Tbool(); if (dt == DawnOf) { result.AddState(DawnOf, false); } else { result.AddState(DawnOf, true); result.AddState(dt.ToDateTime, false); } return result; }