public BDDNode GetObstructionSet(GoalRefinement refinement) { BDDNode acc = null; foreach (var child in refinement.SubGoals()) { if (acc == null) { acc = GetObstructionSet(child); } else { var bDDNode = GetObstructionSet(child); acc = _manager.Or(acc, bDDNode); } } foreach (var child in refinement.DomainHypotheses()) { if (acc == null) { acc = GetObstructionSet(child); } else { var bDDNode = GetObstructionSet(child); acc = _manager.Or(acc, bDDNode); } } return(acc); }
GoalRefinement GenerateGoalRefinement(Goal parent) { var mode = (_options.MinGoalBranchingFactor + _options.MaxGoalBranchingFactor) / 2; var nbchild = Triangular.Sample(_random, _options.MinGoalBranchingFactor, _options.MaxGoalBranchingFactor, mode); var refinement = new GoalRefinement(_model) { ParentGoalIdentifier = parent.Identifier }; do { refinement.RefinementPattern = _faker.PickRandom <RefinementPattern> (); } while (refinement.RefinementPattern == RefinementPattern.None || refinement.RefinementPattern == RefinementPattern.Case); for (int i = 0; i < nbchild; i++) { var c = GenerateGoal(); refinement.Add(c); } _model.Add(refinement); return(refinement); }
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})"); } }
private static void AssertParameter(GoalRefinement refinement, string goalId, double expected) { IRefineeParameter p1 = refinement.SubGoalIdentifiers.Single(x => x.Identifier == goalId).Parameters; Assert.IsInstanceOf(typeof(PrimitiveRefineeParameter <double>), p1); var cp1 = (PrimitiveRefineeParameter <double>)p1; Assert.AreEqual(expected, cp1.Value); }
public void Add(GoalRefinement refinement) { if (GoalRefinements.ContainsKey(refinement.Identifier)) { throw new ArgumentException(string.Format("Goal refinement identifier already exist: {0}", refinement.Identifier)); } GoalRefinements.Add(refinement.Identifier, refinement); }
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); }
protected void Render(GoalRefinement refinement) { var circle = GetCircle(); if (refinement.IsComplete) { SetFillColor(circle, 0, 0, 0); } Add(refinement.Identifier, circle); if (shapes.ContainsKey(refinement.ParentGoalIdentifier)) { var parentGraphic = shapes[refinement.ParentGoalIdentifier].First(); var topArrow = GetFilledArrow(circle, parentGraphic); if (refinement.SystemReferenceIdentifier != null) { AddText(topArrow, ""); } sheet.GraphicsList.Add(topArrow); } foreach (var child in refinement.SubGoalIdentifiers) { if (!shapes.ContainsKey(child.Identifier)) { continue; } var childGraphic = shapes [child.Identifier].First(); var line = GetLine(childGraphic, circle); sheet.GraphicsList.Add(line); } foreach (var child in refinement.DomainPropertyIdentifiers) { if (!shapes.ContainsKey(child.Identifier)) { continue; } var childGraphic = shapes [child.Identifier].First(); var line = GetLine(childGraphic, circle); sheet.GraphicsList.Add(line); } foreach (var child in refinement.DomainHypothesisIdentifiers) { if (!shapes.ContainsKey(child.Identifier)) { continue; } var childGraphic = shapes [child.Identifier].First(); var line = GetLine(childGraphic, circle); sheet.GraphicsList.Add(line); } foreach (var child in refinement.PositiveSoftGoalsIdentifiers) { if (!shapes.ContainsKey(child)) { continue; } var childGraphic = shapes [child].First(); var line = GetFilledArrow(circle, childGraphic, true); AddText(line, @"Positive\par contribution"); sheet.GraphicsList.Add(line); } foreach (var child in refinement.NegativeSoftGoalsIdentifiers) { if (!shapes.ContainsKey(child)) { continue; } var childGraphic = shapes [child].First(); var line = GetFilledArrow(circle, childGraphic, true); AddText(line, @"Negative\par contribution"); sheet.GraphicsList.Add(line); } // Context label if (!string.IsNullOrEmpty(refinement.ContextIdentifier)) { string contextName = refinement.model.modelMetadataRepository.GetContext(refinement.ContextIdentifier).FriendlyName; var contextGraphic = GetRectangle(); contextGraphic.Style.Stroke.CornerRadius = 3; contextGraphic.Wrap = false; AddText(contextGraphic, "Context " + contextName); Add("context:" + refinement.Identifier, contextGraphic); var topArrow = GetLine(circle, contextGraphic); topArrow.Style.Stroke.Pattern = StrokePattern.Dashed; sheet.GraphicsList.Add(topArrow); } }
public BDDNode GetObstructionSet(GoalRefinement refinement) { BDDNode acc = null; var dict = new Dictionary <Goal, BDDNode>(); foreach (var child in refinement.SubGoals()) { dict.Add(child, GetObstructionSet(child)); } BDDNode acc2 = _manager.Zero; foreach (var child in refinement.SubGoalIdentifiers) { if (child.Parameters != null && child.Parameters is PrimitiveRefineeParameter <double> c) { Console.WriteLine("We have a partial satisfaction."); // Create or get a variable for the partial refinement int idx; if (_mapping.ContainsKey(c)) { idx = _mapping[c]; } else { idx = _manager.CreateVariable(); _mapping.Add(c, idx); _rmapping.Add(idx, c); } // AND the negation of the variable to the other subgoals' bddnode var neg = _manager.Create(idx, 0, 1); acc2 = _manager.Xor(_manager.Create(idx, 1, 0), acc2); //BDDNode bDDNode1 = GetObstructionSet(refinement.model.Goal(child.Identifier)); //var imp = _manager.Imply(bDDNode1, neg); //Console.WriteLine("---"); //Console.WriteLine(this.ToDot(imp)); //Console.WriteLine("---"); foreach (var node in dict.ToArray()) { if (node.Key.Identifier != child.Identifier) { //Console.WriteLine("==="); //Console.WriteLine(this.ToDot(dict[node.Key])); dict[node.Key] = _manager.And(node.Value, neg); //Console.WriteLine(this.ToDot(dict[node.Key])); //Console.WriteLine("==="); } } } } foreach (var kv in dict) { if (acc == null) { acc = kv.Value; } else { acc = _manager.Or(acc, kv.Value); } } foreach (var child in refinement.DomainHypotheses()) { if (acc == null) { acc = GetObstructionSet(child); } else { var bDDNode = GetObstructionSet(child); acc = _manager.Or(acc, bDDNode); } } return(_manager.And(acc, acc2)); }
public override void Handle(Goal element, ParsedRefinedByAttribute attribute, KAOSModel model) { var refinement = new GoalRefinement(model); refinement.SetParentGoal(element); // Parse the reference to children foreach (var child in attribute.ParsedRefinees) { var id = child.Identifier; var param = child.Parameters; IRefineeParameter refineeParameter = null; if (param != null) { if (param is ParsedPrimitiveRefineeParameter <double> ) { var cast = ((ParsedPrimitiveRefineeParameter <double>)param); refineeParameter = new PrimitiveRefineeParameter <double> (cast.Value); } else { throw new NotImplementedException(); } } Goal refinee; DomainProperty domprop; DomainHypothesis domhyp; if ((refinee = model.goalRepository.GetGoal(id)) != null) { refinement.Add(refinee, refineeParameter); } else if ((domprop = model.domainRepository.GetDomainProperty(id)) != null) { refinement.Add(domprop, refineeParameter); } else if ((domhyp = model.domainRepository.GetDomainHypothesis(id)) != null) { refinement.Add(domhyp, refineeParameter); } else { refinee = new Goal(model, id) { Implicit = true }; model.goalRepository.Add(refinee); refinement.Add(refinee); } } // Parse the context if (!string.IsNullOrEmpty(attribute.ContextIdentifier)) { var id = attribute.ContextIdentifier; Context context; if ((context = model.modelMetadataRepository.GetContext(id)) == null) { context = new Context(model, id); model.modelMetadataRepository.Add(context); } refinement.ContextIdentifier = context.Identifier; } // Parse the refinement pattern provided if (attribute.RefinementPattern != null) { if (attribute.RefinementPattern.Name == ParsedRefinementPatternName.Milestone) { refinement.RefinementPattern = RefinementPattern.Milestone; } else if (attribute.RefinementPattern.Name == ParsedRefinementPatternName.Case) { refinement.RefinementPattern = RefinementPattern.Case; } else if (attribute.RefinementPattern.Name == ParsedRefinementPatternName.IntroduceGuard) { refinement.RefinementPattern = RefinementPattern.IntroduceGuard; } else if (attribute.RefinementPattern.Name == ParsedRefinementPatternName.DivideAndConquer) { refinement.RefinementPattern = RefinementPattern.DivideAndConquer; } else if (attribute.RefinementPattern.Name == ParsedRefinementPatternName.Uncontrollability) { refinement.RefinementPattern = RefinementPattern.Uncontrollability; } else if (attribute.RefinementPattern.Name == ParsedRefinementPatternName.Unmonitorability) { refinement.RefinementPattern = RefinementPattern.Unmonitorability; } else { throw new BuilderException("Unsupported pattern " + refinement.RefinementPattern, attribute); } } if (!refinement.IsEmpty) { model.Add(refinement); } }