public IExpressionType ExpressionImportFromFormatToModel(IFormat expression) { List <IExpressionType> operands = new List <IExpressionType>(); Context ctx = Context.getInstance(); if (expression.getOperands() != null) { foreach (var operand in expression.getOperands()) { operands.Add(ExpressionImportFromFormatToModel(operand)); } } switch (expression.getType()) { case Types.Variable: Variable newVar = new Variable(expression.getValue()); ctx.addVariable(newVar.getValue(), ctx.getCurrPath(), null); return(newVar); case Types.Number: return(new Number(Convert.ToDouble(expression.getValue()))); case Types.VarDefinition: Equation ne = new Equation(operands, false); ctx.changeVariable(((Variable)operands[0]).getValue(), ctx.getCurrPath(), ne); return(ne); case Types.SuspendedVarDefinition: Equation nde = new Equation(operands, true); ctx.changeVariable(((Variable)operands[0]).getValue(), ctx.getCurrPath(), nde); return(nde); case Types.List: return(new MathList(operands)); case Types.Interval: return(new Interval(operands)); case Types.FuncExpression: return(new Function(expression.getType(), operands, expression.getOptions().getValue())); default: return(new Function(expression.getType(), operands, "")); } }
private void toXml(XmlElement root, XmlDocument doc, IFormat el) { switch (el.getType()) { case Types.VarDefinition: { XmlElement elem = doc.CreateElement("Equation"); for (int i = 0; i < el.getOperands().Count; i++) { toXml(elem, doc, el.getOperands()[i]); } root.AppendChild(elem); break; } case Types.Addition: { XmlElement elem = doc.CreateElement("Sum"); for (int i = 0; i < el.getOperands().Count; i++) { toXml(elem, doc, el.getOperands()[i]); } root.AppendChild(elem); break; } case Types.Multiplication: { XmlElement elem = doc.CreateElement("Mul"); for (int i = 0; i < el.getOperands().Count; i++) { toXml(elem, doc, el.getOperands()[i]); } root.AppendChild(elem); break; } case Types.Fraction: { XmlElement elem = doc.CreateElement("Fraction"); XmlElement numerator = doc.CreateElement("Numerator"); toXml(numerator, doc, el.getOperands()[0]); XmlElement denumerator = doc.CreateElement("Denumerator"); toXml(denumerator, doc, el.getOperands()[1]); elem.AppendChild(numerator); elem.AppendChild(denumerator); root.AppendChild(elem); break; } case Types.Power: { XmlElement elem = doc.CreateElement("Power"); XmlElement numerator = doc.CreateElement("Base"); toXml(numerator, doc, el.getOperands()[0]); XmlElement denumerator = doc.CreateElement("Value"); toXml(denumerator, doc, el.getOperands()[1]); elem.AppendChild(numerator); elem.AppendChild(denumerator); root.AppendChild(elem); break; } case Types.Root: { XmlElement elem = doc.CreateElement("Sqrt"); XmlElement numerator = doc.CreateElement("Base"); toXml(numerator, doc, el.getOperands()[0]); XmlElement denumerator = doc.CreateElement("Value"); toXml(denumerator, doc, el.getOperands()[1]); elem.AppendChild(numerator); elem.AppendChild(denumerator); root.AppendChild(elem); break; } case Types.Number: case Types.Variable: { XmlElement elem = doc.CreateElement("Operand"); elem.InnerText = el.getValue(); root.AppendChild(elem); break; } case Types.FuncDefinition: /*XmlElement elem1 = doc.CreateElement(el.getOperands()[0].getValue()); * for( int i=1; i< el.getOperands().Count; i++) * { * toXml(elem1,doc,el.getOperands()[i]); * } * root.AppendChild(elem1);*/ break; case Types.FuncExpression: XmlElement elem2 = doc.CreateElement(el.getOperands()[0].getValue()); for (int i = 1; i < el.getOperands().Count; i++) { toXml(elem2, doc, el.getOperands()[i]); } root.AppendChild(elem2); break; default: throw new Exception(); } }