public Expression ToSum()
 {
     if (Deleted)
     {
         throw new NullReferenceException();
     }
     foreach (Expression eProdComponent in SubExpressions)
     {
         if (eProdComponent is SumExpression)
         {
             SumExpression eSum = (SumExpression)eProdComponent;
             SumExpression eNew = MemoryAllocationUnit.GetInstance().NewSumExpression();
             foreach (Expression eSumComponent in eSum.SubExpressions)
             {
                 ProductExpression eProd = MemoryAllocationUnit.GetInstance().NewProductExpression();
                 eProd.AddSubExpression(eSumComponent);
                 foreach (Expression eOtherProdComponent in SubExpressions)
                 {
                     if (eOtherProdComponent != eSum)
                     {
                         eProd.AddSubExpression(eOtherProdComponent);
                     }
                 }
                 eNew.AddSubExpression(eProd);
             }
             return(eNew);
         }
     }
     return(this);
 }
        static void TestExpressionsI()
        {
            Expression    eOne   = MemoryAllocationUnit.GetInstance().NewValueExpression(1);
            Expression    eTwo   = MemoryAllocationUnit.GetInstance().NewValueExpression(2);
            Expression    eThree = MemoryAllocationUnit.GetInstance().NewValueExpression(3);
            SumExpression eSum12 = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum12.AddSubExpression(eOne);
            eSum12.AddSubExpression(eTwo);
            SumExpression eSum23 = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum23.AddSubExpression(eTwo);
            eSum23.AddSubExpression(eThree);
            ProductExpression eProd = MemoryAllocationUnit.GetInstance().NewProductExpression();

            eProd.AddSubExpression(eSum12);
            eProd.AddSubExpression(eSum23);
            Debug.WriteLine(eProd);

            Debug.WriteLine(eProd.Evaluate());

            Expression eConverted = eProd.ConvertProductOfSumToSumOfProducts();

            Debug.WriteLine(eConverted);

            Debug.WriteLine(eConverted.Evaluate());

            List <Expression> lRoot = new List <Expression>();

            lRoot.Add(eConverted);
            MemoryAllocationUnit.GetInstance().GarbageCollection(lRoot);
            int cCollectedObjects = MemoryAllocationUnit.GetInstance().CollectedCount;
            //should be 5 with the example above
            int iResult = eConverted.Evaluate();
        }
Example #3
0
 public void Delete()
 {
     if (Deleted)
     {
         throw new NullReferenceException();
     }
     Deleted = true;
     MemoryAllocationUnit.GetInstance().CollectedCount++;
 }
        static void TestExpressionsII(bool bPreserveProduct)
        {
            Expression    eOne   = MemoryAllocationUnit.GetInstance().NewValueExpression(1);
            Expression    eTwo   = MemoryAllocationUnit.GetInstance().NewValueExpression(2);
            Expression    eThree = MemoryAllocationUnit.GetInstance().NewValueExpression(3);
            SumExpression eSum12 = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum12.AddSubExpression(eOne);
            eSum12.AddSubExpression(eTwo);
            SumExpression eSum23 = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum23.AddSubExpression(eTwo);
            eSum23.AddSubExpression(eThree);
            ProductExpression eProd23 = MemoryAllocationUnit.GetInstance().NewProductExpression();

            eProd23.AddSubExpression(eTwo);
            eProd23.AddSubExpression(eThree);

            ProductExpression eProd = MemoryAllocationUnit.GetInstance().NewProductExpression();

            eProd.AddSubExpression(eSum12);
            eProd.AddSubExpression(eSum23);
            Debug.WriteLine(eProd);

            SumExpression eSum = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum.AddSubExpression(eProd23);
            eSum.AddSubExpression(eThree);

            List <Expression> lRoot = new List <Expression>();

            if (bPreserveProduct)
            {
                lRoot.Add(eProd);
                MemoryAllocationUnit.GetInstance().GarbageCollection(lRoot);
                int cCollectedObjects = MemoryAllocationUnit.GetInstance().CollectedCount;
                //should be 2 with the example above:
                //eSum and eProd23 should be removed because they do not appear under eProd
                int iResult = eProd.Evaluate();
                eSum.Evaluate(); //should generate an exception
            }
            else
            {
                lRoot.Add(eSum);
                MemoryAllocationUnit.GetInstance().GarbageCollection(lRoot);
                int cCollectedObjects = MemoryAllocationUnit.GetInstance().CollectedCount;
                //should be 4 with the example above:
                //eProd, eSum12, eSum23, eOne do not appear under eSum
                int iResult = eSum.Evaluate();
                eProd.Evaluate(); //should generate an exception
            }
        }