示例#1
0
        private void Calc_SlopeIntercept_General()
        {
            //slope and intercept known
            Debug.Assert(Slope != null);
            Debug.Assert(Intercept != null);


            double dSlope;
            double dIntercept;
            bool   cond1 = LogicSharp.IsDouble(Slope, out dSlope);
            bool   cond2 = LogicSharp.IsDouble(Intercept, out dIntercept);

            if (cond1 && cond2)
            {
                if (dSlope > 0.0)
                {
                    A = Slope;
                    B = -1;
                    C = Intercept;
                }
                else
                {
                    A = -1 * dSlope;
                    B = 1;
                    C = -1 * dIntercept;
                }
            }
        }
示例#2
0
        public Line(string label, object slope, object intercept)
            : base(ShapeType.Line, label)
        {
            InputType = LineType.SlopeIntercept;

            _slope     = slope;
            _intercept = intercept;

            double d;

            if (LogicSharp.IsDouble(_slope, out d))
            {
                _slope = Math.Round(d, 2);
            }
            if (LogicSharp.IsDouble(_intercept, out d))
            {
                _intercept = Math.Round(d, 2);
            }

            if (_slope is string)
            {
                _slope = new Var(_slope);
            }
            if (_intercept is string)
            {
                _intercept = new Var(_intercept);
            }
            if (_intercept == null)
            {
                _intercept = 0.0d;
            }

            Calc_SlopeIntercept_General();
            PropertyChanged += Line_PropertyChanged;
        }
示例#3
0
        public void TestLogicAll()
        {
/*            x = var('x')
 *          assert results(lall((eq, x, 2))) == ({x: 2},)
 *          assert results(lall((eq, x, 2), (eq, x, 3))) == ()            */

            var x     = new Var('x');
            var goal1 = new EqGoal(x, 2);
            var goal2 = new EqGoal(x, 3);

            var lst = new List <Goal>();

            lst.Add(goal1);
            lst.Add(goal2);
            var    dict   = new Dictionary <object, object>();
            object result = LogicSharp.logic_All(lst, dict);

            Assert.Null(result);

            /*assert results(lall((eq, x, 2), (eq, y, 3))) == ({x:2, y:3}) */
            var y     = new Var('y');
            var goal3 = new EqGoal(y, 4);

            lst = new List <Goal>();
            lst.Add(goal1);
            lst.Add(goal3);
            dict   = new Dictionary <object, object>();
            result = LogicSharp.logic_All(lst, dict);
            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Dictionary <object, object>), result);
            var resultDict = result as Dictionary <object, object>;

            Assert.IsTrue(resultDict.Count == 2);
        }
        /// <summary>
        /// Goal must be slope or intercept
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="goal"></param>
        /// <returns></returns>
        public static LineSymbol GenerateLine(Point pt, double?slope, double?intercept)
        {
            Debug.Assert(pt.Concrete);
            double X, Y;

            LogicSharp.IsDouble(pt.XCoordinate, out X);
            LogicSharp.IsDouble(pt.YCoordinate, out Y);

            if (slope != null)
            {
                double intercept1 = Y - slope.Value * X;
                var    line       = new Line(slope, intercept1);
                line.InputType = LineType.Relation;
                return(new LineSymbol(line));
            }
            if (intercept != null)
            {
                double slope1 = 0.0;
                slope1 = (Y - intercept.Value) / X;
                var line = new Line(slope1, intercept);
                line.InputType = LineType.Relation;
                return(new LineSymbol(line));
            }
            throw new Exception("Cannot reach here!");
        }
        private static bool SatisfyGeneralForm(Equation equation,
                                               out Circle circle)
        {
            circle = null;
            var term = equation.Lhs as Term;

            if (term != null && term.Op.Method.Name.Equals("Add"))
            {
                var lst = term.Args as List <object>;
                if (lst != null && lst.Count == 3)
                {
                    bool isNum = LogicSharp.IsNumeric(lst[2]);

                    if (isNum)
                    {
                        double dNum;
                        LogicSharp.IsDouble(lst[2], out dNum);
                        dNum *= -1;
                        object coeffX, coeffY;
                        bool   xTerm1 = IsXSquareTerm(lst[0], out coeffX);
                        bool   yTerm1 = IsYSquareTerm(lst[1], out coeffY);
                        if (xTerm1 && yTerm1)
                        {
                            var pt = new Point(coeffX, coeffY);
                            circle = new Circle(pt, Math.Pow(dNum, 0.5));
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
示例#6
0
        public void TestWalk()
        {
            /*
             * s = {1: 2, 2: 3}
             * assert walk(2, s) == 3
             * assert walk(1, s) == 3
             * assert walk(4, s) == 4
             * Utils.transitive_get()
             */

            var dict = new Dictionary <object, object>();

            dict.Add(1, 2);
            dict.Add(2, 3);

            object obj = LogicSharp.transitive_get(2, dict);

            Assert.True(3.Equals(obj));

            obj = LogicSharp.transitive_get(1, dict);
            Assert.True(3.Equals(obj));

            obj = LogicSharp.transitive_get(4, dict);
            Assert.True(4.Equals(obj));
        }
示例#7
0
        public static Expr ToCoord(string coord)
        {
            int  number;
            bool result = LogicSharp.IsInt(coord, out number);

            if (result)
            {
                if (number < 0)
                {
                    var abs = Math.Abs(number);
                    return(new CompositeExpr(WellKnownSym.minus,
                                             new Expr[] { new IntegerNumber(abs) }));
                }
                return(new IntegerNumber(coord));
            }

            double dNumber;

            result = LogicSharp.IsDouble(coord, out dNumber);

            if (result)
            {
                if (number < 0)
                {
                    var abs = Math.Abs(number);
                    return(new CompositeExpr(WellKnownSym.minus,
                                             new Expr[] { new DoubleNumber(abs) }));
                }
                return(new DoubleNumber(dNumber));
            }



            return(new WordSym(coord));
        }
示例#8
0
        public void TestApproximateEqual()
        {
            double a = 5.81;
            double b = 5.8444;

            bool result = LogicSharp.NumericApproximateEqual(a, b);

            Assert.True(result);
        }
示例#9
0
 public bool AddXCoord(object x)
 {
     if (LogicSharp.IsNumeric(x))
     {
         Properties.Add(XCoordinate, x);
         return(true);
     }
     return(false);
 }
示例#10
0
 public bool AddYCoord(object y)
 {
     if (LogicSharp.IsNumeric(y))
     {
         Properties.Add(YCoordinate, y);
         return(true);
     }
     return(false);
 }
示例#11
0
        public void TestUnify3()
        {
            /*
             * assert unify({1: 2}, {1: 2}, {}) == {}
             * assert unify({1: 2}, {1: 3}, {}) == False
             * assert unify({2: 2}, {1: 2}, {}) == False
             * assert unify({1: var(5)}, {1: 2}, {}) == {var(5): 2}
             * assert unify({1: var(5)}, {2: var(4)}, {}) == {}
             */
            var dict1 = new Dictionary <object, object>();

            dict1.Add(1, 2);
            var dict2 = new Dictionary <object, object>();

            dict2.Add(1, 2);
            var dict = new Dictionary <object, object>();

            bool result = LogicSharp.Unify(dict1, dict2, dict);

            Assert.True(result);

            var dict3 = new Dictionary <object, object>();

            dict3.Add(1, 3);
            dict   = new Dictionary <object, object>();
            result = LogicSharp.Unify(dict1, dict3, dict);
            Assert.False(result);

            var dict4 = new Dictionary <object, object>();

            dict4.Add(2, 2);
            dict   = new Dictionary <object, object>();
            result = LogicSharp.Unify(dict1, dict4, dict);
            Assert.False(result);

            var variable = new Var(5);
            var dict5    = new Dictionary <object, object>();

            dict5.Add(1, variable);
            dict   = new Dictionary <object, object>();
            result = LogicSharp.Unify(dict1, dict5, dict);
            Assert.True(result);
            Assert.True(dict.Count == 1);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(2));

            dict5 = new Dictionary <object, object>();
            dict5.Add(1, variable);
            dict  = new Dictionary <object, object>();
            dict1 = new Dictionary <object, object>();
            var variable2 = new Var(4);

            dict1.Add(2, variable2);
            result = LogicSharp.Unify(dict1, dict5, dict);
            Assert.False(result);
        }
示例#12
0
        public override bool ApproximateMatch(object obj)
        {
            var ps = obj as PointSymbol;
            var ls = obj as LineSegmentSymbol;

            if (ps != null)
            {
                var pShape = Shape as Point;
                if (pShape == null)
                {
                    return(false);
                }
                var pShape1 = ps.Shape as Point;
                if (pShape1 == null)
                {
                    return(false);
                }

                bool cond1 = LogicSharp.NumericEqual(pShape.XCoordinate, pShape1.XCoordinate);
                bool cond2 = LogicSharp.NumericEqual(pShape.YCoordinate, pShape1.YCoordinate);
                if (cond1 && cond2)
                {
                    return(true);
                }

                if (CachedSymbols.Count == 0)
                {
                    return(false);
                }

                foreach (var temp in CachedSymbols)
                {
                    var cachedPt = temp as PointSymbol;
                    Debug.Assert(cachedPt != null);
                    bool inResult = cachedPt.ApproximateMatch(obj);
                    if (inResult)
                    {
                        return(true);
                    }
                }
            }

            if (ls != null)
            {
                var lineSeg = ls.Shape as LineSegment;
                Debug.Assert(lineSeg != null);
                bool cond1 = ApproximateMatch(new PointSymbol(lineSeg.Pt1));
                bool cond2 = ApproximateMatch(new PointSymbol(lineSeg.Pt2));
                if (cond1 || cond2)
                {
                    return(true);
                }
            }

            return(false);
        }
示例#13
0
        public void TestRun()
        {
            var x    = new Var('x');
            var goal = new EqGoal(x, 3);

            object result = LogicSharp.Run(x, goal);

            Assert.NotNull(result);
            Assert.True(result.Equals(3));
        }
        private static bool SatisfySpecialForm(Equation equation, out Circle circle)
        {
            circle = null;

            bool isRhsNum = LogicSharp.IsNumeric(equation.Rhs);

            if (!isRhsNum)
            {
                return(false);
            }
            double dNum;

            LogicSharp.IsDouble(equation.Rhs, out dNum);
            var term = equation.Lhs as Term;

            if (term == null)
            {
                return(false);
            }
            if (!term.Op.Method.Name.Equals("Add"))
            {
                return(false);
            }
            var lst = term.Args as List <object>;

            if (lst == null || lst.Count != 2)
            {
                return(false);
            }

            object coeffX, coeffY;
            bool   xTerm = IsXSquareTerm(lst[0], out coeffX);
            bool   yTerm = IsYSquareTerm(lst[1], out coeffY);

            if (xTerm && yTerm)
            {
                var pt = new Point(coeffX, coeffY);
                circle = new Circle(pt, Math.Pow(dNum, 0.5));
                return(true);
            }
            xTerm = false;
            yTerm = false;
            xTerm = IsXSquareTerm(lst[1], out coeffX);
            yTerm = IsYSquareTerm(lst[0], out coeffY);
            if (xTerm && yTerm)
            {
                var pt = new Point(coeffX, coeffY);
                circle = new Circle(pt, Math.Pow(dNum, 0.5));
                return(true);
            }
            return(false);
        }
示例#15
0
        public override string ToString()
        {
            var circle = Shape as Circle;

            Debug.Assert(circle != null);

            var builder = new StringBuilder();

            builder.Append("(x");

            double dNum;
            bool   isDouble = LogicSharp.IsDouble(circle.CenterPt.XCoordinate, out dNum);

            if (isDouble)
            {
                if (dNum > 0)
                {
                    builder.Append("-").Append(dNum);
                }
                else
                {
                    builder.Append("+").Append(Math.Abs(dNum));
                }
            }
            else
            {
                builder.Append("-").Append(circle.CenterPt.XCoordinate);
            }
            builder.Append(")^2+");

            builder.Append("(y");
            isDouble = LogicSharp.IsDouble(circle.CenterPt.YCoordinate, out dNum);
            if (isDouble)
            {
                if (dNum > 0)
                {
                    builder.Append("-").Append(dNum);
                }
                else
                {
                    builder.Append("+").Append(Math.Abs(dNum));
                }
            }
            else
            {
                builder.Append("-").Append(circle.CenterPt.YCoordinate);
            }
            builder.Append(")^2");
            builder.Append("=").Append(circle.Radius).Append("^2");

            return(builder.ToString());
        }
示例#16
0
        public void TestUnify()
        {
            /*
             *          assert unify(1, 1, {}) == {}
             *          assert unify(1, 2, {}) == False
             *          assert unify(var(1), 2, {}) == {var(1): 2}
             *          assert unify(2, var(1), {}) == {var(1): 2}
             *          assert unify(2, var(1), {var(1):3}) = {}
             *          assert unify(3, var(2), {var(1):3}) = {}
             */
            var  dict   = new Dictionary <object, object>();
            bool result = LogicSharp.Unify(1, 1, dict);

            Assert.True(result);
            dict   = new Dictionary <object, object>();
            result = LogicSharp.Unify(1, 2, dict);
            Assert.False(result);

            dict = new Dictionary <object, object>();
            var variable = new Var(1);

            result = LogicSharp.Unify(variable, 2, dict);
            Assert.True(result);
            Assert.True(dict.Count == 1);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(2));

            dict   = new Dictionary <object, object>();
            result = LogicSharp.Unify(2, variable, dict);
            Assert.True(result);
            Assert.True(dict.Count == 1);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(2));

            dict = new Dictionary <object, object>();
            dict.Add(variable, 3);
            result = LogicSharp.Unify(2, variable, dict);
            Assert.False(result);

            var variable2 = new Var(2);

            dict = new Dictionary <object, object>();
            dict.Add(variable, 3);
            result = LogicSharp.Unify(3, variable2, dict);
            Assert.True(result);
        }
示例#17
0
        public Line(string label, object a, object b, object c)
            : base(ShapeType.Line, label)
        {
            InputType = LineType.GeneralForm;
            _a        = a;
            _b        = b;
            _c        = c;

            double d;

            if (LogicSharp.IsDouble(a, out d))
            {
                _a = Math.Round(d, 1);
            }

            if (LogicSharp.IsDouble(b, out d))
            {
                _b = Math.Round(d, 1);
            }

            if (LogicSharp.IsDouble(c, out d))
            {
                _c = Math.Round(d, 1);
            }

            if (_a is string)
            {
                _a = new Var(_a);
            }
            if (_b is string)
            {
                _b = new Var(_b);
            }
            if (_c is string)
            {
                _c = new Var(_c);
            }

            if (_c == null)
            {
                _c = 0.0d;
            }

            Calc_General_SlopeIntercept();
            PropertyChanged += Line_PropertyChanged;
        }
示例#18
0
        public void TestReify_List()
        {
            /*          x, y = var(), var()
             *          s = {x: 2, y: 4}
             *          e = [1, [x, 3], y]
             *          assert reify(e, s) == [1, [2, 3], 4]   */

            var x    = new Var();
            var y    = new Var();
            var dict = new Dictionary <object, object>();

            dict.Add(x, 2);
            dict.Add(y, 4);

            var lst = new List <object>();

            lst.Add(1);
            lst.Add(new List <object>()
            {
                x, 3
            });
            lst.Add(y);

            var obj = LogicSharp.Reify(lst, dict) as List <object>;

            Assert.IsNotNull(obj);

            var mockList = new List <object>()
            {
                1,
                new List <object>()
                {
                    2, 3
                },
                4
            };
            var test1 = obj[1] as List <object>;

            Assert.IsNotNull(test1);
            Assert.True(test1[0].Equals(2));
            var test2 = obj[2];

            Assert.IsNotNull(test2);
            Assert.True(test2.Equals(4));
        }
示例#19
0
        public void TestLogicAny()
        {
            var x     = new Var('x');
            var goal1 = new EqGoal(x, 2);

            var lst = new List <Goal>();

            lst.Add(goal1);
            var    dict   = new Dictionary <object, object>();
            object result = LogicSharp.logic_Any(lst, dict);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(IEnumerable <KeyValuePair <object, object> >), result);
            Assert.IsInstanceOf(typeof(Dictionary <object, object>), result);
            var resultDict = result as Dictionary <object, object>;

            Assert.IsTrue(resultDict.Count == 1);
            Assert.True(dict.ContainsKey(x));

            //assert len(tuple(lany(eq(x, 2), eq(x, 3))({}))) == 2
            //assert len(tuple(lany((eq, x, 2), (eq, x, 3))({}))) == 2

            var goal2 = new EqGoal(x, 3);

            lst = new List <Goal>();
            lst.Add(goal1);
            lst.Add(goal2);
            dict   = new Dictionary <object, object>();
            result = LogicSharp.logic_Any(lst, dict);
            Assert.IsInstanceOf(typeof(IEnumerable <KeyValuePair <object, object> >), result);
            Assert.IsInstanceOf(typeof(HashSet <KeyValuePair <object, object> >), result);
            var myHashSet = result as HashSet <KeyValuePair <object, object> >;

            Assert.NotNull(myHashSet);
            Assert.True(myHashSet.Count == 2);

            //assert len(tuple(lany(eq(x, 2), eq(x, 3))({x:2}))) == 1
            lst = new List <Goal>();
            lst.Add(goal1);
            lst.Add(goal2);
            dict = new Dictionary <object, object>();
            dict.Add(x, 2);
            result = LogicSharp.logic_Any(lst, dict);
            Assert.IsNull(result);
        }
示例#20
0
        public void UnifyTest()
        {
            var term1 = new Term(Expression.Add, new List <object>()
            {
                1, 1
            });
            var x     = new Var('x');
            var term2 = new Term(Expression.Add, new List <object>()
            {
                1, x
            });

            var  dict   = new Dictionary <object, object>();
            bool result = LogicSharp.Unify(term1, term2, dict);

            Assert.True(result);
            Assert.True(dict.Count == 1);
        }
示例#21
0
        public Point(string label, object xcoordinate, object ycoordinate)
            : base(ShapeType.Point, label)
        {
            _xCoord = xcoordinate;
            _yCoord = ycoordinate;

            double d;

            if (LogicSharp.IsDouble(xcoordinate, out d))
            {
                _xCoord = Math.Round(d, 1);
            }

            if (LogicSharp.IsDouble(ycoordinate, out d))
            {
                _yCoord = Math.Round(d, 1);
            }
        }
示例#22
0
        public bool Reify(EqGoal goal)
        {
            var line = this.Shape as Line;

            Debug.Assert(line != null);
            EqGoal tempGoal = goal;
            bool   cond1    = Var.IsVar(tempGoal.Lhs) && LogicSharp.IsNumeric(tempGoal.Rhs);
            bool   cond2    = Var.IsVar(tempGoal.Rhs) && LogicSharp.IsNumeric(tempGoal.Lhs);

            Debug.Assert(cond1 || cond2);

            if (line.Concrete)
            {
                return(false);
            }

            object aResult = EvalGoal(line.A, tempGoal);
            object bResult = EvalGoal(line.B, tempGoal);
            object cResult = EvalGoal(line.C, tempGoal);

            //Atomic operation
            if (aResult != null && !line.A.Equals(aResult))
            {
                CacheA(aResult, goal);
                RaiseReify(null);
                return(true);
            }

            if (bResult != null && !line.B.Equals(bResult))
            {
                CacheB(bResult, goal);
                RaiseReify(null);
                return(true);
            }

            if (cResult != null && !line.C.Equals(cResult))
            {
                CacheC(cResult, goal);
                RaiseReify(null);
                return(true);
            }

            return(false);
        }
示例#23
0
        public void TestUnify2()
        {
            //            assert unify((1, 2), (1, 2), {}) == {}
            //            assert unify([1, 2], [1, 2], {}) == {}
            //            assert unify((1, 2), (1, 2, 3), {}) == False
            //            assert unify((1, var(1)), (1, 2), {}) == {var(1): 2}
            //            assert unify((1, var(1)), (1, 2), {var(1): 3}) == False
            var  tuple1 = new Tuple <object, object>(1, 2);
            var  tuple2 = new Tuple <object, object>(1, 2);
            var  dict   = new Dictionary <object, object>();
            bool result = LogicSharp.Unify(tuple1, tuple2, dict);

            Assert.True(dict.Count == 0);
            Assert.True(result);

            var lst1 = new List <object>()
            {
                1, 2
            };
            var lst2 = new List <object>()
            {
                1, 2
            };

            dict   = new Dictionary <object, object>();
            result = LogicSharp.Unify(lst1, lst2, dict);
            Assert.True(dict.Count == 0);
            Assert.True(result);

            var variable = new Var(1);
            var tuple3   = new Tuple <object, object>(1, variable);

            dict   = new Dictionary <object, object>();
            result = LogicSharp.Unify(tuple1, tuple3, dict);
            Assert.True(dict.Count == 1);
            Assert.True(result);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(2));

            dict = new Dictionary <object, object>();
            dict.Add(variable, 3);
            result = LogicSharp.Unify(tuple1, tuple3, dict);
            Assert.False(result);
        }
示例#24
0
        public void TestLogicConde()
        {
            /*
             * x = var('x')
             * assert results(conde([eq(x, 2)], [eq(x, 3)])) == ({x: 2}, {x: 3})
             * assert results(conde([eq(x, 2), eq(x, 3)])) == ()
             */
            var x     = new Var('x');
            var goal1 = new EqGoal(x, 2);
            var goal2 = new EqGoal(x, 3);
            var lst   = new List <Goal>();

            lst.Add(goal1);
            var lst2 = new List <Goal>();

            lst2.Add(goal2);
            var lslst = new List <List <Goal> >();

            lslst.Add(lst);
            lslst.Add(lst2);

            var    dict   = new Dictionary <object, object>();
            object result = LogicSharp.logic_Conde(lslst, dict);

            Assert.IsInstanceOf(typeof(IEnumerable <KeyValuePair <object, object> >), result);
            Assert.IsInstanceOf(typeof(HashSet <KeyValuePair <object, object> >), result);
            var myHashSet = result as HashSet <KeyValuePair <object, object> >;

            Assert.NotNull(myHashSet);
            Assert.True(myHashSet.Count == 2);

            lst = new List <Goal>();
            lst.Add(goal1);
            lst.Add(goal2);
            lslst = new List <List <Goal> >();
            lslst.Add(lst);
            dict   = new Dictionary <object, object>();
            result = LogicSharp.logic_Conde(lslst, dict);
            Assert.IsInstanceOf(typeof(IEnumerable <KeyValuePair <object, object> >), result);
            Assert.IsInstanceOf(typeof(HashSet <KeyValuePair <object, object> >), result);
            myHashSet = result as HashSet <KeyValuePair <object, object> >;
            Assert.IsEmpty(myHashSet);
        }
示例#25
0
        public void TestDeepWalk()
        {
            var dict = new Dictionary <object, object>();
            var z    = new Var('z');
            var y    = new Var('y');
            var x    = new Var('x');

            dict.Add(z, 6);
            dict.Add(y, 5);
            var tuple = new Tuple <object, object>(y, z);

            dict.Add(x, tuple);

            object obj = LogicSharp.transitive_get(x, dict);

            Assert.True(obj.Equals(tuple));

            obj = LogicSharp.deep_transitive_get(x, dict);
            Assert.IsInstanceOf(typeof(Tuple <object, object>), obj);
            var result = obj as Tuple <object, object>;

            Assert.IsNotNull(result);
            Assert.True(5.Equals(result.Item1));
            Assert.True(6.Equals(result.Item2));
            //    Transitive get that propagates within tuples
            //    >>> d = {1: (2, 3), 2: 12, 3: 13}
            //    >>> transitive_get(1, d)
            //    (2, 3)
            //    >>> deep_transitive_get(1, d)
            //    (12, 13)

            dict  = new Dictionary <object, object>();
            tuple = new Tuple <object, object>(2, 3);
            dict.Add(1, tuple);
            dict.Add(2, 12);
            dict.Add(3, 13);
            obj = LogicSharp.deep_transitive_get(1, dict);
            Assert.IsInstanceOf(typeof(Tuple <object, object>), obj);
            result = obj as Tuple <object, object>;
            Assert.IsNotNull(result);
            Assert.True(12.Equals(result.Item1));
            Assert.True(13.Equals(result.Item2));
        }
示例#26
0
        public void TestReify()
        {
            var x    = new Var();
            var y    = new Var();
            var z    = new Var();
            var dict = new Dictionary <object, object>();

            dict.Add(x, 1);
            dict.Add(y, 2);
            dict.Add(z, new Tuple <object, object>(x, y));

            object result = LogicSharp.Reify(x, dict);

            Assert.True(result.Equals(1));
            Assert.True(LogicSharp.Reify(10, dict).Equals(10));

            var t = new Var('t');

            result = LogicSharp.Reify(t, dict);
            Assert.True(t.Equals(result));

            var tuple = new Tuple <object, object>(1, y);

            Assert.True(LogicSharp.Reify(tuple, dict)
                        .Equals(new Tuple <object, object>(1, 2)));

            // assert reify((1, (x, (y, 2))), s) == (1, (1, (2, 2)))

            var tuple1 = new Tuple <object, object>(y, 2);
            var tuple2 = new Tuple <object, object>(x, tuple1);
            var tuple3 = new Tuple <object, object>(1, tuple2);

            var obj = LogicSharp.Reify(tuple3, dict);

            var tuple10 = new Tuple <object, object>(2, 2);
            var tuple20 = new Tuple <object, object>(1, tuple10);
            var tuple30 = new Tuple <object, object>(1, tuple20);

            Assert.True(obj.Equals(tuple30));

            // assert reify(z, s) == (1, 2)
            Assert.True(LogicSharp.Reify(z, dict).Equals(new Tuple <object, object>(1, 2)));
        }
示例#27
0
        public void TestUnifyComplex()
        {
            //    assert unify((1, {2: 3}), (1, {2: 3}), {}) == {}
            //    assert unify((1, {2: 3}), (1, {2: 4}), {}) == False
            //    assert unify((1, {2: var(5)}), (1, {2: 4}), {}) == {var(5): 4}
            //
            //    assert unify({1: (2, 3)}, {1: (2, var(5))}, {}) == {var(5): 3}
            //    assert unify({1: [2, 3]}, {1: [2, var(5)]}, {}) == {var(5): 3}

            var dict = new Dictionary <object, object>();

            var dict1 = new Dictionary <object, object>();

            dict1.Add(2, 3);

            var tuple1 = new Tuple <object, object>(1, dict1);
            var tuple2 = new Tuple <object, object>(1, dict1);

            bool result = LogicSharp.Unify(tuple1, tuple2, dict);

            Assert.True(result);
            Assert.True(dict.Count == 0);

            var dict2 = new Dictionary <object, object>();

            dict2.Add(2, 4);
            var tuple3 = new Tuple <object, object>(1, dict2);

            result = LogicSharp.Unify(tuple1, tuple3, dict);
            Assert.False(result);

            var variable = new Var(5);
            var dict3    = new Dictionary <object, object>();

            dict3.Add(2, variable);
            var tuple4 = new Tuple <object, object>(1, dict3);

            result = LogicSharp.Unify(tuple3, tuple4, dict);
            Assert.True(result);
            Assert.True(dict.Count == 1);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(4));
        }
        private static bool IsYWithConstTerm(object obj, out object coeff)
        {
            coeff = null;
            var term = obj as Term;

            if (term == null)
            {
                return(false);
            }

            if (term.Op.Method.Name.Equals("Add") ||
                term.Op.Method.Name.Equals("Substract"))
            {
                var lst = term.Args as List <object>;
                if (lst == null)
                {
                    return(false);
                }
                bool isYTerm = IsYTerm(lst[0], out coeff);
                if (coeff == null || !coeff.ToString().Equals("1"))
                {
                    return(false);
                }
                if (!isYTerm)
                {
                    return(false);
                }
                bool isRhsNum = LogicSharp.IsNumeric(lst[1]);
                if (!isRhsNum)
                {
                    return(false);
                }
                double dNum;
                LogicSharp.IsDouble(lst[1], out dNum);
                if (term.Op.Method.Name.Equals("Add"))
                {
                    dNum *= -1;
                }
                coeff = dNum;
                return(true);
            }
            return(false);
        }
示例#29
0
        public void test_unify_dyna_object()
        {
            /*
             *          assert unify_object(Foo(1, 2), Foo(1, 2), {}) == {}
             *          assert unify_object(Foo(1, 2), Foo(1, 3), {}) == False
             *          assert unify_object(Foo(1, 2), Foo(1, var(3)), {}) == {var(3): 2}
             */
            dynamic foo = new DyLogicObject();

            foo.a = 1;
            foo.b = 2;

            dynamic foo2 = new DyLogicObject();

            foo2.a = 1;
            foo2.b = 2;

            var  dict   = new Dictionary <object, object>();
            bool result = LogicSharp.Unify_Object(foo, foo2, dict);

            Assert.True(result);
            Assert.True(dict.Count == 0);

            dynamic foo3 = new DyLogicObject();

            foo3.a = 1;
            foo3.b = 3;
            result = LogicSharp.Unify_Object(foo, foo3, dict);
            Assert.False(result);

            dynamic foo4 = new DyLogicObject();

            foo4.a = 1;
            var variable = new Var(3);

            foo4.b = variable;
            result = LogicSharp.Unify_Object(foo, foo4, dict);
            Assert.True(result);
            Assert.True(dict.Count == 1);
            Assert.True(dict.ContainsKey(variable));
            Assert.True(dict[variable].Equals(2));
        }
        public static PointSymbol GenerateMidPoint(Point pt1, Point pt2)
        {
            if (pt1.Equals(pt2))
            {
                return(null);
            }
            Debug.Assert(pt1.Concrete);
            Debug.Assert(pt2.Concrete);
            double p1x, p1y, p2x, p2y;

            LogicSharp.IsDouble(pt1.XCoordinate, out p1x);
            LogicSharp.IsDouble(pt1.YCoordinate, out p1y);
            LogicSharp.IsDouble(pt2.XCoordinate, out p2x);
            LogicSharp.IsDouble(pt2.YCoordinate, out p2y);
            var midX     = (p1x + p2x) / 2;
            var midY     = (p1y + p2y) / 2;
            var midPoint = new Point(midX, midY);

            return(new PointSymbol(midPoint));
        }