コード例 #1
0
 public static IEnumerable <TreeTemplate> TChild(TreeTemplate template)
 {
     return(new List <TreeTemplate>()
     {
         template
     });
 }
コード例 #2
0
ファイル: WitnessFunctions.cs プロジェクト: eglassman/refazer
        public static DisjunctiveExamplesSpec WitnessRelativeToken(GrammarRule rule, int parameter, ExampleSpec spec)
        {
            var result = new Dictionary <State, IEnumerable <object> >();

            foreach (var input in spec.ProvidedInputs)
            {
                var example = spec.DisjunctiveExamples[input].First() as Tuple <PythonNode, PythonNode>;
                if (example != null)
                {
                    //if the anchor node is the target node, we don't generate relative paths
                    //just the root path "0"
                    if (AnchorAndTargetAreEqual(example))
                    {
                        return(null);
                    }
                    var node         = example.Item1;
                    var treeTemplate = new TreeTemplate(node.GetType().Name);
                    if (node.Value != null)
                    {
                        treeTemplate.Value = node.Value;
                    }

                    result[input] = new List <TreeTemplate>()
                    {
                        treeTemplate, new Wildcard(node.GetType().Name)
                    };
                }
                else
                {
                    return(null);
                }
            }
            return(DisjunctiveExamplesSpec.From(result));
        }
コード例 #3
0
        public void MakeTreeScene()
        {
            Configuration.idBuffer = false;
#if DEBUG
            float size = 40.0f;
#else
            float size = 40.0f;
#endif
            Reset();

            AddFloor(size, 5, -0.5f);

            var tree1 = new TreeTemplate(22, 0, 4, 3.0f, 0.6f, 0.6f);
            var tree2 = new TreeTemplate(26, 0, 6, 7.0f, 1.0f, 0.6f);

            Random         r         = new Random();
            List <Vector2> positions = UniformPoissonDiskSampler.SampleCircle(Vector2.Zero, size / 2.0f, 3.0f);
            foreach (var pos in positions)
            {
                Vector3 p3d = new Vector3(pos.X, 0.0f, pos.Y);
                //Vector3 p3d = new Vector3(0.0, 0.0f, 0.0);
                double type = r.NextDouble();
                if (type > 0.5)
                {
                    MakeTree(p3d, tree1);
                }
                else
                {
                    MakeTree(p3d, tree2);
                }
            }

            AddCameras();
            AddCameraUserControls();
        }
コード例 #4
0
    public static Tree FromTemplate(TreeTemplate template, Vector2 location)
    {
        var t = GlobalMapper.Mapper.Map(template);

        t.Location = location;

        return(t);
    }
コード例 #5
0
        public static void PrintShape(TextWriter consoleWriter, int height)
        {
            // Handle boundaries as close to print as possible to detect changes in console size.
            var boundaries    = new Rectangle(0, 0, Console.WindowWidth, Console.WindowHeight);
            var shapeTemplate = new TreeTemplate('*', boundaries);

            consoleWriter.Write(shapeTemplate.Print(height));
        }
コード例 #6
0
ファイル: TreeTests.cs プロジェクト: mcmorris/shapeprinter
        public void TestTreePattern()
        {
            using (var inputReader = new StreamReader(Console.OpenStandardInput()))
            {
                using (var consoleWriter = new StreamWriter(Console.OpenStandardOutput()))
                {
                    consoleWriter.AutoFlush = true;
                    Console.SetOut(consoleWriter);
                    Console.SetIn(inputReader);

                    IInputHandler inputHandler = new InputHandler();

                    string inputTest = null;
                    Assert.AreEqual(inputHandler.IsInputValid(inputTest), false);

                    var boundaries    = new Rectangle(0, 0, Console.WindowWidth, Console.WindowHeight);
                    var shapeTemplate = new TreeTemplate('*', boundaries);

                    // Point is to test algorithm, so avoid using another algorithm to test with.
                    var    shape       = shapeTemplate.Print(1);
                    String checkString = " *\n";

                    Assert.AreEqual(shape, checkString);

                    shape        = shapeTemplate.Print(2);
                    checkString  = "  *\n";
                    checkString += " ***\n";

                    Assert.AreEqual(shape, checkString);

                    shape        = shapeTemplate.Print(5);
                    checkString  = "     *\n";
                    checkString += "    ***\n";
                    checkString += "   *****\n";
                    checkString += "  *******\n";
                    checkString += " *********\n";
                    Assert.AreEqual(shape, checkString);

                    shape        = shapeTemplate.Print(10);
                    checkString  = "          *\n";
                    checkString += "         ***\n";
                    checkString += "        *****\n";
                    checkString += "       *******\n";
                    checkString += "      *********\n";
                    checkString += "     ***********\n";
                    checkString += "    *************\n";
                    checkString += "   ***************\n";
                    checkString += "  *****************\n";
                    checkString += " *******************\n";
                    Assert.AreEqual(shape, checkString);

                    // Check the truncation message is displayed.
                    shape = shapeTemplate.Print(100000);
                    Assert.AreNotEqual(shape.IndexOf("The rest of this shape is too large to display properly."), 0);
                }
            }
        }
コード例 #7
0
        public static IEnumerable <TreeTemplate> TChildren(TreeTemplate template, IEnumerable <TreeTemplate> templateChildren)
        {
            var result = new List <TreeTemplate>()
            {
                template
            };

            result.AddRange(templateChildren);
            return(result);
        }
コード例 #8
0
        public static TreeTemplate Node(NodeInfo info)
        {
            var treeTemplate = new TreeTemplate(info.NodeType);

            if (info.NodeValue != null)
            {
                treeTemplate.Value = info.NodeValue;
            }
            return(treeTemplate);
        }
コード例 #9
0
        public static TreeTemplate Node(NodeInfo info)
        {
            var wrapped      = NodeBuilder.Create(info);
            var treeTemplate = new TreeTemplate(wrapped.GetType().Name + "Node");

            if (info.NodeValue != null)
            {
                treeTemplate.Value = info.NodeValue;
            }
            return(treeTemplate);
        }
コード例 #10
0
        public void MakeTree(Vector3 pos, TreeTemplate template)
        {
            int      i          = 0;
            Material wood       = materialManager["wood"];
            Material leaves     = materialManager["leaves"];
            float    coneHeight = template.Height / (float)template.ConeCount;
            Matrix4  rotZ       = Matrix4.CreateRotation(
                RenderStack.Math.Conversions.DegreesToRadians(90.0f),
                Vector3.UnitZ
                );
            float        cylHeight     = coneHeight;
            float        cylRadius     = template.Height / 20.0f;
            GeometryMesh cylinderMesh  = template.Meshes[i];
            Shape        cylinderShape = template.Shapes[i];

            ++i;
            Model rootModel = new Model("TreeRoot", cylinderMesh, wood, pos.X, pos.Y + cylHeight / 2.0f, pos.Z);

            AddModel(rootModel, null);
            Model below      = rootModel;
            float prevOffset = cylHeight / 2.0f;

            for (int c = 0; c < template.ConeCount; c++)
            {
                float topRadius      = (template.ConeCount - 1 - c) * template.Radius / (float)template.ConeCount;
                float bottomRadius   = topRadius + template.RadAdd;
                float R              = bottomRadius;
                float r              = topRadius;
                float fullConeHeight = (R * coneHeight) / (R - r);
                float minX           = -fullConeHeight / 3.0f;
                //float maxX = 2.0f * fullConeHeight / 3.0f;
                float        offset    = -minX;
                GeometryMesh coneMesh  = template.Meshes[i];
                Shape        coneShape = template.Shapes[i];
                ++i;
                Model coneModel = new Model("TreeCone", coneMesh, leaves, 0.0f, prevOffset + offset, 0.0f);
                coneModel.Frame.Parent = below.Frame;
                AddModel(coneModel, null);
                Bender bender = new Bender(Vector3.UnitZ, 0.1f, 2.0f);
                bender.Frame = coneModel.Frame;
                Add(bender);
                below      = coneModel;
                prevOffset = offset;
            }
        }
コード例 #11
0
ファイル: WitnessFunctions.cs プロジェクト: eglassman/refazer
        //[WitnessFunction("Relative", 1, DependsOnParameters = new[] { 0 }) ]
        //public static ExampleSpec WitnessRelativeK(GrammarRule rule, int parameter, ExampleSpec spec, ExampleSpec tokenSpec)
        //{
        //    var result = new Dictionary<State, object>();
        //    foreach (var input in spec.ProvidedInputs)
        //    {
        //        var example = spec.DisjunctiveExamples[input].First() as Tuple<PythonNode, PythonNode>;
        //        var template = (TreeTemplate)tokenSpec.Examples[input];
        //        if (example != null)
        //        {
        //            var parent = example.Item2;
        //            var target = example.Item1;
        //            var k = 0;
        //            var exampleK = 0;
        //            foreach (var child in parent.Children)
        //            {
        //                if (MatchRecursively(child, template, target, ref k))
        //                {
        //                    exampleK = k;
        //                }
        //            }
        //            if (exampleK != 0)
        //                result[input] = exampleK;
        //            else
        //            {
        //                return null;
        //            }
        //        }
        //        else
        //        {
        //            return null;
        //        }
        //    }
        //    return new ExampleSpec(result);
        //}        //[WitnessFunction("Relative", 1, DependsOnParameters = new[] { 0 }) ]
        //public static ExampleSpec WitnessRelativeK(GrammarRule rule, int parameter, ExampleSpec spec, ExampleSpec tokenSpec)
        //{
        //    var result = new Dictionary<State, object>();
        //    foreach (var input in spec.ProvidedInputs)
        //    {
        //        var example = spec.DisjunctiveExamples[input].First() as Tuple<PythonNode, PythonNode>;
        //        var template = (TreeTemplate)tokenSpec.Examples[input];
        //        if (example != null)
        //        {
        //            var parent = example.Item2;
        //            var target = example.Item1;
        //            var k = 0;
        //            var exampleK = 0;
        //            foreach (var child in parent.Children)
        //            {
        //                if (MatchRecursively(child, template, target, ref k))
        //                {
        //                    exampleK = k;
        //                }
        //            }
        //            if (exampleK != 0)
        //                result[input] = exampleK;
        //            else
        //            {
        //                return null;
        //            }
        //        }
        //        else
        //        {
        //            return null;
        //        }
        //    }
        //    return new ExampleSpec(result);
        //}

        private static bool MatchRecursively(PythonNode current, TreeTemplate pattern, PythonNode target, ref int i)
        {
            if (pattern.Match(current))
            {
                i++;
                if (current.Equals(target))
                {
                    return(true);
                }
            }
            foreach (var child in current.Children)
            {
                if (MatchRecursively(child, pattern, target, ref i))
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #12
0
        public static TreeTemplate Pattern(TreeTemplate token, IEnumerable <TreeTemplate> children)
        {
            TreeTemplate result;

            if (token is Wildcard)
            {
                result = new Wildcard(token.Type, token.Children);
            }
            else
            {
                result = new TreeTemplate(token.Type);
                if (token.Value != null)
                {
                    result.Value = token.Value;
                }
            }
            result.Children = children.ToList();
            return(result);
        }
コード例 #13
0
ファイル: WitnessFunctions.cs プロジェクト: eglassman/refazer
        public static ExampleSpec WitnessContextTemplate(GrammarRule rule, int parameter,
                                                         DisjunctiveExamplesSpec spec, ExampleSpec dSpec)
        {
            var result = new Dictionary <State, object>();

            foreach (var input in spec.ProvidedInputs)
            {
                var outerSpec = spec.DisjunctiveExamples[input] as IEnumerable <Tuple <int, PythonNode> >;
                var d         = (int)dSpec.Examples[input];
                var deepSpec  = (int)dSpec.Examples[input];
                if (outerSpec != null)
                {
                    var node = outerSpec.Where(e => e.Item1 == d).First().Item2;
                    result[input] = new TreeTemplate(GetAncestor(node, d), node);
                }
                else
                {
                    return(null);
                }
            }
            return(new ExampleSpec(result));
        }
コード例 #14
0
        //public static TreeTemplate Target(TreeTemplate template)
        //{
        //    TreeTemplate result;
        //    if (template is Wildcard)
        //    {
        //        result = new Wildcard(template.Type);
        //    }
        //    else
        //    {
        //        result = new TreeTemplate(template.Type);
        //        if (template.Value != null)
        //            result.Value = template.Value;
        //    }
        //    if (template.Children != null && template.Children.Any())
        //        result.Children = template.Children;
        //    result.Target = true;
        //    return result;
        //}


        //private static TreeTemplate TreeTemplate(TreeTemplate template)
        //{
        //    TreeTemplate result;
        //    if (template is Wildcard)
        //    {
        //        result = new Wildcard(template.Type);
        //    }
        //    else
        //    {
        //        result = new TreeTemplate(template.Type);
        //        if (template.Value != null)
        //            result.Value = template.Value;
        //    }
        //    if (template.Children != null && template.Children.Any())
        //        result.Children = template.Children;
        //    result.Target = template.Target;
        //    return result;
        //}

        public static TreeTemplate LeafPattern(TreeTemplate pattern)
        {
            return(pattern);
        }
コード例 #15
0
 public static Path Relative(TreeTemplate token, int k)
 {
     return(new Path(token, k));
 }
コード例 #16
0
        public static Pattern Context(int d, TreeTemplate pattern, Path path)
        {
            var treeTemplate = new Pattern(pattern, d, path);

            return(treeTemplate);
        }