// Draws trabasso dependencies between steps in the plan. private void CreateTrabassos() { // Reset the causal links. trabassos = new List <CausalLink>(); // Create an empty list of flaws. List <Flaw> flaws = new List <Flaw>(); // Create a list of steps that includes the initial and goal steps. List <Operator> allSteps = new List <Operator>(); // Add the initial step to the new list. allSteps.Add(InitialStep); // Loop through the plan's steps. foreach (Operator step in steps) { // Add the current step to the new list. allSteps.Add(step); // Add every precondition to the flaw list. foreach (Predicate precon in step.Preconditions) { flaws.Add(new Flaw(precon, step)); } } // Add the goal step to the new list. allSteps.Add(GoalStep); // Add every goal precondition to the flaw list. foreach (Predicate precon in GoalStep.Preconditions) { flaws.Add(new Flaw(precon, GoalStep)); } // Resolve every flaw. while (flaws.Count > 0) { List <IOperator> span = new List <IOperator>(); span.Add(flaws.First().step.Clone() as Operator); Operator found = null; Predicate literal = flaws.First().precondition.Clone() as Predicate; // Loop through the steps: // Start at the flaw's step, // Loop backward to the initial step. for (int i = allSteps.IndexOf(flaws.First().step) - 1; i >= 0; i--) { // Loop through each step's effects. foreach (Predicate effect in allSteps[i].Effects) { if (literal.Equals(effect)) { found = allSteps.ElementAt(i).Clone() as Operator; } if (found != null) { // Check to see if the effect matches the flaw's predicate. if (literal.IsInverse(effect)) { // Create a new causal link object. CausalLink link = new CausalLink(); // Set the link's predicate to the flaw's precondition. link.Predicate = flaws[0].precondition; // Set the link's head to the flaw's step. link.Head = flaws[0].step; // Set the link's tail to the effect step. link.Tail = found; // Set the link's span. link.Span = span; // Add the causal link to the list of links. trabassos.Add(link); // Exit the loop. i = 0; found = null; } } } span.Add(allSteps.ElementAt(i).Clone() as Operator); } // If a step was not found with an effect of the reverse literal... // Check to see if the reverse exists in the initial state. if (found != null) { // A boolean that tracks whether the reversed literal was found in the initial state. bool inInitial = false; if (literal.Sign) { // Loop through the initial state literals and check to see if the reversed literal exists. foreach (Predicate init in initial.Predicates) { if (literal.Equals(init)) { inInitial = true; } } } else { inInitial = true; // Loop through the initial state literals and check to see if the reversed literal exists. foreach (Predicate init in initial.Predicates) { if (literal.IsInverse(init)) { inInitial = false; } } } // If the literal was not enabled in the initial state, add it. if (!inInitial) { trabassos.Add(new CausalLink(flaws.First().precondition, flaws.First().step, found, span)); } } // Remove the current flaw from the list. flaws.RemoveAt(0); } }
// Draws dependencies between steps in the plan. private void CreateDependencies() { // Reset the causal links. dependencies = new List <CausalLink>(); // Create an empty list of flaws. List <Flaw> flaws = new List <Flaw>(); // Create a list of steps that includes the initial and goal steps. List <Operator> allSteps = new List <Operator>(); // Add the initial step to the new list. allSteps.Add(InitialStep); // Loop through the plan's steps. foreach (Operator step in steps) { // Add the current step to the new list. allSteps.Add(step); // Add every precondition to the flaw list. foreach (Predicate precon in step.Preconditions) { flaws.Add(new Flaw(precon, step)); } } // Add the goal step to the new list. allSteps.Add(GoalStep); // Add every goal precondition to the flaw list. foreach (Predicate precon in GoalStep.Preconditions) { flaws.Add(new Flaw(precon, GoalStep)); } // Resolve every flaw. while (flaws.Count > 0) { bool found = false; List <IOperator> span = new List <IOperator>(); span.Add(flaws.First().step.Clone() as Operator); // Loop through the steps: // Start at the flaw's step, // Loop backward to the initial step. for (int i = allSteps.IndexOf(flaws.First().step) - 1; i >= 0; i--) { // Loop through each step's effects. foreach (Predicate effect in allSteps[i].Effects) { // Check to see if the effect matches the flaw's predicate. if (flaws.First().precondition.Equals(effect)) { // Record that the flaw has been fixed. found = true; // Create a new causal link object. CausalLink link = new CausalLink(); // Set the link's predicate to the flaw's precondition. link.Predicate = flaws[0].precondition; // Set the link's head to the flaw's step. link.Head = flaws[0].step; // Set the link's tail to the effect step. link.Tail = allSteps[i]; // Set the link's span. link.Span = span; // Add the causal link to the list of links. dependencies.Add(link); // Exit the loop. i = 0; } } span.Add(allSteps.ElementAt(i).Clone() as Operator); } // If the flaw wasn't fixed, add it to the initial state. if (!found) { dependencies.Add(new CausalLink(flaws.First().precondition, flaws.First().step, allSteps.First(), span)); } // Remove the current flaw from the list. flaws.RemoveAt(0); } }