public CompileResult(string classNamespace, string className, string methodName, CodeCompileUnit code, List <IPropertyPointer> pointers, VariableList vars, AssemblyRefList importedAssemblies) { ClassNamespace = classNamespace; ClassName = className; MethodName = methodName; CodeUnit = code; PropertyPointers = pointers; Variables = vars; Imports = importedAssemblies; }
public bool ContainsID(UInt32 id) { if (mathExp != null) { VariableList vs = mathExp.FindAllInputVariables(); for (int i = 0; i < vs.Count; i++) { if (vs[i] != null) { if (vs[i].ID == id) { return(true); } } } } return(false); }
public VariableMap CloneExp(MathNodeRoot root) { VariableList vlist = root.FindAllInputVariables(); VariableMap obj = new VariableMap(); foreach (KeyValuePair <IVariable, ICompileableItem> kv in this) { IVariable v = vlist.FindMatchingPublicVariable(kv.Key); if (v == null) { throw new MathException("Variable not found for {0}", kv.Key); } //at this time, assume the root alread get the right clone owner kv.Value.SetCloneOwner(root.ActionContext); ICompileableItem item = (ICompileableItem)kv.Value.Clone(); obj.Add(v, item); } return(obj); }
/// <summary> /// new load or re-load. /// this control must already be added to the viewer (DiagramViewer). /// It generates ports for all variables /// </summary> /// <param name="data">the result of lower level editing</param> public void LoadData(IMathExpression data) { bLoading = true; DiagramViewer dv = this.Parent as DiagramViewer; VariableList currentVariables = null; IVariable currOutVar = null; if (mathExp != null) { currOutVar = mathExp.OutputVariable; currentVariables = mathExp.InputVariables; } mathExp = data; if (mathExp == null) { mathExp = new MathNodeRoot(); } mathExp.ClearFocus(); //1. find out all unique and non-local variables VariableList _portVariables = mathExp.InputVariables; //2. remove removed ports if (currentVariables != null) { foreach (IVariable v in currentVariables) { if (_portVariables.GetVariableById(v.ID) == null) { //remove port and nodes List <Control> cs = new List <Control>(); foreach (Control c in dv.Controls) { LinkLineNodeInPort p = c as LinkLineNodeInPort; if (p != null) { if (p.PortID == v.ID) { cs.Add(p); cs.Add(p.Label); ILinkLineNode prev = p.PrevNode; while (prev != null) { if (prev is LinkLineNodePort) { break; } cs.Add((Control)prev); prev = prev.PrevNode; } break; } } } foreach (Control c in cs) { dv.Controls.Remove(c); } } } } List <Control> newControls = new List <Control>(); List <LinkLineNodeInPort> newInPorts = new List <LinkLineNodeInPort>(); //3.create new ports foreach (IVariable v in _portVariables) { if (currentVariables == null || currentVariables.GetVariableById(v.ID) == null) { if (data.IsContainer) { //create a new variable instance MathNodeRoot r = new MathNodeRoot(); ((MathNode)v).root.CopyAttributesToTarget(r); r[1] = new MathNodeVariable(r); IVariable vi = (IVariable)r[1]; vi.VariableType = (RaisDataType)v.VariableType.Clone(); vi.VariableName = v.VariableName; vi.SubscriptName = v.SubscriptName; vi.ResetID(v.ID); vi.InPort = new LinkLineNodeInPort(vi); vi.InPort.SetPortOwner(vi); vi.InPort.Owner = this; newControls.Add(vi.InPort); newControls.Add(vi.InPort.Label); vi.InPort.CheckCreatePreviousNode(); newControls.Add((Control)vi.InPort.PrevNode); newInPorts.Add(vi.InPort); } else { v.InPort = new LinkLineNodeInPort(v); v.InPort.Owner = this; newControls.Add(v.InPort); newControls.Add(v.InPort.Label); v.InPort.CheckCreatePreviousNode(); newControls.Add((Control)v.InPort.PrevNode); newInPorts.Add(v.InPort); } } } if (newInPorts.Count > 0) { int dn = this.Width / (newInPorts.Count + 1); for (int i = 0; i < newInPorts.Count; i++) { newInPorts[i].Position = i * dn + dn; newInPorts[i].Left = this.Left + newInPorts[i].Position; newInPorts[i].SaveLocation(); newInPorts[i].PrevNode.Left = newInPorts[i].Left; } } //3. re-map to existing ports if (currOutVar != null && currOutVar.OutPorts != null) { //if port exists then re-use it mathExp.OutputVariable.OutPorts = currOutVar.OutPorts; for (int i = 0; i < mathExp.OutputVariable.OutPorts.Length; i++) { mathExp.OutputVariable.OutPorts[i].SetPortOwner(mathExp.OutputVariable); //if linking exists then re-establish it if (currOutVar.OutPorts[i].LinkedPortID != 0) { //find the variable if (dv != null) { IVariable v = dv.FindVariableById(currOutVar.OutPorts[i].LinkedPortID); if (v != null) { v.InPort.LinkedPortID = mathExp.OutputVariable.ID; } } } } } else { //for a new load, create the output port mathExp.OutputVariable.OutPorts = new LinkLineNodeOutPort[] { new LinkLineNodeOutPort(mathExp.OutputVariable) }; mathExp.OutputVariable.OutPorts[0].CheckCreateNextNode(); ////use the default position mathExp.OutputVariable.OutPorts[0].Position = this.Width / 2; } for (int i = 0; i < mathExp.OutputVariable.OutPorts.Length; i++) { mathExp.OutputVariable.OutPorts[i].Owner = this; mathExp.OutputVariable.OutPorts[i].Label.Visible = false; mathExp.OutputVariable.OutPorts[i].SaveLocation(); } if (!(currOutVar != null && currOutVar.OutPorts != null)) { for (int i = 0; i < mathExp.OutputVariable.OutPorts.Length; i++) { //for a new load, set default postion for the empty linking node ((Control)(mathExp.OutputVariable.OutPorts[i].NextNode)).Location = mathExp.OutputVariable.OutPorts[i].DefaultNextNodePosition(); //add new controls newControls.Add(mathExp.OutputVariable.OutPorts[i]); newControls.Add((Control)mathExp.OutputVariable.OutPorts[i].NextNode); newControls.Add(mathExp.OutputVariable.OutPorts[i].Label); } } //add the new controls if (this.Parent != null && newControls.Count > 0) { Control[] a = new Control[newControls.Count]; newControls.CopyTo(a); this.Parent.Controls.AddRange(a); } mathExp.PrepareDrawInDiagram(); // createImage(); bLoading = false; }
public void AddMethod(CodeTypeDeclaration t, CodeNamespace ns, CodeMemberMethod testMethod, AssemblyRefList imports, VariableList parameters, List <IPropertyPointer> pointers) { DiagramDesignerHolder.CreateTestMethod(mathExpCtrl1.Root, t, ns, testMethod, imports, parameters, pointers); }
public void AddMethod(CodeTypeDeclaration t, CodeNamespace ns, CodeMemberMethod m, AssemblyRefList imports, VariableList parameters, List <IPropertyPointer> pointerList) { CreateTestMethod(result, t, ns, m, imports, parameters, pointerList); }
public static void CreateTestMethod(IMathExpression result, CodeTypeDeclaration t, CodeNamespace ns, CodeMemberMethod m, AssemblyRefList imports, VariableList parameters, List <IPropertyPointer> pointerList) { result.GetAllImports(imports); // m.ReturnType = new CodeTypeReference(result.DataType.Type); // m.Comments.Add(new CodeCommentStatement("Variable mapping:")); m.Comments.Add(new CodeCommentStatement("In formula: method parameter")); // MethodType mt = new MethodType(); mt.MethodCode = m; result.GenerateInputVariables(); VariableList variables = result.InputVariables; Dictionary <string, IPropertyPointer> pointers = new Dictionary <string, IPropertyPointer>(); result.GetPointers(pointers); int n = variables.Count; MathNode.Trace("Generate arguments from {0} input variables", n); MathNode.IndentIncrement(); for (int k = 0; k < n; k++) { IVariable var = variables[k]; MathNode.Trace(k, var); if (!(var is MathNodeVariableDummy) && !var.IsParam && !var.IsConst) { string paramName = ""; parameters.Add(var); paramName = var.CodeVariableName; CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(new CodeTypeReference(var.VariableType.Type), paramName); m.Parameters.Add(p); //add comment string sub = var.SubscriptName; if (string.IsNullOrEmpty(sub)) { sub = " "; } m.Comments.Add(new CodeCommentStatement(string.Format("{0}{1}:\t {2}", var.VariableName, sub, var.CodeVariableName))); MathNode.IndentIncrement(); MathNode.Trace("Argument {0} {1} for {2}, {3}", var.VariableType.Type, paramName, var.TraceInfo, var.GetType()); MathNode.IndentDecrement(); // result.AssignCodeExp(new CodeArgumentReferenceExpression(paramName), var.CodeVariableName); } } MathNode.Trace("Generate arguments from {0} pointers", pointers.Count); foreach (KeyValuePair <string, IPropertyPointer> kv in pointers) { string paramName = ""; pointerList.Add(kv.Value); paramName = kv.Value.CodeName; CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(new CodeTypeReference(kv.Value.ObjectType), paramName); m.Parameters.Add(p); //add comment m.Comments.Add(new CodeCommentStatement(string.Format("{0}:\t {1}", kv.Value.ToString(), paramName))); MathNode.IndentIncrement(); MathNode.Trace("Argument {0} {1} for {2}", kv.Value.ObjectType, paramName, kv.Value.ToString()); MathNode.IndentDecrement(); } MathNode.IndentDecrement(); //do the compiling CodeExpression ce = result.ReturnCodeExpression(mt); // MathNode.Trace("Test method returns {0}, compiled type: {1}", result.DataType.Type, result.ActualCompileDataType.Type); if (result.ActualCompileDataType.Type.Equals(result.DataType.Type)) { CodeMethodReturnStatement mr = new CodeMethodReturnStatement(ce); m.Statements.Add(mr); } else { if (result.ActualCompileDataType.IsVoid) { m.Statements.Add(new CodeExpressionStatement(ce)); CodeMethodReturnStatement mr = new CodeMethodReturnStatement(ValueTypeUtil.GetDefaultValueByType(result.DataType.Type)); m.Statements.Add(mr); } else { if (result.DataType.IsVoid) { m.Statements.Add(new CodeExpressionStatement(ce)); } else { if (result.DataType.Type.Equals(typeof(string))) { CodeMethodReturnStatement mr = new CodeMethodReturnStatement(new CodeMethodInvokeExpression(ce, "ToString", new CodeExpression[] { })); m.Statements.Add(mr); } else { CodeExpression mie = RaisDataType.GetConversionCode(result.ActualCompileDataType, ce, result.DataType, m.Statements); if (mie != null) { CodeMethodReturnStatement mr = new CodeMethodReturnStatement(mie); m.Statements.Add(mr); } } } } } }