/** method that is called by the event handler whenever the mind initiates a new action */ protected synchronized void receiveActionFromMind(MindAction a) { // create a new entry in the rulesAlreadyTried map ArrayList<CompetencyManagerRule> rulesForThisAction = new ArrayList<CompetencyManagerRule>(); // find a suitable rule CompetencyManagerRule rule = findMatch(a, rulesForThisAction); // check if we have found a rule if (rule != null) { // remember that we tried this rule for this action rulesForThisAction.add(rule); rulesAlreadyTried.put(a, rulesForThisAction); // get an instantiated copy of the plan associated with the rule CompetencyExecutionPlan plan = rule.getExecutionPlan().getInstantiatedCopy(a.getMappings(), a); // and also remember that this execution plan belongs to this action plansCurrentlyExecuted.put(plan, a); // schedule a new request with comeptency execution component architecture.getCompetencyExecution().schedule(new RequestNewCompetencyExecutionPlan(plan)); } else { // we could not find any execution plan, raise an event that the mind action has failed this.raise(new EventMindActionFailed(a)); } }
/** * find a rule from the rule base that maps the specified Mind Action to a competency execution * plan * * @param a the mind action that we are checking matches for * @param rulesToIgnore the rules that should not be matched with * @return a rule that matches or null if no match could be found */ protected synchronized CompetencyManagerRule findMatch( MindAction a, ArrayList<CompetencyManagerRule> rulesToIgnore) { // create temporary structure to record matches in ArrayList<CompetencyManagerRule> matches = new ArrayList<CompetencyManagerRule>(); // iterate through all rules and check for matches for (CompetencyManagerRule rule : rules) if (rule.getMindAction().compareMatch(a)) if (!rulesToIgnore.contains(rule)) matches.add(rule); // TODO: filter out matches that use competencies which are not available // check if we have matches left if (matches.size() == 0) return null; // if there is more than one match left, decide for the first most specific one CompetencyManagerRule match = matches.get(0); int bestSpecificity = matches.get(0).getSpecificity(); for (CompetencyManagerRule rule : matches) if (rule.getSpecificity() > bestSpecificity) { match = rule; bestSpecificity = rule.getSpecificity(); } return match; }
/** handles unsuccessful competency execution plans */ private synchronized void processPlanFailure(CompetencyExecutionPlan plan) { // check if this is one of the plans currently executed if (plansCurrentlyExecuted.containsKey(plan)) { // obtain the mind action belonging to this execution plan MindAction ma = plansCurrentlyExecuted.get(plan); // remove this plan from our memory plansCurrentlyExecuted.remove(plan); // see if we can find another rule CompetencyManagerRule rule = findMatch(ma, rulesAlreadyTried.get(ma)); // check if we have found a rule if (rule != null) { // remember that we tried this rule for this action rulesAlreadyTried.get(ma).add(rule); // get an instantiated copy of the plan associated with the rule CompetencyExecutionPlan newPlan = rule.getExecutionPlan().getInstantiatedCopy(ma.getMappings(), ma); // and also remember that this execution plan belongs to this action plansCurrentlyExecuted.put(newPlan, ma); // schedule a new request with competency execution component architecture .getCompetencyExecution() .schedule(new RequestNewCompetencyExecutionPlan(newPlan)); } else // we could not find any other plans { // remove what's left of this action from our memory rulesAlreadyTried.remove(ma); // raise an event that the mind action has failed this.raise(new EventMindActionFailed(ma)); } } else { // this was not one of the plans we are waiting for // this should not happen, so print out error System.err.println("CompetencyManager error: received plan failure for unknown plan"); System.err.println("plan steps: " + plan.getNoOfSteps()); for (CompetencyExecutionPlanStep step : plan.getPlanSteps()) System.err.println("step " + step.getCompetencyType()); } }