private void setMoleculeStartValueFormula(IFormula formula, IMoleculeStartValue moleculeStartValue, IBuildingBlock moleculesStartValues)
 {
     if (!formula.IsConstant())
     {
         moleculeStartValue.Formula = _cloneManagerForBuildingBlock.Clone(formula, moleculesStartValues.FormulaCache);
     }
 }
Exemple #2
0
        private void renameAbsolutePathIn(IFormula formula)
        {
            if (formula == null || formula.IsConstant())
            {
                return;
            }

            formula.ObjectPaths.Where(isAbsolutePath).Each(renameObjectPath);
        }
Exemple #3
0
 /// <summary>
 ///    add the given formula to the cache only if
 ///    1- the formula is not constant (constant formula are not registered in cache)
 ///    2- the formula was not registered already.
 /// </summary>
 /// <param name="formula">formula to add</param>
 /// <param name="formulaCache">formula cache</param>
 private void addFormulaToCacheIfNecessary(IFormula formula, IFormulaCache formulaCache)
 {
     if (formula.IsConstant())
     {
         return;
     }
     if (formulaCache.Contains(formula.Id))
     {
         return;
     }
     formulaCache.Add(formula);
 }
Exemple #4
0
        private static void addFormualDescription(IFormula formula, StringBuilder description)
        {
            if (formula.IsConstant())
            {
                description.AppendLine($"{AppConstants.Captions.Value}: {((ConstantFormula) formula).Value.ToString(CultureInfo.InvariantCulture)}");
            }

            if (formula.IsExplicit())
            {
                description.AppendLine($"{AppConstants.Captions.Formula}:{((ExplicitFormula) formula).FormulaString}");
            }
        }
        /// <summary>
        ///    Checks that the formula is equivalent for the start value. This includes evaluation of constant formula to a double
        /// </summary>
        /// <param name="startValue">The start value to check</param>
        /// <param name="targetFormula">The formula being evaluated</param>
        /// <returns>True if the formula is equivalent to the start value formula</returns>
        public bool HasEquivalentFormula(IStartValue startValue, IFormula targetFormula)
        {
            var startValueFormula = startValue.Formula;

            if (startValueFormula == null && targetFormula == null)
            {
                return(true);
            }

            if ((startValueFormula == null || startValueFormula.IsConstant()) && targetFormula.IsConstant())
            {
                return(isConstantFormulaEqualToStartValue(startValue, targetFormula.DowncastTo <ConstantFormula>()));
            }

            return(_formulaTask.FormulasAreTheSame(startValue.Formula, targetFormula));
        }
Exemple #6
0
        protected void RemoveConstantFormula(XElement quantityElement, IFormula formula, string formulaAttributeName, SerializationContext serializationContext)
        {
            if (!formula.IsConstant())
            {
                return;
            }

            //we need to save the value and remove the formula attribute that won't be saved
            var attribute = quantityElement.Attribute(formulaAttributeName);

            if (attribute != null)
            {
                attribute.Remove();
            }

            //remove unused formula from cache that was saved automatically in the reference mapping
            serializationContext.RemoveFormulaFromCache(formula);
        }
        public FormulaType MapFrom(IFormula formula)
        {
            if (formula == null)
            {
                return(FormulaType.Constant);
            }

            if (formula.IsAnImplementationOf <IDistributionFormula>())
            {
                return(FormulaType.Distribution);
            }

            if (formula.IsAnImplementationOf <TableFormula>())
            {
                return(FormulaType.Table);
            }

            return(formula.IsConstant() ? FormulaType.Constant : FormulaType.Rate);
        }
        public FormulaBuilderDTO MapFrom(IFormula formula)
        {
            if (formula == null)
            {
                return(FormulaBuilderDTO.NULL);
            }

            var dto = Map <FormulaBuilderDTO>(formula);

            if (formula.IsConstant())
            {
                dto.FormulaString = ((ConstantFormula)formula).Value.ConvertedTo <string>();
            }

            else if (formula.IsExplicit())
            {
                dto.FormulaString = ((ExplicitFormula)formula).FormulaString;
            }

            dto.FormulaType = _resolver.TypeFor(formula);
            dto.Dimension   = formula.Dimension;
            dto.ObjectPaths = _formulaUsablePathMapper.MapFrom(formula);
            return(dto);
        }
 public DistributionFormulaType MapFrom(IFormula formula)
 {
     if (formula == null)
     {
         return(DistributionFormulaType.DiscreteDistribution);
     }
     if (formula.IsAnImplementationOf <DiscreteDistributionFormula>() || formula.IsConstant())
     {
         return(DistributionFormulaType.DiscreteDistribution);
     }
     if (formula.IsAnImplementationOf <UniformDistributionFormula>())
     {
         return(DistributionFormulaType.UniformDistribution);
     }
     if (formula.IsAnImplementationOf <NormalDistributionFormula>())
     {
         return(DistributionFormulaType.NormalDistribution);
     }
     if (formula.IsAnImplementationOf <LogNormalDistributionFormula>())
     {
         return(DistributionFormulaType.LogNormalDistribution);
     }
     throw new MoBiException(AppConstants.Exceptions.UnknownDistributedFormula(formula.GetType()));
 }
        protected override IFormula CreateFormulaCloneFor(IFormula srcFormula)
        {
            // constant formulas are always cloned
            if (srcFormula.IsConstant())
            {
                return(CloneFormula(srcFormula));
            }

            // Check if target formula cache already contains the formula
            // to be cloned. This can happen if we are cloning
            // any substructure within the same building block
            // In this case, all formulas are already present in the
            // formula cache of this building block
            if (_formulaCache.Contains(srcFormula.Id))
            {
                return(srcFormula);
            }

            // Check if the formula was already cloned. Return the cloned
            // formula if so.
            if (_clonedFormulasByOriginalFormulaId.Contains(srcFormula.Id))
            {
                return(_clonedFormulasByOriginalFormulaId[srcFormula.Id]);
            }

            // Formula is neither present in the passed formula cahce nor
            // was already cloned. So create a new clone and insert it
            // in both target formula cache and the cache of all cloned formulas
            var clonedFormula = CloneFormula(srcFormula);

            _formulaCache.Add(clonedFormula);

            _clonedFormulasByOriginalFormulaId.Add(srcFormula.Id, clonedFormula);

            return(clonedFormula);
        }
Exemple #11
0
        public bool FormulasAreTheSame(IFormula firstFormula, IFormula secondFormula)
        {
            if (firstFormula == null && secondFormula == null)
            {
                return(true);
            }

            if (firstFormula == null || secondFormula == null)
            {
                return(false);
            }

            var firstType  = firstFormula.GetType();
            var secondType = secondFormula.GetType();

            if (firstType != secondType)
            {
                return(false);
            }

            //nothing more to check for distributed formula or black box formula
            if (firstFormula.IsDistributed() || firstFormula.IsBlackBox() || firstFormula.IsDynamic())
            {
                return(true);
            }

            if (firstFormula.IsConstant())
            {
                var firstConstFormula  = firstFormula.DowncastTo <ConstantFormula>();
                var secondConstFormula = secondFormula.DowncastTo <ConstantFormula>();
                return(firstConstFormula.Value == secondConstFormula.Value);
            }

            if (firstFormula.IsExplicit())
            {
                var firstExplicit  = firstFormula.DowncastTo <ExplicitFormula>();
                var secondExplicit = secondFormula.DowncastTo <ExplicitFormula>();

                if (!string.Equals(firstExplicit.FormulaString, secondExplicit.FormulaString))
                {
                    return(false);
                }

                //check that formula have the same references using the same alias
                var firstObjectPathCache = new Cache <string, IFormulaUsablePath>(x => x.Alias);
                firstObjectPathCache.AddRange(firstExplicit.ObjectPaths);
                var secondObjectPathCache = new Cache <string, IFormulaUsablePath>(x => x.Alias);
                secondObjectPathCache.AddRange(secondExplicit.ObjectPaths);


                if (firstObjectPathCache.Count() != secondObjectPathCache.Count())
                {
                    return(false);
                }

                foreach (var keyValue in firstObjectPathCache.KeyValues)
                {
                    if (!secondObjectPathCache.Contains(keyValue.Key))
                    {
                        return(false);
                    }
                    var path = secondObjectPathCache[keyValue.Key];
                    if (!path.Equals(keyValue.Value))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemple #12
0
 public void should_have_retrieved_the_formula_as_a_consant_formula()
 {
     _formula.IsConstant().ShouldBeTrue();
 }