public override void ExportPhpScriptCodeStatements(StringCollection method) { MathNode.Trace("ExportPhpScriptCodeStatements for {0}", this.GetType()); //0: start //1: end //2: function //3: dx IsParam //4: sum IsLocal //5: summing index IsLocal IsParam base.ExportPhpScriptCodeStatements(method); //assign code expression to all x in the function OnPreparePhpScriptVariable(method); MathNodeVariable.DeclarePhpScriptVariable(method, (IVariable)this[4]); string sum = ((IVariable)this[4]).CodeVariableName; method.Add(MathNode.FormString("{0}=0;\r\n", sum)); string c5 = this.GetParameterCodePhp(method, 5); string f1 = MathNode.FormString("for({0}=1;{1}<={2}-1;({1})++)\r\n{\r\n"); method.Add(f1); method.Add(MathNode.FormString("\t{0}={0} + {1};\r\n", sum, this.GetParameterCodePhp(method, 2))); method.Add("}\r\n"); //clear code expression to all x in the function this[2].AssignPhpScriptCodeExp(null, ((MathNodeVariable)this[3]).CodeVariableName); }
public override bool Update(string newValue) { if (!string.IsNullOrEmpty(newValue)) { if (Char.IsLetter(newValue, 0)) { MathNodeVariable node = new MathNodeVariable(this.Parent); node.VariableName = newValue; return(this.ReplaceMe(node)); } else { try { double d = Convert.ToDouble(newValue); _value = d; _placeHolder = false; return(true); } catch { } } } return(false); }
public override void ExportCodeStatements(IMethodCompile method) { MathNode.Trace("ExportCodeStatements for {0}", this.GetType()); //0: start //1: end //2: function //3: dx IsParam //4: sum IsLocal //5: summing index IsLocal IsParam base.ExportCodeStatements(method); //assign code expression to all x in the function OnPrepareVariable(method); MathNodeVariable.DeclareVariable(method.MethodCode.Statements, (IVariable)this[4]); CodeVariableReferenceExpression sum = new CodeVariableReferenceExpression(((IVariable)this[4]).CodeVariableName); method.MethodCode.Statements.Add(new CodeAssignStatement(sum, new CodePrimitiveExpression(0))); CodeExpression c5 = this.GetParameterCode(method, 5); CodeIterationStatement cis = new CodeIterationStatement( new CodeVariableDeclarationStatement(this[5].DataType.Type, ((IVariable)this[5]).CodeVariableName, new CodePrimitiveExpression(1)), new CodeBinaryOperatorExpression(c5, CodeBinaryOperatorType.LessThanOrEqual, new CodePrimitiveExpression(_intervals - 1)), new CodeAssignStatement(c5, new CodeBinaryOperatorExpression(c5, CodeBinaryOperatorType.Add, new CodePrimitiveExpression(1))), new CodeStatement[] { new CodeAssignStatement(sum, new CodeBinaryOperatorExpression(sum, CodeBinaryOperatorType.Add, this.GetParameterCode(method, 2))) }); method.MethodCode.Statements.Add(cis); //clear code expression to all x in the function this[2].AssignCodeExp(null, ((MathNodeVariable)this[3]).CodeVariableName); }
public override MathNode CreateDefaultNode(int i) { if (i == 0 || i == 1) { MathNode n = base.CreateDefaultNode(i); n.IsSuperscript = true; return(n); } if (i == 2) { return(base.CreateDefaultNode(i)); } MathNodeVariable v = new MathNodeVariable(this); if (i == 3 || i == 5) { v.IsParam = true; } if (i == 4 || i == 5) //not displayed { v.IsLocal = true; v.VariableName = "l" + v.ID.ToString("x"); } if (i == 5) { v.NoAutoDeclare = true; } return(v); }
/// <summary> /// /// </summary> /// <param name="newValue"></param> /// <returns>true:append the key strokes in the buffer; false:discard the key strokes after processing</returns> public override bool Update(string newValue) { if (!string.IsNullOrEmpty(newValue)) { if (Char.IsLetter(newValue, 0)) { MathNodeVariable node = new MathNodeVariable(this.Parent); node.VariableName = newValue; return(this.ReplaceMe(node)); } else { try { TypeConverter converter = TypeDescriptor.GetConverter(_dataType.LibType); if (converter != null) { if (converter.CanConvertFrom(typeof(string))) { object obj = converter.ConvertFromInvariantString(newValue); _value = obj; return(true); } } } catch { } } } return(false); }
protected override void OnCloneDataType(MathNode cloned) { if (_dataType != null) { MathNodeVariable m = (MathNodeVariable)cloned; m._dataType = _dataType.Clone() as RaisDataType; } }
public static void SetNewNodeID(XmlNode node) { UInt32 id = GetAttributeUInt(node, XMLATT_ID); if (id == 0) { id = MathNodeVariable.GetNewID(); SetAttribute(node, XMLATT_ID, id.ToString()); } }
public override string CreatePhpScript(StringCollection method) { MathNode.Trace("{0}.CreatePhpScript", this.GetType().Name); MathNodeVariable var = (MathNodeVariable)this[0]; var.VariableType = this.DataType; string e = this[0].CreatePhpScript(method); return(string.Format(CultureInfo.InvariantCulture, "({0})++", e)); }
protected override void OnLoaded() { if (!(this[1] is IVariable)) { this[1] = new MathNodeVariable(this); } ((IVariable)this[1]).IsLocal = true; ((IVariable)this[1]).IsParam = true; ((IVariable)this[1]).NoAutoDeclare = true; if (!(this[4] is IVariable)) { this[4] = new MathNodeVariable(this); } ((IVariable)this[4]).IsLocal = true; ((IVariable)this[4]).IsParam = true; }
public override MathNode CreateDefaultNode(int i) { if (i == 0) { return(new MathNodeCondition(this)); } int n = ChildNodeCount; if (i == n - 1) { MathNodeVariable v = new MathNodeVariable(this); v.IsLocal = true; v.VariableName = string.Format(CultureInfo.InvariantCulture, "s{0}", Guid.NewGuid().GetHashCode().ToString("x", CultureInfo.InvariantCulture)); return(v); } return(base.CreateDefaultNode(i)); }
public override object CloneExp(MathNode parent) { MathNodeVariable node = (MathNodeVariable)base.CloneExp(parent); node.VariableName = _value; node.SubscriptName = _subscript; node._id = ID; node.IsLocal = IsLocal; node.IsParam = IsParam; node.IsReturn = IsReturn; node._passin = _passin; if (_subscriptFont != null) { node._subscriptFont = (Font)_subscriptFont.Clone(); } node.IsSuperscript = IsSuperscript; node.Position = new Point(Position.X, Position.Y); if (VariableType == null) { throw new MathException("VariableType is null when clone it. {0}", _value); } else { node.VariableType = (RaisDataType)VariableType.Clone(); } if (ClonePorts) { if (_outports != null) { LinkLineNodeOutPort[] ports = new LinkLineNodeOutPort[_outports.Length]; for (int i = 0; i < ports.Length; i++) { _outports[i].ConstructorParameters = new object[] { node }; ports[i] = (LinkLineNodeOutPort)_outports[i].Clone(); } node.OutPorts = ports; } if (_inport != null) { _inport.ConstructorParameters = new object[] { node }; node.InPort = (LinkLineNodeInPort)_inport.Clone(); } } return(node); }
public override MathNode CreateDefaultNode(int i) { if (i == 1 || i == 4) { MathNodeVariable v = new MathNodeVariable(this); v.IsLocal = true; v.IsParam = true; if (i == 1) { v.VariableName = "n"; v.NoAutoDeclare = true; } else { v.VariableName = "sum"; } return(v); } return(base.CreateDefaultNode(i)); }
protected override void OnLoaded() { this[0].IsSuperscript = true; this[1].IsSuperscript = true; if (!(this[3] is IVariable)) { this[3] = new MathNodeVariable(this); } ((IVariable)this[3]).IsParam = true; if (!(this[4] is IVariable)) { this[4] = new MathNodeVariable(this); } ((IVariable)this[4]).IsLocal = true; if (!(this[5] is IVariable)) { this[5] = new MathNodeVariable(this); } ((IVariable)this[5]).IsLocal = true; ((IVariable)this[5]).IsParam = true; ((IVariable)this[5]).NoAutoDeclare = true; }
protected override void OnLoaded() { int n = ChildNodeCount; if (n < 2) { ChildNodeCount = 2; } if (!(this[n - 1] is IVariable)) { this[n - 1] = new MathNodeVariable(this); ((IVariable)this[n - 1]).IsLocal = true; } ((IVariable)this[n - 1]).IsLocal = true; ((IVariable)this[n - 1]).VariableName = string.Format(CultureInfo.InvariantCulture, "s{0}", Guid.NewGuid().GetHashCode().ToString("x", CultureInfo.InvariantCulture)); for (int i = 0; i < n - 2; i++) { if (!(this[i] is MathNodeCondition)) { this[i] = new MathNodeCondition(this); } } }
/// <summary> /// generate code and put final result to n-th node /// </summary> /// <returns></returns> public override void ExportCodeStatements(IMethodCompile method) { int n = Branches; MathNode.Trace("ExportCodeStatements for {0}, branches: {1}", this.GetType(), n); IVariable v = (IVariable)this[n + 1]; string resultName = v.CodeVariableName; v.VariableType = this.ResultType; OnPrepareVariable(method); MathNode.Trace("result is varibale {0}", resultName); CodeVariableReferenceExpression result = new CodeVariableReferenceExpression(resultName); MathNodeVariable.DeclareVariable(method.MethodCode.Statements, v); MathNode.Trace("default value: {0}", this[n].ToString()); CodeExpression eDefault = this[n].ExportCode(method); if (!this[n].DataType.IsSameType(this.DataType)) { MathNode.Trace("last code conversion from default value of {0} to {1}", this[n].DataType, this.DataType); eDefault = convert(eDefault); } CodeAssignStatement stDefault = new CodeAssignStatement(result, eDefault); if (n == 0) { MathNode.Trace("no condition given"); method.MethodCode.Statements.Add(stDefault); } else { CodeExpression e = this[0][1].ExportCode(method); if (!this[0][1].DataType.IsSameType(this.DataType)) { e = convert(e); } CodeConditionStatement ccs = new CodeConditionStatement(); CodeExpression ce = this[0][0].ExportCode(method); if (!(this[0][0].DataType.Type.Equals(typeof(bool)))) { ce = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(Convert)), "ToBoolean", new CodeExpression[] { ce }); } ccs.Condition = ce; bool isSameVariable = false; if (e is CodeVariableReferenceExpression) { if (string.CompareOrdinal(((CodeVariableReferenceExpression)e).VariableName, resultName) == 0) { isSameVariable = true; } } if (!isSameVariable) { ccs.TrueStatements.Add(new CodeAssignStatement(result, e)); } method.MethodCode.Statements.Add(ccs); for (int i = 1; i < n; i++) { CodeConditionStatement ccs1 = new CodeConditionStatement(); ccs.FalseStatements.Add(ccs1); ccs = ccs1; e = this[i][1].ExportCode(method); if (!this[i][1].DataType.IsSameType(this.DataType)) { e = convert(e); } ccs.TrueStatements.Add(new CodeAssignStatement(result, e)); ce = this[i][0].ExportCode(method); if (!(this[i][0].DataType.Type.Equals(typeof(bool)))) { ce = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(typeof(Convert)), "ToBoolean", new CodeExpression[] { ce }); } ccs.Condition = ce; } ccs.FalseStatements.Add(stDefault); } }
/// <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 override void ExportPhpScriptCodeStatements(StringCollection method) { int n = Branches; MathNode.Trace("ExportPhpScriptCodeStatements for {0}, branches: {1}", this.GetType(), n); string resultName = ((IVariable)this[n + 1]).CodeVariableName; OnPreparePhpScriptVariable(method); MathNode.Trace("result is varibale {0}", resultName); string result = resultName; MathNodeVariable.DeclarePhpScriptVariable(method, (IVariable)this[n + 1]); MathNode.Trace("default value: {0}", this[n].ToString()); this[n].ExportPhpScriptCodeStatements(method); string eDefault = this[n].CreatePhpScript(method); string stDefault = MathNode.FormString("{0}={1};\r\n", result, eDefault); if (n == 0) { MathNode.Trace("no condition given"); method.Add(stDefault); } else { string e = this[0][1].CreatePhpScript(method); string ce = this[0][0].CreatePhpScript(method); bool isSameVariable = false; IVariable ve = this[0][0] as IVariable; if (ve != null) { if (string.CompareOrdinal(ve.VariableName, resultName) == 0) { isSameVariable = true; } } StringBuilder trueStatements = new StringBuilder(); StringBuilder falseStatements = new StringBuilder(); if (!isSameVariable) { trueStatements.Append(MathNode.FormString("{0}={1};\r\n", result, e)); } int indent = 0; string tabs = string.Empty; method.Add(MathNode.FormString("if({0}) {{\r\n\t", ce)); method.Add(trueStatements.ToString()); method.Add("}\r\n"); for (int i = 1; i < n; i++) { e = this[i][1].CreatePhpScript(method); ce = this[i][0].CreatePhpScript(method); method.Add(tabs); method.Add("else {\r\n"); indent++; tabs = new string('\t', indent); method.Add(tabs); method.Add(MathNode.FormString("if({0}) {{\r\n", ce)); method.Add(tabs); method.Add(MathNode.FormString("\t{0}={1};\r\n", result, e)); method.Add(tabs); method.Add("}\r\n"); } method.Add(tabs); method.Add("else {\r\n\t"); method.Add(tabs); method.Add(stDefault); method.Add("\r\n"); method.Add(tabs); method.Add("}\r\n"); for (int i = 1; i < n; i++) { indent--; tabs = new string('\t', indent); method.Add(tabs); method.Add("}\r\n"); } } }