Example #1
1
 protected OperatorGraph(OperatorGraph original, Cloner cloner)
   : base(original, cloner) {
   operators = cloner.Clone(original.operators);
   initialOperator = cloner.Clone(original.initialOperator);
   visualizationInfo = cloner.Clone(original.visualizationInfo);
   Initialize();
 }
Example #2
0
        public void Translate_Journey_CorrectTranslation()
        {
            const ulong time         = 15761461220ul;
            const ulong previousTime = 15761454920ul;



            var tdb = new TransitDb(0);
            var wr  = tdb.GetWriter();
            var l   = wr.AddOrUpdateStop("Some Station", 4.123, 51.789, null);

            var rootL      = new StopId(1, 140860940, 184354050); // Supposed to be an OSM-location
            var connection = new Connection(new ConnectionId(0, 1),
                                            "testConnection", rootL, l, previousTime, (ushort)(time - previousTime), new TripId(0, 0));

            var connId = wr.AddOrUpdateConnection(connection);

            wr.Close();
            var genesis = new Journey <TransferMetric>(rootL, previousTime, TransferMetric.Factory);
            var j       = genesis.ChainSpecial(connId, previousTime, l, new TripId(3, 3));

            var op        = new Operator("Op", tdb, null, 500, new string[] { }, new string[] { });
            var operators = new OperatorSet(new List <Operator> {
                op
            });
            var translatedSegment =
                operators.TranslateWalkSegment(j, new CoordinatesCache(new DummyOtherMode(), false));

            Assert.Contains("openstreetmap.org", translatedSegment.Departure.Location.Id);
            Assert.DoesNotContain("openstreetmap.org", translatedSegment.Arrival.Location.Id);
        }
Example #3
0
        public static void Main()
        {
            EntitySet objects = new EntitySet();

            objects.addObject("Ax", "Cargo");
            objects.addObject("Bx", "Cargo");
            objects.addObject("Rx", "Rocket");
            objects.addObject("Lx", "Place");
            objects.addObject("Px", "Place");

            Conjunction init = new Conjunction("At (Ax, Lx) & At (Bx, Lx) & At (Rx, Lx) & HasFuel (Rx)");
            Conjunction goal = new Conjunction("At (Ax, Px) & At (Bx, Px)");

            OperatorHead opMoveHead = new OperatorHead("move", new ParaList("Rocket ?r, Place ?from, Place ?to"));
            Condition    opMoveCond = new Condition("?from != ?to");
            Conjunction  opMovePre  = new Conjunction("At (?r, ?from) & HasFuel (?r)");
            Conjunction  opMoveAdd  = new Conjunction("At (?r, ?to)");
            Conjunction  opMoveDel  = new Conjunction("At (?r, ?from) & HasFuel (?r)");
            Operator     opMove     = new Operator(opMoveHead, opMoveCond, opMovePre, opMoveAdd, opMoveDel);

            OperatorHead opUnloadHead = new OperatorHead("unload", new ParaList("Rocket ?r, Place ?p, Cargo ?c"));
            Condition    opUnloadCond = new Condition("");
            Conjunction  opUnloadPre  = new Conjunction("At (?r, ?p) & In (?c, ?r)");
            Conjunction  opUnloadAdd  = new Conjunction("At (?c, ?p)");
            Conjunction  opUnloadDel  = new Conjunction("In (?c, ?r)");
            Operator     opUnload     = new Operator(opUnloadHead, opUnloadCond, opUnloadPre, opUnloadAdd, opUnloadDel);

            OperatorHead opLoadHead = new OperatorHead("load", new ParaList("Rocket ?r, Place ?p, Cargo ?c"));
            Condition    opLoadCond = new Condition("");
            Conjunction  opLoadPre  = new Conjunction("At (?r, ?p) & At (?c, ?p)");
            Conjunction  opLoadAdd  = new Conjunction("In (?c, ?r)");
            Conjunction  opLoadDel  = new Conjunction("At (?c, ?p)");
            Operator     opLoad     = new Operator(opLoadHead, opLoadCond, opLoadPre, opLoadAdd, opLoadDel);


            OperatorSet opSet = new OperatorSet();

            opSet.Operators.Add(opMove);
            opSet.Operators.Add(opUnload);
            opSet.Operators.Add(opLoad);

            GraphPlan graph = new GraphPlan(objects, init, goal, opSet);
            bool      res   = graph.CreateGraph();

            //Assert.IsTrue(res);

            res = graph.SearchGoal();
            //Assert.IsTrue(res);

            List <string> plan = graph.GetPlan();

            foreach (string step in plan)
            {
                System.Console.WriteLine(step);
            }
        }
 protected virtual OperatorSet InitOperators()
 {
     if ((this.Options & ExpressiveOptions.IgnoreCaseForParsing) > 0)
     {
         return(OperatorSet.CreateDefault(StringComparer.OrdinalIgnoreCase));
     }
     else
     {
         return(OperatorSet.CreateDefault(StringComparer.Ordinal));
     }
 }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Context"/> class with the specified <paramref name="options"/>.
        /// </summary>
        /// <param name="options">The <see cref="ExpressiveOptions"/> to use when evaluating.</param>
        /// <param name="mainCurrentCulture">The <see cref="CultureInfo"/> for use in general parsing/conversions.</param>
        /// <param name="decimalCurrentCulture">The <see cref="CultureInfo"/> for use in decimal parsing/conversions.</param>
        public Context(ExpressiveOptions options, CultureInfo mainCurrentCulture, CultureInfo decimalCurrentCulture, FunctionSet functions, OperatorSet operators)
        {
            Options = options;

            this.CurrentCulture = mainCurrentCulture ?? throw new ArgumentNullException(nameof(mainCurrentCulture));
            // For now we will ignore any specific cultures but keeping it in a single place to simplify changing later if required.
            this.DecimalCurrentCulture = decimalCurrentCulture ?? throw new ArgumentNullException(nameof(decimalCurrentCulture));

            DecimalSeparator         = Convert.ToChar(this.DecimalCurrentCulture.NumberFormat.NumberDecimalSeparator, this.DecimalCurrentCulture);
            this.registeredFunctions = functions ?? throw new ArgumentNullException(nameof(functions));
            this.registeredOperators = operators ?? throw new ArgumentNullException(nameof(operators));
        }
Example #6
0
        public void GraphPlanTest_Robot()
        {
            EntitySet objects = new EntitySet();

            objects.addObject("green", "Color");
            objects.addObject("red", "Color");
            objects.addObject("sec1", "Section");

            Conjunction init = new Conjunction("CurrentColor (green)");
            Conjunction goal = new Conjunction("Painted (sec1, red)");

            OperatorHead opPaintHead = new OperatorHead("Paint", new ParaList("Section ?sec, Color ?clr"));
            Condition    opPaintCond = new Condition("");
            Conjunction  opPaintPre  = new Conjunction("CurrentColor (?clr)");
            Conjunction  opPaintAdd  = new Conjunction("Painted (?sec, ?clr)");
            Conjunction  opPaintDel  = new Conjunction("");
            Operator     opPaint     = new Operator(opPaintHead, opPaintCond, opPaintPre, opPaintAdd, opPaintDel);

            OperatorHead opChangeColorHead = new OperatorHead("ChangeColor", new ParaList("Color ?old, Color ?new"));
            Condition    opChangeColorCond = new Condition("?old != ?new");
            Conjunction  opChangeColorPre  = new Conjunction("CurrentColor (?old)");
            Conjunction  opChangeColorAdd  = new Conjunction("CurrentColor (?new)");
            Conjunction  opChangeColorDel  = new Conjunction("CurrentColor (?old)");
            Operator     opChangeColor     = new Operator(opChangeColorHead, opChangeColorCond, opChangeColorPre, opChangeColorAdd, opChangeColorDel);

            OperatorSet opSet = new OperatorSet();

            opSet.Operators.Add(opPaint);
            opSet.Operators.Add(opChangeColor);

            GraphPlan graph = new GraphPlan(objects, init, goal, opSet);
            bool      res   = graph.CreateGraph();

            Assert.IsTrue(res);

            res = graph.SearchGoal();
            Assert.IsTrue(res);

            List <string> plan = graph.GetPlan();

            //foreach (string step in plan)
            //{
            //    System.Console.WriteLine(step);
            //}

            Assert.AreEqual(2, plan.Count);

            Assert.AreEqual("ChangeColor ( green, red )", plan[0]);
            Assert.AreEqual("Paint ( sec1, red )", plan[1]);
        }
Example #7
0
        public void GraphPlanTest_Blocks()
        {
            EntitySet objects = new EntitySet();

            objects.addObject("b1", "Block");
            objects.addObject("b2", "Block");
            objects.addObject("b3", "Block");
            objects.addObject("Table", "Block");

            Conjunction init = new Conjunction("ON (b1, b2) & ON (b2, Table) & ON (b3, Table) & Clear (b1) & Clear (b3) & Clear (Table)");
            Conjunction goal = new Conjunction("ON (b3, Table) & ON (b2, b3) & ON (b1, b2)");

            OperatorHead opMoveHead = new OperatorHead("Move", new ParaList("Block ?obj, Block ?from, Block ?to"));
            Condition    opMoveCond = new Condition("?obj != ?from & ?obj != ?to & ?from != ?to & ?to != Table");
            Conjunction  opMovePre  = new Conjunction("ON (?obj, ?from) & Clear (?obj) & Clear (?to)");
            Conjunction  opMoveAdd  = new Conjunction("ON (?obj, ?to) & Clear (?from)");
            Conjunction  opMoveDel  = new Conjunction("ON (?obj, ?from) & Clear (?to)");
            Operator     opMove     = new Operator(opMoveHead, opMoveCond, opMovePre, opMoveAdd, opMoveDel);

            OperatorHead opMoveToTableHead = new OperatorHead("MoveToTable", new ParaList("Block ?obj, Block ?from"));
            Condition    opMoveToTableCond = new Condition("?obj != ?from & ?obj != Table & ?from != Table");
            Conjunction  opMoveToTablePre  = new Conjunction("Clear (?obj) & ON (?obj, ?from)");
            Conjunction  opMoveToTableAdd  = new Conjunction("ON (?obj, Table) & Clear (?from)");
            Conjunction  opMoveToTableDel  = new Conjunction("ON (?obj, ?from)");
            Operator     opMoveToTable     = new Operator(opMoveToTableHead, opMoveToTableCond, opMoveToTablePre, opMoveToTableAdd, opMoveToTableDel);

            OperatorSet opSet = new OperatorSet();

            opSet.Operators.Add(opMove);
            opSet.Operators.Add(opMoveToTable);

            GraphPlan graph = new GraphPlan(objects, init, goal, opSet);
            bool      res   = graph.CreateGraph();

            Assert.IsTrue(res);

            res = graph.SearchGoal();
            Assert.IsTrue(res);

            List <string> plan = graph.GetPlan();

            Assert.AreEqual(3, plan.Count);

            Assert.AreEqual("MoveToTable ( b1, b2 )", plan[0]);
            Assert.AreEqual("Move ( b2, Table, b3 )", plan[1]);
            Assert.AreEqual("Move ( b1, Table, b2 )", plan[2]);
        }
        /// <summary>
        /// Intersects two nodes. If
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static SetNode Intersect(SetNode A, SetNode B)
        {
            if (A.Type == SetNode.NodeType.OPERATOR || B.Type == SetNode.NodeType.OPERATOR)
            {
                return(new OperatorSet(OperatorSet.OperatorType.INTERSECTION, A, B));
            }
            var(goodAPieces, badAPieces) = GatherEvaluablePieces(A as Set);
            var(goodBPieces, badBPieces) = GatherEvaluablePieces(B as Set);
            if (goodAPieces.Count * goodBPieces.Count == 0)
            {
                return(new OperatorSet(OperatorSet.OperatorType.INTERSECTION, A, B));
            }
            var newPieces = new List <Piece>();

            foreach (var goodAPiece in goodAPieces)
            {
                foreach (var goodBPiece in goodBPieces)
                {
                    var piece = PieceFunctions.Intersect(goodAPiece, goodBPiece);
                    if (!(piece is null) && PieceFunctions.IsPieceCorrect(piece))
                    {
                        newPieces.Add(piece);
                    }
                }
            }
            var union = UniteList(newPieces);
            var badA  = new Set {
                Pieces = badAPieces
            };
            var badB = new Set {
                Pieces = badBPieces
            };

            if (union.Count == 0)
            {
                if (badA.IsEmpty() || badB.IsEmpty())
                {
                    return(new Set());
                }
                else
                {
                    return(new OperatorSet(OperatorSet.OperatorType.INTERSECTION, badA, badB));
                }
            }
            var united = new Set {
                Pieces = union
            };

            if (badBPieces.Count + badAPieces.Count == 0)
            {
                return(united);
            }

            /*
             * A = A1 or A2 (A1 - good, A2 - bad)
             * B = B1 or B2 (B1 - good, B2 - bad)
             * A & B = (A1 & B1) | (A1 & B2) | (A2 & B1) | (A2 & B2)
             */
            var goodA = new Set {
                Pieces = goodAPieces
            };
            var goodB = new Set {
                Pieces = goodBPieces
            };

            return(OperatorSet.Or(
                       united,                               // A1 & B1
                       OperatorSet.Or(
                           OperatorSet.And(                  // A2 & B2
                               badA,
                               badB
                               ),
                           OperatorSet.Or(
                               OperatorSet.And(badA, goodB), // A2 & B1
                               OperatorSet.And(badB, goodA)  // A1 & B2
                               ))));
        }
Example #9
0
 /// <summary>
 /// Initializes a new instance of <see cref="OperatorGraph"/>.
 /// </summary>
 public OperatorGraph() {
   operators = new OperatorSet();
   initialOperator = null;
   visualizationInfo = null;
   Initialize();
 }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Context"/> class with the specified <paramref name="options"/>.
 /// </summary>
 /// <param name="options">The <see cref="ScryberOptions"/> to use when evaluating.</param>
 /// <param name="functions">All the standard functions</param>
 /// <param name="operators">All The standard Operators</param>
 public Context(ExpressiveOptions options, FunctionSet functions, OperatorSet operators)
     : this(options, CultureInfo.CurrentCulture, CultureInfo.InvariantCulture, functions, operators)
 {
 }