Exemplo n.º 1
0
 ISatisfactionRate GetESR(GoalRefinement goalRefinement)
 {
     if (goalRefinement.RefinementPattern == RefinementPattern.Milestone
         | goalRefinement.RefinementPattern == RefinementPattern.IntroduceGuard
         | goalRefinement.RefinementPattern == RefinementPattern.DivideAndConquer
         | goalRefinement.RefinementPattern == RefinementPattern.Unmonitorability
         | goalRefinement.RefinementPattern == RefinementPattern.Uncontrollability)
     {
         var subGoalSR = ComputeProductRefineeSatisfactionRate(goalRefinement.SubGoalIdentifiers,
                                                               x => GetESR(_model.Goal(x)));
         var domPropSR = ComputeProductRefineeSatisfactionRate(goalRefinement.DomainPropertyIdentifiers,
                                                               x => GetESR(_model.DomainProperty(x)));
         var domHypSR = ComputeProductRefineeSatisfactionRate(goalRefinement.DomainHypothesisIdentifiers,
                                                              x => GetESR(_model.DomainHypothesis(x)));
         return(subGoalSR.Product(domPropSR).Product(domHypSR));
     }
     else if (goalRefinement.RefinementPattern == RefinementPattern.Case)
     {
         var subGoalSR = ComputeRefineeSatisfactionRate(goalRefinement.SubGoalIdentifiers,
                                                        x => GetESR(_model.Goal(x)));
         var domPropSR = ComputeRefineeSatisfactionRate(goalRefinement.DomainPropertyIdentifiers,
                                                        x => GetESR(_model.DomainProperty(x)));
         var domHypSR = ComputeRefineeSatisfactionRate(goalRefinement.DomainHypothesisIdentifiers,
                                                       x => GetESR(_model.DomainHypothesis(x)));
         return((DoubleSatisfactionRate)subGoalSR.Sum(domPropSR).Sum(domHypSR));
     }
     else
     {
         throw new PropagationException(PropagationException.PATTERN_NOT_SUPPORTED + $" ({goalRefinement.RefinementPattern})");
     }
 }
        void KeepObstructedGoal(Resolution resolution)
        {
            // Create the exception
            var exception = new GoalException(_model);

            exception.AnchorGoalIdentifier       = resolution.AnchorIdentifier;
            exception.ResolvedObstacleIdentifier = resolution.ObstacleIdentifier;
            exception.ResolvingGoalIdentifier    = resolution.ResolvingGoalIdentifier;

            _model.Add(exception);

            var anchor = _model.Goal(resolution.AnchorIdentifier);

            Propagate(anchor, resolution.Obstacle());

            Obstacle obstacle = resolution.Obstacle();

            obstacle.Resolved = true;
        }
Exemplo n.º 3
0
        void KeepObstructedGoal(Resolution resolution)
        {
            // Get the anchor goal
            var anchor      = resolution.AnchorIdentifier;
            var anchor_goal = _model.Goal(anchor);

            // Get the refinements in which the anchor goal is involved
            var refinements = _model.GoalRefinements(x => x.SubGoalIdentifiers.Any(y => y.Identifier == anchor));

            // Create a new goal
            var new_anchor = new Goal(_model);

            new_anchor.Name       = anchor_goal.Name;
            new_anchor.FormalSpec = anchor_goal.FormalSpec;
            _model.Add(new_anchor);

            // Replace anchor goal by new goal in refinements
            foreach (var r in refinements)
            {
                r.SubGoalIdentifiers = new HashSet <GoalRefinee> (r.SubGoalIdentifiers.Where(x => x.Identifier != anchor));
                r.Add(new_anchor);
            }

            // Create a new refinement with anchor goal and cm goal
            var new_refinement = new GoalRefinement(_model);

            new_refinement.ParentGoalIdentifier = new_anchor.Identifier;
            new_refinement.Add(anchor);
            new_refinement.Add(resolution.ResolvingGoalIdentifier);
            _model.Add(new_refinement);

            Obstacle obstacle = resolution.Obstacle();

            propagator.DownPropagateAddConjunct
                (resolution,
                new Not(obstacle.FormalSpec),
                obstacle.Name != null ? "Not " + obstacle.Name : null);

            // Set the probability of the resolved obstacle to 0
            // TODO Use a flag "resolved"
            obstacle.Resolved = true;
            //_model.satisfactionRateRepository.AddObstacleSatisfactionRate (obstacle.Identifier, new DoubleSatisfactionRate (0));

            // Remove the resolution, its appearans in refinements and obstructions
            //Delete (resolution);
        }
Exemplo n.º 4
0
        public void DownPropagateAddConjunct(Resolution resolution, Formula formula, string name)
        {
            var goal     = _model.Goal(resolution.AnchorIdentifier);
            var obstacle = resolution.Obstacle();

            if (goal.FormalSpec != null)
            {
                visited = new HashSet <string> (new [] { resolution.ResolvingGoalIdentifier });

                var antecedant  = GetAntecedant(goal.FormalSpec);
                var replacement = new And(antecedant, formula);

                DownPropagateReplacement(goal, antecedant, replacement);
                UpPropagateReplacement(obstacle, antecedant, replacement);
            }


            visited = new HashSet <string> (new [] { resolution.ResolvingGoalIdentifier });
            DownPropagateNameConjunct(goal, name);
            UpPropagateNameConjunct(obstacle, name);
        }
Exemplo n.º 5
0
        public KAOSModel Generate()
        {
            if (_options.NbObstacles < _options.NbObstructions)
            {
                throw new ArgumentException("Cannot generate a model with more obstructions than obstacles.");
            }


            root = GenerateGoal();

            var goalToRefine     = new Stack <Tuple <Goal, int> > (new [] { new Tuple <Goal, int> (root, 0) });
            var obstacleToRefine = new Stack <Tuple <Obstacle, int> > ();
            var leafGoal         = new HashSet <string> ();
            var leafObstacles    = new HashSet <string> ();

            while (_model.Goals().Count() < _options.NbGoals)
            {
                var current = goalToRefine.Pop();
                leafGoal.Remove(current.Item1.Identifier);
                var r = GenerateGoalRefinement(current.Item1);
                foreach (var sg in r.SubGoals())
                {
                    goalToRefine.Push(new Tuple <Goal, int> (sg, current.Item2 + 1));
                    leafGoal.Add(sg.Identifier);
                }
            }

            var nbleafGoals    = _model.LeafGoals().Count();
            var maxObstruction = Math.Min(nbleafGoals, _options.NbObstructions);

            while (_model.ObstructedGoals().Count() < maxObstruction)
            {
                int randomIndex = _random.Next(0, nbleafGoals);
                var current     = _model.Goal(leafGoal.ElementAt(randomIndex));
                leafGoal.Remove(current.Identifier);
                nbleafGoals--;
                var r = GenerateObstruction(current);
                obstacleToRefine.Push(new Tuple <Obstacle, int> (r.Obstacle(), 0));
                leafObstacles.Add(r.ObstacleIdentifier);
            }

            while (_model.Obstacles().Count() < _options.NbObstacles)
            {
                var current = obstacleToRefine.Pop();
                leafObstacles.Remove(current.Item1.Identifier);
                var nbRefinement = _random.Next(_options.MinObstacleORBranchingFactor,
                                                _options.MaxObstacleORBranchingFactor);
                for (int i = 0; i < nbRefinement; i++)
                {
                    var r = GenerateObstacleRefinement(current.Item1);
                    foreach (var sg in r.SubObstacles())
                    {
                        obstacleToRefine.Push(new Tuple <Obstacle, int> (sg, current.Item2 + 1));
                        leafObstacles.Add(sg.Identifier);
                    }
                }
            }

            foreach (var current in leafObstacles)
            {
                _model.satisfactionRateRepository.AddObstacleSatisfactionRate(current,
                                                                              new DoubleSatisfactionRate(_random.NextDouble()));
            }

            return(_model);
        }
Exemplo n.º 6
0
        static void Optimize()
        {
            logger.Info("Optimize :-)");
            var goal = model.Goal(x => x.Identifier == "avoid_ambulance_mobilized_on_road");

            if (DateTime.Now - startTime > TimeSpan.FromMinutes(25))
            {
                optimizer.model.Goal(x => x.Identifier == goal.Identifier).RDS = .8;
                if (goal.CPS > .8)
                {
                    return;
                }
            }
            else
            {
                optimizer.model.Goal(x => x.Identifier == goal.Identifier).RDS = .5;
                if (goal.CPS > .5)
                {
                    return;
                }
            }

            var countermeasures = optimizer.Optimize();

            if (countermeasures == null)
            {
                return;
            }

            logger.Info("---- Selected CM");
            foreach (var item in countermeasures)
            {
                logger.Info(item.ResolvingGoal().FriendlyName);
            }
            logger.Info("---");

            Type simType = typeof(LAS.Monitoring.MainClass);

            // Adapt the software
            foreach (var cm in ActiveResolutions.Except(countermeasures))
            {
                if (cm.ResolvingGoal().CustomData.ContainsKey("onremove"))
                {
                    var method = cm.ResolvingGoal().CustomData["onremove"];
                    var mi     = simType.GetMethod(method);
                    if (mi != null)
                    {
                        mi.Invoke(null, new object[] { });
                    }
                    else
                    {
                        logger.Warn("Cannot find the method " + method);
                    }
                }
                else
                {
                    logger.Info("Miss onremove for " + cm.ResolvingGoal().FriendlyName);
                }
            }
            foreach (var cm in countermeasures.Except(ActiveResolutions))
            {
                if (cm.ResolvingGoal().CustomData.ContainsKey("ondeploy"))
                {
                    var method = cm.ResolvingGoal().CustomData["ondeploy"];
                    var mi     = simType.GetMethod(method);
                    if (mi != null)
                    {
                        mi.Invoke(null, new object[] { });
                    }
                    else
                    {
                        logger.Warn("Cannot find the method " + method);
                    }
                }
                else
                {
                    logger.Info("Miss ondeploy for " + cm.ResolvingGoal().FriendlyName);
                }
            }

            // Update the requirement model
            logger.Info("Countermeasures to withold:");
            foreach (var cm in ActiveResolutions.Except(countermeasures))
            {
                logger.Info("- {0}", cm.ResolvingGoal().FriendlyName);

                var resolutionToWithold = model.Elements.OfType <Resolution>()
                                          .Single(x => x.ResolvingGoalIdentifier == cm.ResolvingGoalIdentifier
                                                  & x.ObstacleIdentifier == cm.ObstacleIdentifier);

                ResolutionIntegrationHelper.Desintegrate(resolutionToWithold);
            }
            logger.Info("Countermeasures to deploy: ");
            foreach (var cm in countermeasures.Except(ActiveResolutions))
            {
                logger.Info("- {0}", cm.ResolvingGoal().FriendlyName);

                var resolutionToDeploy = model.Elements.OfType <Resolution>()
                                         .Single(x => x.ResolvingGoalIdentifier == cm.ResolvingGoalIdentifier
                                                 & x.ObstacleIdentifier == cm.ObstacleIdentifier);

                ResolutionIntegrationHelper.Integrate(resolutionToDeploy);
            }

            ActiveResolutions = countermeasures;

            // Update the obstruction sets
            ComputeObstructionSets();
            UpdateCPS();
            logger.Info("New configuration deployed");
        }