Exemple #1
0
        public override Node VisitStatements(TParser.StatementsContext context)
        {
            var nodes = new NodesCollection <Node>(context.Start);

            foreach (var statement in context.statement())
            {
                nodes.Add(Visit(statement));
            }
            return(nodes);
        }
Exemple #2
0
        public override Node VisitIdentifiers(TParser.IdentifiersContext context)
        {
            var nodes = new NodesCollection <Identifier>(context.Start);

            foreach (var exp in context.Identifier())
            {
                nodes.Add(new Identifier(exp.GetText(), exp.Symbol));
            }
            return(nodes);
        }
Exemple #3
0
        public override Node VisitCodeBlock(TParser.CodeBlockContext context)
        {
            var body = new NodesCollection <Node>(context.Start);

            if (context.expression() != null && !context.expression().IsEmpty)
            {
                body.Add(Visit(context.expression()));
            }
            else if (context.statements() != null && !context.statements().IsEmpty)
            {
                body = VisitStatements(context.statements()).As <NodesCollection <Node> >();
            }
            else if (context.statement() != null && !context.statement().IsEmpty)
            {
                body.Add(Visit(context.statement()));
            }

            return(body);
        }
Exemple #4
0
        public override Node VisitCommaExpressions(TParser.CommaExpressionsContext context)
        {
            var nodes = new NodesCollection <Node>(context.Start);

            foreach (var exp in context.expression())
            {
                nodes.Add(Visit(exp));
            }
            return(nodes);
        }
Exemple #5
0
        private void VisitObjectAccess(TParser.ObjectAccessContext context, NodesCollection <Node> path)
        {
            while (true)
            {
                var id = new Identifier(context.Identifier().GetText(), context.Identifier().Symbol);
                path.Add(id);
                var leftExp = context.expression();
                if (leftExp is TParser.ObjectAccessContext objCtx)
                {
                    context = objCtx;
                    continue;
                }
                else
                {
                    path.Add(Visit(leftExp));
                }

                break;
            }
        }
        /// <summary>
        /// Create random problem with given number of nodes
        /// </summary>
        /// <param name="numberOfNodes">number of nodes in the proble</param>
        /// <param name="width">problem space width</param>
        /// <param name="height">problem space height</param>
        /// <returns>Generated problem</returns>
        public static NodesCollection MakeNodes(int numberOfNodes, int width, int height)
        {
            Random random = new Random((int)DateTime.Now.Ticks);
            NodesCollection nodes = new NodesCollection();
            for (int i = 0; i < numberOfNodes; i++)
            {
                nodes.Add(new Node2D(i + 1, random.NextDouble() * width, random.NextDouble() * height));
            }

            return nodes;
        }
Exemple #7
0
        /// <summary>
        /// Create random problem with given number of nodes
        /// </summary>
        /// <param name="numberOfNodes">number of nodes in the proble</param>
        /// <param name="width">problem space width</param>
        /// <param name="height">problem space height</param>
        /// <returns>Generated problem</returns>
        public static NodesCollection MakeNodes(int numberOfNodes, int width, int height)
        {
            Random          random = new Random((int)DateTime.Now.Ticks);
            NodesCollection nodes  = new NodesCollection();

            for (int i = 0; i < numberOfNodes; i++)
            {
                nodes.Add(new Node2D(i + 1, random.NextDouble() * width, random.NextDouble() * height));
            }

            return(nodes);
        }
 public void Update()
 {
     foreach (Node node in ConnectedNodes)
     {
         if (!_nodes.Contains(node))
         {
             _nodes.Add(node);
             AddHandlers(node);
             StateChanged(node, NodeState.Offline);
         }
     }
 }
        /// <summary>
        /// Loads nodes that are depots
        /// </summary>
        /// <param name="nodes">all nodes in graph</param>
        /// <returns>Nodes that are depots</returns>
        protected List <INode> LoadDepots(IEnumerable <INode> nodes)
        {
            NodesCollection graphNodes = new NodesCollection(nodes);
            NodesCollection result     = new NodesCollection();

            foreach (int id in tspFile.Depots)
            {
                result.Add(graphNodes.FindById(id));
            }

            return(result.ToList());
        }
Exemple #10
0
        public static NodesCollection SelectAll()
        {
            NodesCollection List = new NodesCollection();

            using (IDataReader rd = SqlHelper.ExecuteReader(DAL.con(), CommandType.StoredProcedure, "sp_tblNodes_Select_SelectAll_linhnx"))
            {
                while (rd.Read())
                {
                    List.Add(getFromReader(rd));
                }
            }
            return(List);
        }
Exemple #11
0
        public override Node VisitObjectDeclaration(TParser.ObjectDeclarationContext context)
        {
            var col = new NodesCollection <ObjectMember>(context.Start);

            if (context.objectMember() == null)
            {
                return(col);
            }
            foreach (var member in context.objectMember())
            {
                col.Add(VisitObjectMember(member).As <ObjectMember>());
            }
            return(new ObjectDeclaration(col, context.Start));
        }
Exemple #12
0
        public override Node VisitArrayDeclaration(TParser.ArrayDeclarationContext context)
        {
            var col = new NodesCollection <Node>(context.Start);

            if (context.commaExpressions() == null)
            {
                return(new ArrayDeclaration(col, context.Start));
            }
            foreach (var exp in context.commaExpressions().expression())
            {
                col.Add(Visit(exp));
            }
            return(new ArrayDeclaration(col, context.Start));
        }
Exemple #13
0
 protected virtual ArrayListSerialized Functions(NodesCollection c, NodeDescriptor d)
 {
     ArrayListSerialized res = new ArrayListSerialized();
     ILisp l = Lisp.Current;
     foreach (Symbol s in l.Interpreter.CurrentPackage.ExternalTable.GetSymbols() ){
         if (s.IsDefined && s.GlobalValue is Front.Lisp.Closure) {
             NodeDescriptor n = c.GetDescriptor(s.GlobalValue);
             n.NodePath = s.Name;
             n.NodeName = s.Name;
             res.Add(n);
             c.Add(n);
         }
     }
     foreach (Symbol s in l.Interpreter.CurrentPackage.InternalTable.GetSymbols()) {
         if (s.IsDefined && s.GlobalValue is Front.Lisp.Closure ) {
             NodeDescriptor n = c.GetDescriptor(s.GlobalValue);
             n.NodePath = s.Name;
             n.NodeName = s.Name;
             res.Add(n);
             c.Add(n);
         }
     }
     return res;
 }
Exemple #14
0
        public static NodesCollection SelectByWf(string WF_ID)
        {
            NodesCollection List = new NodesCollection();

            SqlParameter[] obj = new SqlParameter[1];
            obj[0] = new SqlParameter("WF_ID", WF_ID);
            using (IDataReader rd = SqlHelper.ExecuteReader(DAL.con(), CommandType.StoredProcedure, "sp_tblNodes_Select_SelectByWf_linhnx", obj))
            {
                while (rd.Read())
                {
                    List.Add(getFromReader(rd));
                }
            }
            return(List);
        }
        public void FindPath(MapPosition startPosition, MapPosition endPosition, ref List <MapPosition> results)
        {
            if (results == null)
            {
                results = new List <MapPosition>();
            }
            results.Clear();

            // if(the locations are the same then ... exit this procedure.
            if (object.Equals(startPosition, endPosition))
            {
                return;
            }

            // Add the starting square to the open list.
            NodesCollection openedNodes = new NodesCollection();
            NodesCollection closedNodes = new NodesCollection();

            openedNodes.Add(startPosition);

            MapCellsCollection parentNodes = new MapCellsCollection();
            int                lowestItem, foundItem, nodeIndex, parentIndex;
            NodeItem           currentNode;
            List <MapPosition> connectingNodes = new List <MapPosition>();

            do
            {
                // ... Look for the lowest F cost square on the open list ...
                lowestItem = openedNodes.FindByLowestF();
                if (lowestItem < 0)
                {
                    return;
                }
                currentNode = openedNodes[lowestItem];

                // ... Switch it to the close list ...
                closedNodes.Add(currentNode);
                openedNodes.RemoveAt(lowestItem);

                // ... if(we added the target square to the closed list, then quit.
                if (object.Equals(currentNode.Position, endPosition))
                {
                    break;
                }

                // ... for(each of the cell's sides to this current square ...
                this.EnumConnectingNodes(currentNode.Position, ref connectingNodes);
                if (connectingNodes != null && connectingNodes.Count > 0)
                {
                    for (nodeIndex = 0; nodeIndex < connectingNodes.Count; nodeIndex++)
                    {
                        if (closedNodes.FindIndex(connectingNodes[nodeIndex]) < 0)
                        {
                            // ... Find if the cell is in the open list ...
                            foundItem = openedNodes.FindIndex(connectingNodes[nodeIndex]);
                            // ... if(the cell isn't in the open list then add it ...
                            if (foundItem < 0)
                            {
                                openedNodes.Add(connectingNodes[nodeIndex], (currentNode.G + 1), connectingNodes[nodeIndex].GetDistance(endPosition));

                                parentIndex = parentNodes.FindIndex(connectingNodes[nodeIndex]);
                                if (parentIndex < 0)
                                {
                                    parentNodes.Add(connectingNodes[nodeIndex], currentNode.Position);
                                }
                                else
                                {
                                    parentNodes[parentIndex].Next = currentNode.Position;
                                }
                            }
                            else
                            {
                                if (openedNodes[foundItem].G < currentNode.G)
                                {
                                    openedNodes[foundItem].G = (currentNode.G + 1);

                                    parentIndex = parentNodes.FindIndex(connectingNodes[nodeIndex]);
                                    if (parentIndex < 0)
                                    {
                                        parentNodes.Add(connectingNodes[nodeIndex], currentNode.Position);
                                    }
                                    else
                                    {
                                        parentNodes[parentIndex].Next = currentNode.Position;
                                    }
                                }
                            }
                        }
                    } //nodeIndex
                }
            } while(true);

            // Add the last cell to the list.
            results.Insert(0, (MapPosition)endPosition.Clone());

            // Now that we're done, let's check the results and return it.
            int currentAddress = parentNodes.FindIndex(endPosition);

            do
            {
                if (currentAddress < 0 || (parentNodes[currentAddress].Next == null || parentNodes[currentAddress].Next.IsEmpty()))
                {
                    break;
                }
                else
                {
                    if (!object.Equals(parentNodes[currentAddress].Next, startPosition))
                    {
                        results.Insert(0, (MapPosition)parentNodes[currentAddress].Next.Clone());
                    }
                    currentAddress = parentNodes.FindIndex(parentNodes[currentAddress].Next);
                }
            } while(true);
        } // FindPath
        public static RootNode BuildGraph(Grammar.Grammar grammar)
        {
            {
                var nonTerminalHeads = ProcessNonTerminalsHeads();
                ProcessRemainingNonTerminalsItems(nonTerminalHeads);

                var rootHead = nonTerminalHeads[grammar.Root.NonTerminalUsage.Impl];
                return(new RootNode().WithChild(rootHead));
            }

            IReadOnlyDictionary <GrammarNonTerminal, Node> ProcessNonTerminalsHeads()
            {
                var nonTerminalHeads = new Dictionary <GrammarNonTerminal, Node>();

                foreach (var nonTerminal in grammar.NonTerminals)
                {
                    ProcessSingle(nonTerminal);
                }

                return(nonTerminalHeads);

                Node ProcessSingle(GrammarNonTerminal nonTerminal)
                {
                    if (nonTerminalHeads.TryGetValue(nonTerminal, out var aleadyProcessed))
                    {
                        if (aleadyProcessed == null)
                        {
                            var msg = $"Recursive non-terminal {nonTerminal.Name} usage cannot be placed " +
                                      "on the very start of non-terminal body. It would cause " +
                                      "infinite recursion.";
                            throw new StateTransitionException(msg,
                                                               StateTransitionException.Id.InfiniteRecursion);
                        }

                        return(aleadyProcessed);
                    }

                    // marks that processing of `nonTerminal` started
                    nonTerminalHeads.Add(nonTerminal, null);

                    var heads = new NodesCollection();

                    foreach (var orConditionOperand in nonTerminal.Body.OrConditionOperands)
                    {
                        var bodyItem = orConditionOperand[0];

                        switch (bodyItem)
                        {
                        case GrammarTerminalUsage terminalUsage:
                            var terminalNode = new TerminalNode(terminalUsage);
                            heads.Add(terminalNode);
                            break;

                        case GrammarNonTerminalUsage nonTerminalUsage:
                            var newNonTerminalHead   = ProcessSingle(nonTerminalUsage.Impl);
                            var nonTerminalUsageNode =
                                new NonTerminalUsageNode(nonTerminalUsage.Name, newNonTerminalHead);
                            heads.Add(nonTerminalUsageNode);
                            break;

                        default:
                            throw new StateTransitionException(
                                      $"Unknown non-terminal body item type: {bodyItem.Type}",
                                      StateTransitionException.Id.UnknownNonTerminalBodyItemType);
                        }
                    }

                    var head = heads.Count > 1 ? new BranchNode(heads) : heads[0];

                    if (nonTerminalHeads[nonTerminal] != null)
                    {
                        throw new StateTransitionException("Unexpected error.",
                                                           StateTransitionException.Id.UnexpectedError);
                    }

                    nonTerminalHeads[nonTerminal] = head;
                    return(head);
                }
            }

            void ProcessRemainingNonTerminalsItems(IReadOnlyDictionary <GrammarNonTerminal, Node> nonTerminalHeads)
            {
                foreach (var nonTerminal in grammar.NonTerminals)
                {
                    ProcessSingle(nonTerminal);
                }

                void ProcessSingle(GrammarNonTerminal nonTerminal)
                {
                    var head = nonTerminalHeads[nonTerminal];

                    for (var operandIndex = 0;
                         operandIndex < nonTerminal.Body.OrConditionOperands.Count;
                         operandIndex++)
                    {
                        var currentTail = head is BranchNode node
                            ? node.Heads.ToList()[operandIndex]
                            : head;

                        foreach (var bodyItem in nonTerminal.Body.OrConditionOperands[operandIndex].Skip(1))
                        {
                            switch (bodyItem)
                            {
                            case GrammarTerminalUsage terminalUsage:
                                var terminalNode = new TerminalNode(terminalUsage);
                                currentTail.AddChild(terminalNode);
                                currentTail = terminalNode;
                                break;

                            case GrammarNonTerminalUsage nonTerminalUsage:
                                var newNonTerminalHead   = nonTerminalHeads[nonTerminalUsage.Impl];
                                var nonTerminalUsageNode =
                                    new NonTerminalUsageNode(nonTerminalUsage.Name, newNonTerminalHead);
                                currentTail.AddChild(nonTerminalUsageNode);
                                currentTail = nonTerminalUsageNode;
                                break;

                            default:
                                throw new StateTransitionException(
                                          $"Unknown non-terminal body item type: {bodyItem.Type}",
                                          StateTransitionException.Id.UnknownNonTerminalBodyItemType);
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Loads nodes that are depots
        /// </summary>
        /// <param name="nodes">all nodes in graph</param>
        /// <returns>Nodes that are depots</returns>
        protected List<INode> LoadDepots(IEnumerable<INode> nodes)
        {
            NodesCollection graphNodes = new NodesCollection(nodes);
            NodesCollection result = new NodesCollection();
            foreach (int id in tspFile.Depots)
            {
                result.Add(graphNodes.FindById(id));
            }

            return result;
        }