Esempio n. 1
0
        /// <summary>
        ///    Expands the dynamic formula using the list of available object that can be used in the formula
        /// </summary>
        public IFormula ExpandUsing(EntityDescriptorMapList <IFormulaUsable> allFormulaUsable, IObjectPathFactory objectPathFactory, IObjectBaseFactory objectBaseFactory)
        {
            var explicitFormula = objectBaseFactory.Create <ExplicitFormula>().WithName(Name).WithFormulaString("0");
            var allEntityToUse  = allFormulaUsable.AllSatisfiedBy(Criteria);

            explicitFormula.Dimension = Dimension;

            //no entity to use in the dynamic formula? return
            if (allEntityToUse.Count == 0)
            {
                return(explicitFormula);
            }

            var stringBuilder = new StringBuilder();

            for (int i = 0; i < allEntityToUse.Count; i++)
            {
                addDynamicVariablesToFormula(explicitFormula, stringBuilder, allEntityToUse[i], i, objectPathFactory);

                if (i < allEntityToUse.Count - 1)
                {
                    stringBuilder.AppendFormat(" {0} ", Operation);
                }
            }

            explicitFormula.FormulaString = stringBuilder.ToString();

            return(explicitFormula);
        }
Esempio n. 2
0
        protected override void Context()
        {
            var organism = new Container().WithName("Organism");

            _objectPathFactory = new ObjectPathFactory(new AliasCreator());
            _fuParameter       = new Parameter().WithName("fu").WithValue(0.5);

            organism.Add(_fuParameter);
            var liver       = new Container().WithName("Liver").WithParentContainer(organism);
            var liverVolume = new Parameter().WithName("Volume").WithValue(10).WithParentContainer(liver);
            var f_vas_liver = new Parameter().WithName("f_vas").WithValue(0.1);

            liver.Add(f_vas_liver);

            var kidney       = new Container().WithName("Kidney").WithParentContainer(organism);
            var kidneyVolume = new Parameter().WithName("Volume").WithValue(20).WithParentContainer(kidney);

            kidney.Add(new Parameter().WithName("f_vas").WithValue(0.2));

            sut               = new SumFormula();
            sut.Criteria      = Create.Criteria(x => x.With("Volume"));
            sut.Variable      = "V";
            sut.FormulaString = "fu * V_#i * f_vas_#i";
            sut.AddObjectPath(_objectPathFactory.CreateRelativeFormulaUsablePath(liverVolume, f_vas_liver).WithAlias("f_vas_#i"));
            sut.AddObjectPath(_objectPathFactory.CreateAbsoluteFormulaUsablePath(_fuParameter).WithAlias("fu"));

            sut.Dimension = new Dimension(new BaseDimensionRepresentation(), "dim1", "unit1");

            _allFormulaUsable = organism.GetAllChildren <IFormulaUsable>().ToEntityDescriptorMapList();

            _objectBaseFactory = A.Fake <IObjectBaseFactory>();
            A.CallTo(() => _objectBaseFactory.Create <ExplicitFormula>()).Returns(new ExplicitFormula());
        }
        public void MergeCalculationMethodInModel(IModel model, IBuildConfiguration buildConfiguration)
        {
            try
            {
                _buildConfiguration    = buildConfiguration;
                _allContainers         = model.Root.GetAllContainersAndSelf <IContainer>().ToEntityDescriptorMapList();
                _allBlackBoxParameters = model.Root.GetAllChildren <IParameter>().Where(p => p.Formula.IsBlackBox()).ToList();
                _model = model;
                foreach (var calculationMethod in buildConfiguration.AllCalculationMethods())
                {
                    var allMoleculesUsingMethod = allMoleculesUsing(calculationMethod, buildConfiguration.Molecules).ToList();

                    createFormulaForBlackBoxParameters(calculationMethod, allMoleculesUsingMethod);

                    addHelpParametersFor(calculationMethod, allMoleculesUsingMethod);
                }
            }
            finally
            {
                _buildConfiguration = null;
                _allContainers.Clear();
                _allBlackBoxParameters.Clear();
                _model = null;
            }
        }
        public void CreateObservers(IBuildConfiguration buildConfiguration, IModel model)
        {
            _allContainerDescriptors = model.Root.GetAllChildren <IContainer>().ToEntityDescriptorMapList();
            _buildConfiguration      = buildConfiguration;
            var observers        = buildConfiguration.Observers;
            var presentMolecules = buildConfiguration.AllPresentMolecules().ToList();

            try
            {
                foreach (var observerBuilder in observers.AmountObserverBuilders)
                {
                    createAmountObserver(observerBuilder, model, presentMolecules);
                }


                foreach (var observerBuilder in observers.ContainerObserverBuilders)
                {
                    createContainerObserver(observerBuilder, model, presentMolecules);
                }
            }
            finally
            {
                _allContainerDescriptors = null;
                _buildConfiguration      = null;
            }
        }
        public void CreateEvents(IBuildConfiguration buildConfiguration, IModel model)
        {
            try
            {
                _model = model;
                _buildConfiguration           = buildConfiguration;
                _allModelContainerDescriptors = model.Root.GetAllContainersAndSelf <IContainer>().ToEntityDescriptorMapList();

                _sourceCriteriaTargetContainerCache       = new Cache <DescriptorCriteria, IEnumerable <IContainer> >();
                _applicationTransportTargetContainerCache = new Cache <DescriptorCriteria, IEnumerable <IContainer> >();

                _eventGroupsWhichAreNotApplications = new List <IEventGroup>();

                //Cache all containers where the event group builder will be created using the source criteria
                foreach (var eventGroupBuilder in _buildConfiguration.EventGroups)
                {
                    if (_sourceCriteriaTargetContainerCache.Contains(eventGroupBuilder.SourceCriteria))
                    {
                        continue;
                    }

                    _sourceCriteriaTargetContainerCache.Add(eventGroupBuilder.SourceCriteria, _allModelContainerDescriptors.AllSatisfiedBy(eventGroupBuilder.SourceCriteria));
                }

                foreach (var eventGroupBuilder in _buildConfiguration.EventGroups)
                {
                    createEventGroupFrom(eventGroupBuilder, buildConfiguration.Molecules);
                }

                //---- Replace the keyword MOLECULE in the event groups which are not applications with
                //     the name of the first floating molecule. This is done only for backward compatibility:
                //
                //     - continuous EHC defined in old PK-Sim 5.x projects.
                //       (the new implementation of EHC does not require this kind of replacement)
                //
                //     - immediate EHC created during project conversion from old PK-Sim 4.2 project
                //
                //     This step must be done after ALL other keyword replacements
                foreach (var nonAppEventGroup in _eventGroupsWhichAreNotApplications)
                {
                    _keywordReplacerTask.ReplaceMoleculeKeywordInNonApplicationEventGroup(nonAppEventGroup, buildConfiguration.Molecules);
                }
            }
            finally
            {
                _model = null;
                _allModelContainerDescriptors = null;
                _sourceCriteriaTargetContainerCache.Clear();
                _sourceCriteriaTargetContainerCache = null;
                _applicationTransportTargetContainerCache.Clear();
                _applicationTransportTargetContainerCache = null;
                _buildConfiguration = null;
            }
        }
        private void addApplicationTransportToModel(ITransportBuilder appTransport, EntityDescriptorMapList <IContainer> allEventGroupParentChildContainers, string moleculeName)
        {
            var appTransportSourceContainers = sourceContainersFor(appTransport, allEventGroupParentChildContainers);
            var appTransportTargetContainers = _applicationTransportTargetContainerCache[appTransport.TargetCriteria].ToList();

            foreach (var sourceContainer in appTransportSourceContainers)
            {
                var sourceAmount = sourceContainer.GetSingleChildByName <IMoleculeAmount>(moleculeName);
                if (sourceAmount == null)
                {
                    throw new OSPSuiteException(Validation.CannotCreateApplicationSourceNotFound(appTransport.Name, moleculeName, sourceContainer.Name));
                }

                foreach (var targetContainer in appTransportTargetContainers)
                {
                    var targetAmount = targetContainer.GetSingleChildByName <IMoleculeAmount>(moleculeName);
                    if (targetAmount == null)
                    {
                        throw new OSPSuiteException(Validation.CannotCreateApplicationTargetNotFound(appTransport.Name, moleculeName, targetContainer.Name));
                    }

                    var transport = _transportMapper.MapFrom(appTransport, _buildConfiguration);

                    transport.SourceAmount = sourceAmount;
                    transport.TargetAmount = targetAmount;

                    _keywordReplacerTask.ReplaceIn(transport, _model.Root, moleculeName);

                    //At the moment, no neighborhoods between application sub-containers and
                    //spatial structure sub-containers are defined. Application transports are
                    //added as direct children of the source molecule amount
                    if (!sourceAmount.ContainsName(transport.Name))
                    {
                        sourceAmount.Add(transport);
                    }
                    else
                    {
                        throw new OSPSuiteException(Validation.TransportAlreadyCreatorForMolecule(appTransport.Name, transport.Name, moleculeName));
                    }
                }
            }
        }
Esempio n. 7
0
        public void CreateEvents(IBuildConfiguration buildConfiguration, IModel model)
        {
            try
            {
                _model = model;
                _buildConfiguration           = buildConfiguration;
                _allModelContainerDescriptors = model.Root.GetAllContainersAndSelf <IContainer>().ToEntityDescriptorMapList();

                _sourceCriteriaTargetContainerCache       = new Cache <DescriptorCriteria, IEnumerable <IContainer> >();
                _applicationTransportTargetContainerCache = new Cache <DescriptorCriteria, IEnumerable <IContainer> >();

                //Cache all containers where the event group builder will be created using the source criteria
                foreach (var eventGroupBuilder in _buildConfiguration.EventGroups)
                {
                    if (_sourceCriteriaTargetContainerCache.Contains(eventGroupBuilder.SourceCriteria))
                    {
                        continue;
                    }

                    _sourceCriteriaTargetContainerCache.Add(eventGroupBuilder.SourceCriteria, _allModelContainerDescriptors.AllSatisfiedBy(eventGroupBuilder.SourceCriteria));
                }

                foreach (var eventGroupBuilder in _buildConfiguration.EventGroups)
                {
                    createEventGroupFrom(eventGroupBuilder, buildConfiguration.Molecules);
                }
            }
            finally
            {
                _model = null;
                _allModelContainerDescriptors = null;
                _sourceCriteriaTargetContainerCache.Clear();
                _sourceCriteriaTargetContainerCache = null;
                _applicationTransportTargetContainerCache.Clear();
                _applicationTransportTargetContainerCache = null;
                _buildConfiguration = null;
            }
        }
 private IEnumerable <IContainer> sourceContainersFor(ITransportBuilder transport, EntityDescriptorMapList <IContainer> allEventGroupParentChildContainers)
 {
     return(allEventGroupParentChildContainers.AllSatisfiedBy(transport.SourceCriteria));
 }
 protected override void Context()
 {
     sut = new EntityDescriptorMapList <IContainer>();
 }