/// <summary>
        /// Detects operation acceptor
        /// </summary>
        /// <param name="s">Operation symbol</param>
        /// <returns>The acceptor</returns>
        public IBinaryAcceptor Detect(MathSymbol s)
        {
            IBinaryAcceptor acc = detector.Detect(s);

            if (acc == null)
            {
                return(null);
            }
            return(new BinaryArrayOperationAcceptor(acc));
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="acceptor">Prototype acceptor</param>
 public BinaryArrayOperationAcceptor(IBinaryAcceptor acceptor)
 {
     this.acceptor = acceptor;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds acceptor
 /// </summary>
 /// <param name="acceptor">Acceptor to add</param>
 public void Add(IBinaryAcceptor acceptor)
 {
     acceptors.Add(acceptor);
 }
Ejemplo n.º 4
0
        /// <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);
        }