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($".........................."); }
public T GetValue <T>(FoComponent context) { var found = context.Reference <FoProperty <T> >(MyName); return(found != null ? found.Value : default(T)); }
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($".........................."); }