Beispiel #1
0
        public static void test1()
        {
            IFoProperty[] props =
            {
                new FoProperty <string>("xxx",   "Stephen R. Strong"),
                new FoProperty <double>("cost",                  100),
                new FoProperty <double>("tax",                   .33),
                new FoProperty <double>("total", () => { return(1000); })
            };


            var comp1 = new FoComponent("Comp1");

            comp1.Properties.AddList(props);
            var cost  = comp1.Reference <FoProperty <double> >("cost");
            var tax   = comp1.Reference <FoProperty <double> >("tax");
            var total = comp1.Reference <FoProperty <double> >("total");

            total.Formula = () => { return(1000 * cost.Value * tax.Value); };


            Console.WriteLine($"==========================");
            Console.WriteLine($"Component: {comp1.MyName}");
            Console.WriteLine($"{comp1.toJson()}");
            Console.WriteLine($"..........................");

            cost.Value = 1;
            Console.WriteLine($"==========================");
            Console.WriteLine($"Component: {comp1.MyName}");
            Console.WriteLine($"{comp1.toJson()}");
            Console.WriteLine($"..........................");
        }
Beispiel #2
0
        public static void test2()
        {
            IFoProperty[] props =
            {
                new FoProperty <string>("proper name", "Stephen R. Strong"),
                new FoProperty <double>("cost",                        100),
                new FoProperty <double>("tax",                         .33),
                new FoProperty <double>("total",       () => { return(1000); })
            };

            IFoComponent[] comps =
            {
                new FoComponent("Comp1", props),
                new FoComponent("Comp2", props),
                new FoComponent("Comp3", props),
            };

            var root  = new FoComponent("Root", comps);
            var total = new FoProperty <double>("total cost", () => {
                double result = root.SumOver("cost");
                return(result);
            });

            root.Properties.Add(total);

            Console.WriteLine($"===========================");
            // Console.WriteLine($"{root.Properties.toJson()}");
            Console.WriteLine($"{root.toJson()}");
            Console.WriteLine($"..........................");

            root.saveToFile(@"data/test2.json");
        }
        public static void test1()
        {
            var prop1 = new FoProperty <string>("xxx", "Stephen R. Strong");

            prop1.toJson();

            var prop2a = new FoProperty <double>("cost", 100);

            prop2a.toJson();

            var prop2b = new FoProperty <double>("tax", .33);

            prop2b.toJson();

            Func <double> func   = () => { return(1000 * prop2a.Value * prop2b.Value); };
            var           prop2c = new FoProperty <double>("total", func);

            prop2c.toJson();

            string[] stuff = { "one", "two", "three", "5" };
            var      prop3 = new FoCollection <string>("count", stuff);

            double[] data  = { 1, 2, 3, 4, 5, 6, 7 };
            var      prop4 = new FoCollection <double>("number", data);
            var      prop5 = new FoCollection <double>("number");

            var comp1 = new FoComponent("my Comp");

            // Console.WriteLine($" to string {prop1}");
            // Console.WriteLine($" to string {prop2}");
            // Console.WriteLine($" to string {prop3}");
            // Console.WriteLine($" to string {prop4}");
            // Console.WriteLine($" to string {prop5}");

            IFoProperty[] props = { prop1, prop2a, prop2b, prop2c };
            comp1.Properties.AddList(props);

            Console.WriteLine($" comp {comp1}");

            comp1.toJson();

            prop2b.Value = .505;

            comp1.toJson();

            prop2c.Formula = () => { return(prop2a.Value * prop2b.Value); };

            comp1.toJson();


            //prop4.asJson(prop4);
            //prop5.asJson(prop5);
        }
        public static void test1()
        {
            var root = new FoComponent("Component");

            var parser = new Parser("1.0 + 2 * (3 - 55.5 )");

            var formula = parser.ReadFormula();
            var json    = formula.toJson();

            Console.WriteLine($"===========================");
            Console.WriteLine($"{json}");
            //Console.WriteLine($"{root.AsJson()}");
            Console.WriteLine($"..........................");
        }
Beispiel #5
0
        public static void test4()
        {
            var body = JSONExtensions.readFromFile(@"data/create_root.json");
            var name = body.GetProperty("MyName");
            var root = new FoComponent(name.ToString());

            root.saveToFile(@"data/test4.json");

            var handle = Activator.CreateInstance(root.GetType()) as FoComponent;

            handle.MyName = "ffrfrf";

            Console.WriteLine($"{handle.toJson()}");
            Console.WriteLine($"===========================");
            Console.WriteLine($"{root.toJson()}");
            Console.WriteLine($"..........................");
        }
Beispiel #6
0
        public static void test3()
        {
            IFoProperty[] props =
            {
                new FoProperty <string>("proper name", "Stephen R. Strong"),
            };


            var root = new FoComponent("Root", props);

            Console.WriteLine($"===========================");
            // Console.WriteLine($"{root.Properties.toJson()}");
            Console.WriteLine($"{root.toJson()}");
            Console.WriteLine($"..........................");

            root.saveToFile(@"data/test3.json");
        }
Beispiel #7
0
        public bool getvalue(FoComponent root)
        {
            var depthProp  = new FoReference("depth");
            var depth      = depthProp.GetValue <double>(root);
            var widthProp  = new FoReference("width");
            var width      = widthProp.GetValue <double>(root);
            var heightProp = new FoReference("height");
            var height     = heightProp.GetValue <double>(root);
            var volumeProp = new FoReference("volume");
            var volume     = volumeProp.GetValue <double>(root);
            var areaProp   = new FoReference("area");
            var area       = areaProp.GetValue <double>(root);

            var result = depth * (width * height) == volume && width * height == area;

            return(result);
        }
Beispiel #8
0
        public T GetValue <T>(FoComponent context)
        {
            var found = context.Reference <FoProperty <T> >(MyName);

            return(found != null ? found.Value : default(T));
        }
Beispiel #9
0
        public static void test2()
        {
            var root = new FoComponent("Component");

            IFoProperty[] props =
            {
                new FoProperty <double>("width",   10),
                new FoProperty <double>("height", .33),
                new FoProperty <double>("depth",   22),
                new FoProperty <double>("area",   () => {
                    var width  = new FoReference("width");
                    var height = new FoReference("height");
                    var depth  = new FoReference("depth");
                    var area   = width.GetValue <double>(root) * height.GetValue <double>(root);
                    var volume = area * depth.GetValue <double>(root);
                    return(area);
                }),
                new FoProperty <double>("volume", () => {
                    var area   = new FoReference("area");
                    var depth  = new FoReference("depth");
                    var volume = area.GetValue <double>(root) * depth.GetValue <double>(root);
                    return(volume);
                })
            };

            root.Properties.AddList(props);

            var parser = new Parser("depth * (width * height) == volume and width * height == area");

            var formula = parser.ReadFormula();

            var list = new List <Operator>();

            formula.CollectionAll(list);

            var rawData = list
                          .GroupBy(item => item.Name)
                          .Select(item => item.First())
                          .Where(item => item is ReferenceOperator)
                          .Select(item => $"{((ReferenceOperator)item).ToReferenceStatement()} ")
                          .ToList <string>();

            var data = String.Join("\n", rawData);

            var json      = formula.toJson();
            var cSharp    = formula.AsCSharp();
            var decompile = formula.Decompile();

            string code = $"{data}\n\nvar result = {cSharp};\n\n return result;";
            // Console.WriteLine($"code: {code}");


            string nameSpace  = "Apprentice";
            string className  = "Compute";
            string typeName   = "bool";
            string methodName = "ComputeValue";
            string args       = "(FoComponent root)";

            var trans = new Transpiler();
            var body  = trans.WrapInClassMethod(nameSpace, className, typeName, methodName, args, code);

            Console.WriteLine($"===========================");

            //Console.WriteLine($"BODY: {body}");

            Assembly assembly;
            var      compile = trans.Compile(body);

            if (trans.LoadAssembly(compile, out assembly))
            {
                string target = $"{nameSpace}.{className}";
                Type   type   = assembly.GetType(target);
                var    obj    = Activator.CreateInstance(type);
                var    thanks = type.InvokeMember(methodName,
                                                  BindingFlags.Default | BindingFlags.InvokeMethod,
                                                  null,
                                                  obj,
                                                  new object[] { root });

                Console.WriteLine($"It Works if the answer is true");
                Console.WriteLine($"{methodName} => {thanks}");
                var volume = root.Reference <FoProperty <double> >("volume").Value;
                Console.WriteLine($"Volume => {volume}");
                var area = root.Reference <FoProperty <double> >("area").Value;
                Console.WriteLine($"Area => {area}");
            }

            Console.WriteLine($"..........................");
        }