/// <summary> /// Constructor /// </summary> /// <param name="creator">Base formula creator</param> public FormulaArrayObjectCreator(IFormulaObjectCreator creator) { this.creator = creator; for (int i = 0; i < creator.BinaryCount; i++) { binary.Add(new BinaryArrayOperationDetector(creator.GetBinaryDetector(i))); } for (int i = 0; i < creator.MultiOperationCount; i++) { multi.Add(new MultiArrayOperationDetector(creator.GetMultiOperationDetector(i))); } for (int i = 0; i < creator.OperationCount; i++) { IOperationDetector d = creator.GetDetector(i); TypeInfo dt = d.ToTypeInfo(); TreeTransformationAttribute da = dt.GetAttribute <TreeTransformationAttribute>(); if (da != null) { detectors.Add(d); goto detectorSelected; } detectors.Add(new ArrayOperationDetector(d)); detectorSelected: continue; } detectors.Add(ArraySingleDetector.Object); }
/// <summary> /// Processes binary operation /// </summary> /// <param name="formula">Input formula</param> /// <param name="creator">Creator of tree</param> protected bool processBinary(MathFormula formula, IFormulaObjectCreator creator) { Dictionary <int, ObjectFormulaTree[]> dic = new Dictionary <int, ObjectFormulaTree[]>(); for (int i = 0; i < creator.BinaryCount; i++) { IBinaryDetector detector = creator.GetBinaryDetector(i); ObjectFormulaTree tA = null; ObjectFormulaTree tB = null; if (detector.AssociationDirection == BinaryAssociationDirection.LeftRight) { int m = 0; for (int j = 0; j < formula.Count - 1; j++) { MathSymbol symbol = formula[j]; if (creator.IsBra(symbol)) { ++m; continue; } if (creator.IsKet(symbol)) { --m; continue; } if (m < 0) { //FormulaEditorPerformer.ThrowErrorException(FormulaTree.ERRORS[1]); } if (j == 0 | m != 0) { continue; } IBinaryAcceptor acceptor = detector.Detect(symbol); if (acceptor == null) { continue; } if (dic.ContainsKey(j)) { tA = dic[j][0]; tB = dic[j][1]; } else { tA = CreateTree(new MathFormula(formula, 0, j - 1), creator); tB = CreateTree(new MathFormula(formula, j + 1, formula.Count - 1), creator); if ((tA == null) | (tB == null)) { continue; } dic[j] = new ObjectFormulaTree[] { tA, tB }; } if (acceptor is IChildTreeCreator) { IChildTreeCreator cc = acceptor as IChildTreeCreator; ObjectFormulaTree t = cc[new ObjectFormulaTree[] { tA, tB }]; if (t != null) { Copy(t); y = new object[children.Count]; return(true); } } operation = acceptor.Accept(tA.ReturnType, tB.ReturnType); if (operation != null) { goto start; } } } else { int m = 0; for (int j = formula.Count - 1; j > 0; j--) { MathSymbol symbol = formula[j]; if (creator.IsKet(symbol)) { ++m; continue; } if (creator.IsBra(symbol)) { --m; continue; } if (m < 0) { //!!! FormulaEditorPerformer.ThrowErrorException(FormulaTree.ERRORS[1]); } if (m != 0) { continue; } IBinaryAcceptor acceptor = detector.Detect(symbol); if (acceptor == null) { continue; } if (dic.ContainsKey(j)) { tA = dic[j][0]; tB = dic[j][1]; } else { tA = CreateTree(new MathFormula(formula, 0, j - 1), creator); tB = CreateTree(new MathFormula(formula, j + 1, formula.Count - 1), creator); if ((tA == null) | (tB == null)) { continue; } dic[j] = new ObjectFormulaTree[] { tA, tB }; } if (acceptor is IChildTreeCreator) { IChildTreeCreator cc = acceptor as IChildTreeCreator; ObjectFormulaTree t = cc[new ObjectFormulaTree[] { tA, tB }]; if (t != null) { Copy(t); y = new object[children.Count]; return(true); } } operation = acceptor.Accept(tA.ReturnType, tB.ReturnType); if (operation != null) { goto start; } } } start: if (operation != null) { children.Add(tA); children.Add(tB); y = new object[2]; return(true); } } return(false); }