package jadex.bdi.examples.marsworld_classic.sentry; import jadex.application.runtime.IApplicationExternalAccess; import jadex.application.space.agr.AGRSpace; import jadex.application.space.agr.Group; import jadex.base.fipa.SFipa; import jadex.bdi.examples.marsworld_classic.RequestProduction; import jadex.bdi.examples.marsworld_classic.Target; import jadex.bdi.runtime.IGoal; import jadex.bdi.runtime.IMessageEvent; import jadex.bdi.runtime.Plan; import jadex.bridge.IComponentIdentifier; /** * Inform the sentry agent about a new target. */ public class AnalyseTargetPlanAGR extends Plan { //-------- constructors -------- /** * Create a new plan. */ public AnalyseTargetPlanAGR() { getLogger().info("Created: "+this); } //-------- methods -------- /** * The plan body. */ public void body() { Target target = (Target)getParameter("target").getValue(); // Move to the target. IGoal go_target = createGoal("move.move_dest"); go_target.getParameter("destination").setValue(target.getLocation()); dispatchSubgoalAndWait(go_target); // Analyse the target. waitFor(1000); target.setMarked(); if(target.getOreCapacity()>0) callProductionAgent(target); startAtomic(); getBeliefbase().getBeliefSet("analysed_targets").addFact(target); getBeliefbase().getBeliefSet("my_targets").removeFact(target); endAtomic(); } /** * Sending a locaton to the Production Agent. * Therefore it has first to be looked up in the DF. * @param target */ private void callProductionAgent(Target target) { // System.out.println("Calling some Production Agent..."); IApplicationExternalAccess app = (IApplicationExternalAccess)getScope().getParent(); AGRSpace agrs = (AGRSpace)app.getSpace("myagrspace"); Group group = agrs.getGroup("mymarsteam"); IComponentIdentifier[] producers = group.getAgentsForRole("producer"); if(producers.length>0) { int sel = (int)(Math.random()*producers.length); // todo: Select not randomly // System.out.println("Found agents: "+producers.length+" selected: "+sel); RequestProduction rp = new RequestProduction(); rp.setTarget(target); //Action action = new Action(); //action.setAction(rp); //action.setActor(SJade.convertAIDtoJade(producers[sel].getName())); IMessageEvent mevent = createMessageEvent("request_producer"); mevent.getParameterSet(SFipa.RECEIVERS).addValue(producers[sel]); mevent.getParameter(SFipa.CONTENT).setValue(rp); sendMessage(mevent); // System.out.println("Sentry Agent: sent location to: "+producers[sel].getName()); } } }