Beispiel #1
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));
        }
Beispiel #2
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)));
        }
Beispiel #3
0
        public void TestReify_Dict()
        {
            var x = new Var();
            var y = new Var();

            /*
             *          s = {x: 2, y: 4}
             *          e = {1: x, 3: {5: y}}
             *          assert reify(e, s) == {1: 2, 3: {5: 4}}
             */
            var dict = new Dictionary <object, object>();

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

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

            testDict.Add(1, x);
            var embedDict = new Dictionary <object, object>();

            embedDict.Add(5, y);
            testDict.Add(3, embedDict);

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

            mockDict.Add(1, 2);
            var mockEmbedDict = new Dictionary <object, object>();

            mockEmbedDict.Add(5, 4);
            mockDict.Add(3, mockEmbedDict);

            var obj   = LogicSharp.Reify(testDict, dict) as Dictionary <object, object>;
            var test1 = obj[1];

            Assert.True(test1.Equals(mockDict[1]));

            var test2     = obj[3] as Dictionary <object, object>;
            var mocktest2 = mockDict[3] as Dictionary <object, object>;

            Assert.True(test2[5].Equals(mocktest2[5]));
        }
Beispiel #4
0
        public void TestReify_Complex()
        {
            /*
             * x, y = var(), var()
             * s = {x: 2, y: 4}
             * e = {1: [x], 3: (y, 5)}
             *
             * assert reify(e, s) == {1: [2], 3: (4, 5)}
             */
            var x    = new Var();
            var y    = new Var();
            var dict = new Dictionary <object, object>();

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

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

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

            var obj = LogicSharp.Reify(input, dict) as Dictionary <object, object>;

            Assert.IsNotNull(obj);
            var test1 = obj[1] as List <object>;

            Assert.IsNotNull(test1);
            Assert.True(2.Equals(test1[0]));

            var test2 = obj[3] as Tuple <object, object>;

            Assert.IsNotNull(test2);
            Assert.True(4.Equals(test2.Item1));
        }
        public void GenerateYCacheSymbol(object obj, EqGoal goal)
        {
            var point = Shape as Point;

            Debug.Assert(point != null);
            CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
            if (CachedSymbols.Count == 0)
            {
                var gPoint       = new Point(point.Label, point.XCoordinate, obj);
                var gPointSymbol = new PointSymbol(gPoint);
                gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
                CachedSymbols.Add(gPointSymbol);
                gPointSymbol.Traces.AddRange(goal.Traces);

                //transform goal trace
                for (int i = goal.Traces.Count - 1; i >= 0; i--)
                {
                    gPoint.Traces.Insert(0, goal.Traces[i]);
                }
                //Substitution trace
                var rule        = SubstitutionRule.ApplySubstitute();
                var appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                var ts          = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                gPointSymbol._innerLoop.Add(ts);


                string strategy = "Reify a Point's y-coordinate by substituing a given fact.";

                gPointSymbol.GenerateATrace(strategy);
                //gPoint.Traces.Insert(0, ts);
            }
            else
            {
                foreach (ShapeSymbol ss in CachedSymbols.ToList())
                {
                    var pt = ss.Shape as Point;
                    if (pt != null)
                    {
                        var yResult = LogicSharp.Reify(pt.YCoordinate, goal.ToDict());
                        if (!pt.YCoordinate.Equals(yResult))
                        {
                            var gPt          = new Point(pt.Label, pt.XCoordinate, pt.YCoordinate);
                            var gPointSymbol = new PointSymbol(gPt);
                            //substitute
                            pt.YCoordinate = yResult;
                            ss.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
                            gPointSymbol.Traces.AddRange(goal.Traces);
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                pt.Traces.Insert(0, goal.Traces[i]);
                            }
                            string rule        = SubstitutionRule.ApplySubstitute();
                            string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                            var    ts          = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);
                            gPointSymbol._innerLoop.Add(ts);
                            string strategy = "Reify a Point's y-coordinate by substituing a given fact.";
                            gPointSymbol.GenerateATrace(strategy);
                            //pt.Traces.Insert(0, ts);
                        }
                        else
                        {
                            //generate
                            var gPoint       = new Point(pt.Label, pt.XCoordinate, obj);
                            var gPointSymbol = new PointSymbol(gPoint);
                            gPointSymbol.Traces.AddRange(goal.Traces);
                            gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.Y, goal));
                            foreach (KeyValuePair <object, EqGoal> pair in ss.CachedGoals)
                            {
                                if (pair.Key.Equals(PointAcronym.X))
                                {
                                    gPointSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(PointAcronym.X, pair.Value));
                                }
                            }
                            CachedSymbols.Add(gPointSymbol);
                            //substitute
                            //Add traces from pt to gPoint
                            for (int i = pt.Traces.Count - 1; i >= 0; i--)
                            {
                                gPoint.Traces.Insert(0, pt.Traces[i]);
                            }
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                gPoint.Traces.Insert(0, goal.Traces[i]);
                            }
                            var rule        = SubstitutionRule.ApplySubstitute();
                            var appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                            var ts          = new TraceStep(this, gPointSymbol, SubstitutionRule.SubstituteKC(), rule, appliedRule);

                            gPointSymbol._innerLoop.Add(ts);
                            string strategy = "Reify a Point's y-coordinate by substituing a given fact.";
                            gPointSymbol.GenerateATrace(strategy);
                            //gPoint.Traces.Insert(0, ts);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        public void CacheC(object obj, EqGoal goal)
        {
            CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal));

            if (CachedSymbols.Count == 0)
            {
                var line = Shape as Line;
                Debug.Assert(line != null);

                #region generate new object

                var gLine       = new Line(line.Label, line.A, line.B, obj);
                var gLineSymbol = new LineSymbol(gLine);
                gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal));
                CachedSymbols.Add(gLineSymbol);

                //Transform goal trace
                for (int i = goal.Traces.Count - 1; i >= 0; i--)
                {
                    gLine.Traces.Insert(0, goal.Traces[i]);
                }

                //Substitution trace
                string rule        = SubstitutionRule.ApplySubstitute();
                string appliedRule = SubstitutionRule.ApplySubstitute(this, goal);
                string kc          = SubstitutionRule.SubstituteKC();

                var ts = new TraceStep(this, gLineSymbol, kc, rule, appliedRule);
                gLine._innerLoop.Add(ts);
                gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy);
                //gLine.Traces.Insert(0, ts);
                #endregion
            }
            else
            {
                #region Iterate existing point object

                foreach (ShapeSymbol ss in CachedSymbols.ToList())
                {
                    var line = ss.Shape as Line;
                    if (line != null)
                    {
                        var cResult = LogicSharp.Reify(line.C, goal.ToDict());
                        if (!line.C.Equals(cResult))
                        {
                            var gline       = new Line(line.Label, line.A, line.B, line.C);
                            var gLineSymbol = new LineSymbol(gline);
                            //substitute
                            line.C = cResult;
                            ss.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal));

                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                line.Traces.Insert(0, goal.Traces[i]);
                            }

                            string rule        = SubstitutionRule.ApplySubstitute();
                            string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal);
                            string kc          = SubstitutionRule.SubstituteKC();

                            var ts = new TraceStep(gLineSymbol, ss, kc, rule, appliedRule);
                            //line.Traces.Insert(0, ts);
                            line._innerLoop.Add(ts);
                            line.GenerateATrace(SubstitutionRule.SubstitutionStrategy);
                        }
                        else
                        {
                            //generate
                            var gLine       = new Line(line.Label, line.A, line.B, obj);
                            var gLineSymbol = new LineSymbol(gLine);
                            gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.C, goal));
                            foreach (KeyValuePair <object, EqGoal> pair in ss.CachedGoals)
                            {
                                if (pair.Key.Equals(LineAcronym.A))
                                {
                                    gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.A, pair.Value));
                                }
                                else if (pair.Key.Equals(LineAcronym.B))
                                {
                                    gLineSymbol.CachedGoals.Add(new KeyValuePair <object, EqGoal>(LineAcronym.B, pair.Value));
                                }
                            }
                            CachedSymbols.Add(gLineSymbol);

                            //substitute
                            //Add traces from line to gLine
                            for (int i = line.Traces.Count - 1; i >= 0; i--)
                            {
                                gLine.Traces.Insert(0, line.Traces[i]);
                            }
                            //transform goal trace
                            for (int i = goal.Traces.Count - 1; i >= 0; i--)
                            {
                                gLine.Traces.Insert(0, goal.Traces[i]);
                            }
                            string rule        = SubstitutionRule.ApplySubstitute();
                            string appliedRule = SubstitutionRule.ApplySubstitute(gLineSymbol, goal);
                            string kc          = SubstitutionRule.SubstituteKC();

                            var ts = new TraceStep(ss, gLineSymbol, kc, rule, appliedRule);
                            gLine._innerLoop.Add(ts);
                            gLine.GenerateATrace(SubstitutionRule.SubstitutionStrategy);
                            //gLine.Traces.Insert(0, ts);
                        }
                    }
                }
                #endregion
            }
        }