Example #1
0
        public void TestEncodeList1()
        {
            //Test1
            ListNode list1   = new ListNode();
            string   source1 = BEncode.StringEncode(list1);

            Assert.AreEqual("{}", source1);

            //Test2
            ListNode list2 = new ListNode();

            for (int i = 1; i <= 3; i++)
            {
                list2.Add(i);
            }
            string source2 = BEncode.StringEncode(list2);

            Assert.AreEqual(source2, "{i1ei2ei3e}");

            //Test3
            ListNode lh31 = new ListNode();

            lh31.Add("Alice");
            lh31.Add("Bob");
            ListNode lh32 = new ListNode();

            lh32.Add(2);
            lh32.Add(3);
            ListNode list3   = new ListNode(new List <BEncodedNode>(new BEncodedNode[] { lh31, lh32 }));
            string   source3 = BEncode.StringEncode(list3);

            Assert.AreEqual(source3, "{{5:Alice3:Bob}{i2ei3e}}");
        }
Example #2
0
        /// <summary>
        /// 处理Reader函数
        /// </summary>
        /// <param name="cmd">数据库命令</param>
        public DictNode HandleReader(DbCommand cmd)
        {
            DictNode result = new DictNode();

            using (DbConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                cmd.Connection = conn;
                using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    ListNode typeListNode = CreateTypeListNode(rdr);
                    result.Add("typelist", typeListNode);

                    ListNode itemsNode = new ListNode();
                    while (rdr.Read())
                    {
                        ListNode itemNode = new ListNode();
                        for (int i = 0; i < rdr.FieldCount; i++)
                        {
                            if (rdr.IsDBNull(i))
                            {
                                itemNode.Add(BytesNode.NullBytesNode);
                            }
                            else
                            {
                                if (IsBinaryType(rdr.GetDataTypeName(i)))
                                {
                                    itemNode.Add((byte[])rdr.GetValue(i));
                                }
                                else
                                {
                                    itemNode.Add(rdr.GetValue(i).ToString());
                                }
                            }
                        }
                        itemsNode.Add(itemNode);
                    }
                    result.Add("items", itemsNode);
                }
            }
            result.Add("code", 2);

            ListNode parmlistNode = CreateParmListNode(cmd);

            if (parmlistNode.Count != 0)
            {
                result.Add("parmlist", parmlistNode);
            }
            return(result);
        }
Example #3
0
        /*
         * Process Method
         */

        public override ListNode Process(GroupNode group, ListNode list)
        {
            if (group.Decorations[MutualExclusionDecision.DECISION_KEY] != null)
            {
                MutualExclusionDecision d =
                    (MutualExclusionDecision)group.Decorations[MutualExclusionDecision.DECISION_KEY];

                if (d.Handled)
                {
                    return(list);
                }
            }

            if (group.Decorations[ListNodeDecision.DECISION_KEY] != null)
            {
                return(list);
            }

            if (group.Labels != null)
            {
                LabeledListNode item = new LabeledListNode(group.Labels);
                item.Group = group;

                list.Add(item);

                group.Decorations.Add(ListNodeDecision.DECISION_KEY,
                                      new ListNodeDecision(item));

                return(item);
            }

            return(list);
        }
Example #4
0
        private ListNode CreateParmListNode(DbCommand cmd)
        {
            ListNode result = new ListNode();

            foreach (DbParameter para in cmd.Parameters)
            {
                switch (para.Direction)
                {
                case ParameterDirection.InputOutput:
                case ParameterDirection.Output:
                case ParameterDirection.ReturnValue:
                    DictNode parmNode = new DictNode();
                    parmNode.Add("name", para.ParameterName);
                    parmNode.Add("type", para.Value.GetType().ToString());
                    if (para.Value == DBNull.Value)
                    {
                        parmNode.Add("value", BytesNode.NullBytesNode);
                    }
                    else if (para.DbType == DbType.Binary)
                    {
                        parmNode.Add("value", (byte[])para.Value);
                    }
                    else
                    {
                        parmNode.Add("value", para.Value.ToString());
                    }
                    result.Add(parmNode);
                    break;
                }
            }
            cmd.Parameters.Clear();
            cmd.Dispose();
            return(result);
        }
Example #5
0
        /*
         * Process Method
         */

        public override ListNode Process(GroupNode group, ListNode list)
        {
            if (group.Decorations[UnitDecision.DECISION_KEY] != null)
            {
                // get the values that we need
                UnitDecision decision =
                    (UnitDecision)group.Decorations[UnitDecision.DECISION_KEY];

                CIOListItemNode item = new CIOListItemNode(decision.CIO);
                item.Decorations[GroupDecision.DECISION_KEY] =
                    new GroupDecision(group);

                // determine whether this CIO should be on a panel
                bool onPanel = decision.CIO is ControlBasedCIO;

                item.Decorations[PanelDecision.DECISION_KEY] =
                    new PanelDecision(decision, onPanel);

                if (onPanel)
                {
                    LabelDictionary labels;
                    if (decision.CIO is SmartCIO)
                    {
                        labels = ((SmartCIO)decision.CIO).Labels;
                    }
                    else
                    {
                        labels = ((ApplianceState)((StateLinkedCIO)decision.CIO).GetApplObj()).Labels;
                    }

                    if (labels != null)
                    {
                        PanelListNode pNode = new PanelListNode(labels);
                        pNode.Add(item);
                        list.Add(pNode);
                    }
                }
                else
                {
                    list.Add(item);
                }
            }

            return(list);
        }
Example #6
0
        public Node ListType()
        {
            var result = new ListNode()
            {
                AnchorToken = Expect(TokenCategory.LIST)
            };

            Expect(TokenCategory.OF);

            result.Add(SimpleType());

            return(result);
        }
Example #7
0
        /// <summary>
        /// 创建类型节点
        /// </summary>
        /// <param name="rdr">数据库读取器</param>
        /// <returns>返回类型节点</returns>
        private ListNode CreateTypeListNode(IDataRecord rdr)
        {
            ListNode result = new ListNode();

            for (int i = 0; i < rdr.FieldCount; i++)
            {
                DictNode columnNode = new DictNode();
                columnNode.Add("name", rdr.GetName(i));
                columnNode.Add("type", rdr.GetDataTypeName(i));
                result.Add(columnNode);
            }
            return(result);
        }
Example #8
0
        /// <summary>
        /// 生成存储过程发送的字节流
        /// </summary>
        /// <returns>返回发送的字节流</returns>
        private byte[] BuildProcMessage(DictNode contentNode)
        {
            contentNode.Add("text", commandText);

            //建立参数列表
            if (parameters.Count != 0)
            {
                ListNode parmsNode = new ListNode();
                foreach (DbProxyParameter para in parameters)
                {
                    DictNode parmNode = new DictNode();
                    parmNode.Add("name", para.ParameterName);
                    parmNode.Add("type", (int)para.DbType);

                    switch (para.Direction)
                    {
                    case ParameterDirection.Input:
                        parmNode.Add("value", NodeEncoder.GetValueNode(para));
                        break;

                    case ParameterDirection.Output:
                        parmNode.Add("direction", OutputNum);
                        break;

                    case ParameterDirection.InputOutput:
                        parmNode.Add("direction", InputOutputNum);
                        parmNode.Add("value", NodeEncoder.GetValueNode(para));
                        break;

                    case ParameterDirection.ReturnValue:
                        parmNode.Add("direction", ReturnValueNum);
                        break;
                    }


                    if (para.Size != 0)
                    {
                        parmNode.Add("size", para.Size);
                    }

                    parmsNode.Add(parmNode);
                }

                contentNode.Add("parms", parmsNode);
            }

            return(BEncode.ByteArrayEncode(contentNode));
        }
Example #9
0
        public static void AddTwoNumbersTest()
        {
            ListNode l1 = new ListNode(2);

            l1.Add(4).Add(3);

            ListNode l2 = new ListNode(5);

            l2.Add(6);

            var res = AlgorithmCenter.AlgorithmCenter191213.AddTwoNumbers(l1, l2);

            while (res != null)
            {
                Console.Write(res.val);
                res = res.next;
            }
        }
Example #10
0
 private void GetListNode(TreeNodeCollection nodes)
 {
     foreach (TreeNode node in nodes)
     {
         string path = (string)node.Tag;
         if (!File.Exists(path))
         {
             GetListNode(node.Nodes);
         }
         else
         {
             if (!ListNode.Contains(node.Parent))
             {
                 ListNode.Add(node.Parent);
             }
         }
     }
     return;
 }
Example #11
0
        public void MergeTwoSortedListByListNodeTest()
        {
            ListNode<int> list1 = new ListNode<int>(1);
            list1.Add(new ListNode<int>(3));
            list1.NextNode.Add(new ListNode<int>(5));
            list1.NextNode.NextNode.Add(new ListNode<int>(7));

            ListNode<int> list2 = new ListNode<int>(2);
            list2.Add(new ListNode<int>(4));
            list2.NextNode.Add(new ListNode<int>(6));
            list2.NextNode.NextNode.Add(new ListNode<int>(8));

            ListNode<int> actual = Algorithm.MergeTwoSortedListByListNode(list1, list2);
            ListNode<int> next = actual;
            while (next.NextNode != null)
            {
                Console.WriteLine(next.Value);
                next = next.NextNode;
            }

            Console.WriteLine(next.Value);
        }
        /*
         * Process Method
         */

        public override ListNode Process(GroupNode g, ListNode list)
        {
            // look for MutualExclusionDecisions

            object o = g.Decorations[MutualExclusionDecision.DECISION_KEY];

            if (o != null && o is MutualExclusionDecision)
            {
                MutualExclusionDecision d = (MutualExclusionDecision)o;

                if (!d.Handled &&
                    d.State.Type.ValueSpace is EnumeratedSpace)
                {
                    EnumeratedSpace espc = (EnumeratedSpace)d.State.Type.ValueSpace;

                    if (espc.GetItemCount() == (d.DependencySets.Count - 1))
                    {
                        for (int i = 1; i < d.DependencySets.Count; i++)
                        {
                            ArrayList dep = (ArrayList)d.DependencySets[i];

                            if (dep.Count != 1)
                            {
                                // Globals.AddLogLine("size! " + dep.Count );
                                return(list);
                            }
                            if (!(dep[0] is EqualsDependency))
                            {
                                // Globals.AddLogLine("not equals!");
                                return(list);
                            }
                        }
                    }
                    else
                    {
                        return(list);
                    }

                    // we need a pointer to the GroupNode that contains the state
                    // variable that we are looking at (because tree manipulations
                    // will need to use it)

                    ObjectGroupNode stateGroup = null;

                    BranchGroupNode bg = (BranchGroupNode)g;
                    IEnumerator     e  = bg.Children.GetEnumerator();
                    while (e.MoveNext())
                    {
                        if (e.Current is ObjectGroupNode &&
                            ((ObjectGroupNode)e.Current).Object == d.State)
                        {
                            stateGroup = (ObjectGroupNode)e.Current;
                            break;
                        }
                    }

                    // re-order the tree

                    ArrayList childOrder = new ArrayList();

                    bg.Children.Remove(stateGroup);

                    BranchGroupNode midG = new BranchGroupNode();

                    midG.Children = bg.Children;
                    e             = midG.Children.GetEnumerator();
                    while (e.MoveNext())
                    {
                        ((GroupNode)e.Current).Parent = midG;
                    }

                    bg.Children = new ArrayList();

                    ArrayList dset = (ArrayList)d.ChildSets[0];
                    if (dset.Count > 0)
                    {
                        e = dset.GetEnumerator();
                        while (e.MoveNext())
                        {
                            GroupNode c = (GroupNode)e.Current;

                            c.Parent = bg;

                            midG.Children.Remove(c);
                            bg.Children.Add(c);
                        }
                    }

                    bg.Children.Add(midG);
                    midG.Parent       = bg;
                    stateGroup.Parent = bg;

                    for (int i = 1; i < d.ChildSets.Count; i++)
                    {
                        dset = (ArrayList)d.ChildSets[i];

                        if (dset.Count > 1)
                        {
                            BranchGroupNode newG = new BranchGroupNode();

                            newG.Parent = midG;

                            e = dset.GetEnumerator();
                            while (e.MoveNext())
                            {
                                GroupNode c = (GroupNode)e.Current;

                                c.Parent = newG;

                                midG.Children.Remove(c);
                                newG.Children.Add(c);
                            }

                            childOrder.Insert(i - 1, newG);
                        }
                        else if (dset.Count == 0)
                        {
                            BranchGroupNode newG = new BranchGroupNode();

                            newG.Parent = midG;
                            midG.Children.Add(newG);

                            childOrder.Insert(i - 1, newG);
                        }
                        else
                        {
                            childOrder.Insert(i - 1, dset[0]);
                        }
                    }

                    d.DependencySets.RemoveAt(0);

                    // now create StateValueListNodes from the re-orged group tree
                    // these nodes will be picked up by an additional rule later
                    // in the generation process
                    StateValueListNode newList = null;
                    for (int i = 0; i < childOrder.Count; i++)
                    {
                        GroupNode group   = (GroupNode)childOrder[i];
                        ArrayList aryDeps = (ArrayList)d.DependencySets[i];

                        EqualsDependency eqDep = (EqualsDependency)aryDeps[0];

                        newList = new StateValueListNode(d.State, eqDep.Value);
                        list.Add(newList);

                        group.Decorations.Add(ListNodeDecision.DECISION_KEY, new ListNodeDecision(newList, d));
                    }

                    d.Handled = true;
                }
            }

            return(list);
        }
Example #13
0
        /*
         * Member Variables
         */

        /*
         * Constructor
         */

        /*
         * Process Rule Method
         */

        /// <summary>
        /// This method checks if for a pattern that looks like
        /// ListNode->ListNode(one child)->ListNode and removes the
        /// intermediate ListNode.
        /// </summary>
        public override bool Process(ListItemNode node, UIGenerator ui)
        {
            // promotion rules can't work on the root node
            bool ableToPromote = node.Parent != null;

            if (node is ListNode)
            {
                ListNode  listNode   = (ListNode)node;
                Hashtable panelNodes = new Hashtable(listNode.Items.Count);

                IEnumerator e = listNode.Items.GetEnumerator();
                while (e.MoveNext())
                {
                    if (e.Current is PanelListNode)
                    {
                        //
                        // ASSUMPTION: All items in a panel node share
                        // the same parent group
                        //
                        GroupDecision d =
                            (GroupDecision)((ListItemNode)((PanelListNode)e.Current).Items[0]).Decorations[GroupDecision.DECISION_KEY];
                        if (d != null)
                        {
                            ArrayList list = (ArrayList)panelNodes[d.Group.Parent];

                            if (list == null)
                            {
                                list = new ArrayList(listNode.Items.Count);
                            }

                            list.Add(e.Current);

                            panelNodes[d.Group.Parent] = list;
                        }
                    }
                }

                e = panelNodes.Keys.GetEnumerator();
                while (e.MoveNext())
                {
                    GroupNode parent = (GroupNode)e.Current;

                    if (parent.Labels == null)
                    {
                        // has no labels, so abort for this group
                        continue;
                    }

                    // create a new PanelNode and move all items into this node
                    PanelListNode newPanel = new PanelListNode(parent.Labels);
                    ArrayList     list     = (ArrayList)panelNodes[parent];

                    if (list.Count <= 1)
                    {
                        continue;
                    }

                    IEnumerator panels = list.GetEnumerator();
                    while (panels.MoveNext())
                    {
                        PanelListNode existPanel = (PanelListNode)panels.Current;
                        IEnumerator   items      = existPanel.Items.GetEnumerator();
                        while (items.MoveNext())
                        {
                            newPanel.Add((ListItemNode)items.Current);
                        }

                        listNode.Remove(existPanel);
                    }

                    listNode.Add(newPanel);
                }
            }

            return(false);
        }
Example #14
0
        protected Boundary DelimitedBy(Queue <LexerNode> lexer,
                                       DelimeterParams _params,
                                       ListNode node,
                                       Func <Queue <LexerNode>, AbstractNode> parser)
        {
            var boundary = new Boundary();

            boundary.StartPointer = lexer.Peek();
            if (_params.IsStart != null && !_params.IsStart(lexer.Dequeue()))  //incorect declaration
            {
                throw new SyntaxException(lexer.Peek());
            }
            var       blocks  = new Queue <LexerNode>();
            LexerNode next    = null;
            var       bracket = 1;

            do
            {
                lexer.ThrowIfEmpty(next ?? boundary.StartPointer);
                next = lexer.Dequeue();

                if (next.Token == LexerTokens.Comment)
                {
                    //OnIgnored(next);
                    node.Add(new CommentsNode()
                    {
                        Lex = next
                    });
                    continue;
                }
                if (_params.IsStart != null && _params.IsStart(next))
                {
                    bracket++;
                }

                if ((_params.IsSeparator(next) && bracket == 1) || !lexer.Any())
                {
                    if (blocks.Any())
                    {
                        node.Add(parser(blocks));
                        blocks.ThrowIfAny();
                    }
                    continue;
                }

                if (_params.IsStop(next))
                {
                    bracket--;
                    if (bracket == 0)
                    {
                        if (blocks.Any())
                        {
                            node.Add(parser(blocks));
                            blocks.ThrowIfAny();
                        }
                    }
                }

                blocks.Enqueue(next);
            } while (bracket != 0 && lexer.Any());
            boundary.EndPointer = next;
            return(boundary);
        }