Flatten() static private method

static private Flatten ( ArrayList ar, TreeNode, nodes ) : void
ar ArrayList
nodes TreeNode,
return void
Ejemplo n.º 1
0
        public void Flatten_IntegerTreeNode_EquivalentToExpectedList()
        {
            // Create a tree like the following
            // 1
            // 2 3 5
            // 6 7 4 9 10
            // 11 12 13 14
            // 16 15 17
            var rootNode   = new TreeNode <int>(1);
            var firstLevel = rootNode.AddChildren(2, 3, 5).ToList();

            firstLevel.First().AddChildren(6).First().AddChildren(11, 12).Last().AddChildren(16);
            firstLevel.Skip(1).First().AddChildren(7, 4);
            var node5Children = firstLevel.Skip(2).First().AddChildren(9, 10).ToList();

            node5Children.First().AddChildren(13);
            var node14 = node5Children.Skip(1).First().AddChildren(14).First();

            node14.AddChildren(15, 17);

            var expected = new List <int> {
                1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17
            };
            var actual = rootNode.Flatten().ToList();

            CollectionAssert.AreEquivalent(expected, actual);
        }
Ejemplo n.º 2
0
        public string CompileProcess(string process, string id)
        {
            Evaluator eval = new Evaluator();

            foreach (KeyValuePair <string, SExpFunctionHandler> function in sexpr_functions)
            {
                eval.RegisterFunction(function.Value, function.Key);
            }

            foreach (PipelineVariable variable in this)
            {
                double?numeric = variable.CurrentValueNumeric;
                if (numeric != null)
                {
                    if (variable.CurrentValue.Contains("."))
                    {
                        eval.RegisterVariable(variable.Id, numeric.Value);
                    }
                    else
                    {
                        eval.RegisterVariable(variable.Id, (int)numeric.Value);
                    }
                }
                else
                {
                    eval.RegisterVariable(variable.Id, variable.CurrentValue);
                }
            }

            TreeNode result = eval.EvaluateString(process);

            if (eval.Success && result is StringLiteral)
            {
                return((result.Flatten() as StringLiteral).Value);
            }
            else if (!eval.Success && ApplicationContext.Debugging)
            {
                Console.WriteLine("Could not compile pipeline S-Expression for pipeline:");
                Console.WriteLine(process);
                Console.WriteLine("-----");
                Console.WriteLine(eval.ErrorMessage);
                Console.WriteLine("-----");
                Console.WriteLine("Stack Trace:");

                foreach (Exception e in eval.Exceptions)
                {
                    Console.WriteLine(e.Message);
                }

                Console.WriteLine("-----");
                Console.WriteLine("Expression Tree:");
                Hyena.SExpEngine.TreeNode.DumpTree(eval.ExpressionTree);
            }

            return(null);
        }
Ejemplo n.º 3
0
        String buildChain(TreeNode <IChainElement> tree)
        {
            List <IChainElement> list = tree.Flatten().Reverse().ToList();

            if (list.Count == 0)
            {
                return(String.Empty);
            }
            var sb = new StringBuilder();

            for (Int32 i = 0; i < list.Count - 1; i++)
            {
                sb.AppendLine(buildCertEntry(i, list[i]));
            }

            return(String.Format(HtmlTemplate.HTML_CERT_CHAIN,
                                 tree.Value.Name,
                                 sb));
        }
        private TreeNode SExprConstructPipelinePart(EvaluatorBase evaluator, TreeNode [] args, bool element)
        {
            StringBuilder builder = new StringBuilder();

            TreeNode list = new TreeNode();

            foreach (TreeNode arg in args)
            {
                list.AddChild(evaluator.Evaluate(arg));
            }

            list = list.Flatten();

            for (int i = 0; i < list.ChildCount; i++)
            {
                TreeNode node = list.Children[i];

                string value = node.ToString().Trim();

                builder.Append(value);

                if (i == 0)
                {
                    if (list.ChildCount > 1)
                    {
                        builder.Append(element ? ' ' : ',');
                    }

                    continue;
                }
                else if (i % 2 == 1)
                {
                    builder.Append('=');
                }
                else if (i < list.ChildCount - 1)
                {
                    builder.Append(element ? ' ' : ',');
                }
            }

            return(new StringLiteral(builder.ToString()));
        }
Ejemplo n.º 5
0
    void drawBrowser()
    {
        foreach (var obj in dirDictionary.Keys.Concat(fileDictionary.Keys))
        {
            Destroy(obj);
        }
        dirDictionary.Clear();
        fileDictionary.Clear();
        foreach (var item in dirRoot.Flatten().Skip(1))
        {
            item.gameObject = addBrowserDirItem(item.dirInfo.Name, item.depth);
            dirDictionary.Add(item.gameObject, item);
        }

        if (fileList == null)
        {
            return;
        }
        foreach (var item in fileList)
        {
            fileDictionary.Add(addBrowserFileItem(item.Name, item.LastWriteTime), item);
        }
    }
        private static Expression CreateTablePerTypeQueryExpression(
            Type targetType,
            TreeNode <TablePerTypeInfo> hierarchyRoot)
        {
            var columnDescriptors
                = hierarchyRoot
                  .Flatten()
                  .SelectMany(x => x.TableDescriptor.ColumnDescriptors)
                  .Select((c, i) => new { c, i });

            var columnExpressions
                = hierarchyRoot
                  .Flatten()
                  .SelectMany(x => x.Columns);

            var tupleType          = ValueTupleHelper.CreateTupleType(columnExpressions.Select(c => c.Type));
            var tupleNewExpression = ValueTupleHelper.CreateNewExpression(tupleType, columnExpressions);
            var tupleParameter     = Expression.Parameter(tupleType);

            var polymorphicTypeDescriptors
                = (from node in hierarchyRoot.Flatten()
                   where !node.Type.GetTypeInfo().IsAbstract
                   let testMember = node.TableDescriptor.PrimaryKeyMembers.First()
                                    select new PolymorphicTypeDescriptor(
                       node.Type,
                       Expression.Lambda(
                           Expression.NotEqual(
                               ValueTupleHelper.CreateMemberExpression(
                                   tupleType,
                                   tupleParameter,
                                   (from x in columnDescriptors
                                    where x.c.SourceMember == testMember
                                    select x.i).Single()),
                               Expression.Constant(null, testMember.GetMemberType().MakeNullableType())),
                           tupleParameter),
                       Expression.Lambda(
                           Expression.MemberInit(
                               Expression.New(node.Type),
                               (from x in columnDescriptors
                                where node.Path.Contains(x.c.SourceType)
                                group x by x.c.SourceMember.MetadataToken into xg
                                select xg.Last() into x
                                select Expression.Bind(
                                    x.c.SourceMember,
                                    Expression.Convert(
                                        ValueTupleHelper.CreateMemberExpression(tupleType, tupleParameter, x.i),
                                        x.c.SourceMember.GetMemberType())))),
                           tupleParameter))).ToArray();

            var tableExpression
                = hierarchyRoot
                  .Aggregate <TableExpression>(
                      hierarchyRoot.Value.Table,
                      (accumulate, parent, child) =>
            {
                var keyComparisons
                    = from k in hierarchyRoot.Value.TableDescriptor.PrimaryKeyMembers
                      join l in parent.TableDescriptor.ColumnDescriptors
                      on k.MetadataToken equals l.SourceMember.MetadataToken
                      join r in child.TableDescriptor.ColumnDescriptors
                      on k.MetadataToken equals r.SourceMember.MetadataToken
                      select Expression.Equal(
                          new SqlColumnExpression(parent.Table, l.ColumnName, l.Type, l.IsNullable, null),
                          new SqlColumnExpression(child.Table, r.ColumnName, r.Type, r.IsNullable, null));

                return(new LeftJoinTableExpression(
                           accumulate,
                           child.Table,
                           keyComparisons.Aggregate(Expression.AndAlso),
                           hierarchyRoot.Value.Type));
            });

            return(new EnumerableRelationalQueryExpression(
                       new SelectExpression(
                           new ServerProjectionExpression(
                               new PolymorphicExpression(
                                   targetType,
                                   tupleNewExpression,
                                   polymorphicTypeDescriptors)),
                           tableExpression)));
        }
        private static Expression CreateTablePerTypeQueryExpression(
            Type targetType,
            TreeNode <TablePerTypeInfo> hierarchyRoot)
        {
            var columnDescriptors
                = hierarchyRoot
                  .Flatten()
                  .SelectMany(x => x.TableDescriptor.ColumnDescriptors)
                  .Select((c, i) => new { c, i });

            var columnExpressions
                = hierarchyRoot
                  .Flatten()
                  .SelectMany(x => x.Columns);

            var tupleType          = ValueTupleHelper.CreateTupleType(columnExpressions.Select(c => c.Type));
            var tupleNewExpression = ValueTupleHelper.CreateNewExpression(tupleType, columnExpressions);
            var tupleParameter     = Expression.Parameter(tupleType);

            // TODO: Use ExpandParameters instead
            // Use ExpandParameters on a LambdaExpression representing
            // the materializer for the concrete type. Expand it the
            // LambdaExpression so that all of the bindings are bound
            // to the tuple's field accessors.

            var polymorphicTypeDescriptors
                = (from node in hierarchyRoot.Flatten()
                   where !node.Type.GetTypeInfo().IsAbstract
                   let testMember = node.TableDescriptor.PrimaryKeyMembers.First()
                                    select new PolymorphicExpression.TypeDescriptor(
                       node.Type,
                       Expression.Lambda(
                           Expression.NotEqual(
                               ValueTupleHelper.CreateMemberExpression(
                                   tupleType,
                                   tupleParameter,
                                   (from x in columnDescriptors
                                    where x.c.SourceMember == testMember
                                    select x.i).Single()),
                               Expression.Constant(null, MakeNullableType(GetMemberType(testMember)))),
                           tupleParameter),
                       Expression.Lambda(
                           Expression.MemberInit(
                               Expression.New(node.Type),
                               (from x in columnDescriptors
                                where node.Path.Contains(x.c.SourceType)
                                group x by x.c.SourceMember.MetadataToken into xg
                                select xg.Last() into x
                                select Expression.Bind(
                                    x.c.SourceMember,
                                    Expression.Convert(
                                        ValueTupleHelper.CreateMemberExpression(tupleType, tupleParameter, x.i),
                                        GetMemberType(x.c.SourceMember))))),
                           tupleParameter))).ToArray();

            // TODO: Use ExpandParameters instead
            // Use ExpandParameters on a LambdaExpression representing
            // the primary key accessor for the root type. Expand it
            // once for the parent table and once for the child table
            // and use an Equal expression to compare the two.

            var tableExpression
                = hierarchyRoot
                  .Aggregate <TableExpression>(
                      hierarchyRoot.Value.Table,
                      (accumulate, parent, child) =>
            {
                var keyComparisons
                    = from k in hierarchyRoot.Value.TableDescriptor.PrimaryKeyMembers
                      join l in parent.TableDescriptor.ColumnDescriptors
                      on k.MetadataToken equals l.SourceMember.MetadataToken
                      join r in child.TableDescriptor.ColumnDescriptors
                      on k.MetadataToken equals r.SourceMember.MetadataToken
                      select Expression.Equal(
                          new SqlColumnExpression(parent.Table, l.ColumnName, l.Type, l.IsNullable),
                          new SqlColumnExpression(child.Table, r.ColumnName, r.Type, r.IsNullable));

                return(new LeftJoinExpression(
                           accumulate,
                           child.Table,
                           keyComparisons.Aggregate(Expression.AndAlso),
                           hierarchyRoot.Value.Type));
            });

            return(new EnumerableRelationalQueryExpression(
                       new SelectExpression(
                           new ServerProjectionExpression(
                               new PolymorphicExpression(
                                   targetType,
                                   tupleNewExpression,
                                   polymorphicTypeDescriptors)),
                           tableExpression)));
        }
Ejemplo n.º 8
0
        public override byte[] SerializeData(string srdiPath, string srdvPath)
        {
            using MemoryStream ms     = new MemoryStream();
            using BinaryWriter writer = new BinaryWriter(ms);

            // Iterate through the list to write the item data, while keeping track of
            // the number of entries so we know where each endpoint reference goes
            int totalEntryCount    = RootNode.Flatten().Where(node => node.Count() > 0).Count();
            int endpointOffset     = 0x10 + (totalEntryCount * 0x10);
            int totalEndpointCount = RootNode.Flatten().Where(node => node.Count() == 0).Count();
            int unknownFloatOffset = endpointOffset + (totalEndpointCount * 8);

            unknownFloatOffset += (16 - (unknownFloatOffset % 16));
            int stringOffset = unknownFloatOffset + (16 * sizeof(float));

            int maxDepth = 0;

            using BinaryWriter entryWriter    = new BinaryWriter(new MemoryStream());
            using BinaryWriter endpointWriter = new BinaryWriter(new MemoryStream());
            using BinaryWriter stringWriter   = new BinaryWriter(new MemoryStream());
            SaveTree(RootNode, entryWriter, endpointWriter, stringWriter, ref maxDepth, 0, endpointOffset, stringOffset);
            byte[] entryData    = ((MemoryStream)entryWriter.BaseStream).ToArray();
            byte[] endpointData = ((MemoryStream)endpointWriter.BaseStream).ToArray();
            byte[] stringData   = ((MemoryStream)stringWriter.BaseStream).ToArray();

            writer.Write((int)maxDepth);
            writer.Write(Unknown14);
            writer.Write((short)totalEntryCount);
            writer.Write(Unknown18);
            writer.Write((short)totalEndpointCount);
            writer.Write(unknownFloatOffset);

            writer.Write(entryData);
            Utils.WritePadding(writer, 16);
            writer.Write(endpointData);
            Utils.WritePadding(writer, 16);

            // Write matrix
            writer.Write(UnknownMatrix.M11);
            writer.Write(UnknownMatrix.M12);
            writer.Write(UnknownMatrix.M13);
            writer.Write(UnknownMatrix.M14);
            writer.Write(UnknownMatrix.M21);
            writer.Write(UnknownMatrix.M22);
            writer.Write(UnknownMatrix.M23);
            writer.Write(UnknownMatrix.M24);
            writer.Write(UnknownMatrix.M31);
            writer.Write(UnknownMatrix.M32);
            writer.Write(UnknownMatrix.M33);
            writer.Write(UnknownMatrix.M34);
            writer.Write(UnknownMatrix.M41);
            writer.Write(UnknownMatrix.M42);
            writer.Write(UnknownMatrix.M43);
            writer.Write(UnknownMatrix.M44);

            // TODO: In official $TRE blocks, the strings seem to be sorted alphabetically
            // within each tree depth layer, but not globally. The nodes are nevertheless
            // saved in the normal (arbitrary?) order.
            writer.Write(stringData);

            byte[] result = ms.ToArray();
            return(result);
        }