public Environment(IFunctionEval functionEval, IDictionary <string, float> variables) { Variables = variables; FunctionEval = functionEval; }
public void Evaluate(int SpreadMax) { if (this.FPinInTerm.PinIsChanged || this.FInvalidate) { this.FAssembly = null; string term; this.FPinInTerm.GetString(0, out term); term = term == null ? String.Empty : term; CSharpCodeProvider csp = new CSharpCodeProvider(); ICodeCompiler cc = csp.CreateCompiler(); #region Build Code CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Data.dll"); cp.ReferencedAssemblies.Add("System.Xml.dll"); cp.ReferencedAssemblies.Add("mscorlib.dll"); cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\AdvExpr.dll"); cp.GenerateInMemory = true; cp.GenerateExecutable = false; cp.CompilerOptions = "/optimize"; //Header StringBuilder sb = new StringBuilder(); sb.AppendLine("using System;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("namespace VVVV.Lib"); sb.AppendLine("{"); sb.AppendLine("public class Evaluator : IFunctionEval"); sb.AppendLine("{"); sb.AppendLine("public double Eval(double[] values)"); sb.AppendLine("{"); for (int i = 0; i < this.FVarNames.Count; i++) { sb.AppendLine("double " + this.FVarNames[i] + " =values[" + i.ToString() + "];"); } sb.AppendLine(term); //Footer sb.AppendLine("}"); sb.AppendLine("}"); sb.AppendLine("}"); #endregion string code = sb.ToString(); CompilerResults cr = cc.CompileAssemblyFromSource(cp, code); if (cr.Errors.Count > 0) { this.FValid = false; string err = ""; foreach (CompilerError ce in cr.Errors) { err += ce.ErrorNumber + ":" + ce.ErrorText + "\n"; } this.FPinOutError.SetString(0, err); } else { this.FValid = true; this.FAssembly = cr.CompiledAssembly; this.FPinOutError.SetString(0, ""); foreach (Type t in this.FAssembly.GetTypes()) { if (typeof(IFunctionEval).IsAssignableFrom(t)) { object o = Activator.CreateInstance(t); this.FFormula = o as IFunctionEval; } } } this.FPinOutCode.SetString(0, code); this.FInvalidate = false; } this.FPinOutput.SliceCount = SpreadMax; if (this.FValid) { double *dbloutput; this.FPinOutput.GetValuePointer(out dbloutput); double[] vals = new double[this.FPinInVariables.Count]; List <IntPtr> pointers = new List <IntPtr>(); List <int> ptrcount = new List <int>(); for (int j = 0; j < this.FPinInVariables.Count; j++) { double *ptr; int cnt; this.FPinInVariables[j].GetValuePointer(out cnt, out ptr); pointers.Add(new IntPtr(ptr)); ptrcount.Add(cnt); } for (int i = 0; i < SpreadMax; i++) { for (int j = 0; j < this.FPinInVariables.Count; j++) { vals[j] = ((double *)pointers[j])[i % ptrcount[j]]; } dbloutput[i] = this.FFormula.Eval(vals); } } else { for (int i = 0; i < SpreadMax; i++) { this.FPinOutput.SetValue(i, 0); } } }
public void RegisterFunction(IFunctionEval func) { Functions.Add(func); }