//----------------------------------------------------------------------------
// Copyright (C) 2011 Ingrid Nunes
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// To contact the authors:
// http://inf.ufrgs.br/prosoft/bdi4jade/
//
//----------------------------------------------------------------------------
package bdi4jade.examples.interactionprotocol.remedial;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import bdi4jade.annotation.Belief;
import bdi4jade.core.Capability;
import bdi4jade.examples.interactionprotocol.domain.Component;
import bdi4jade.examples.interactionprotocol.domain.Service;
import bdi4jade.examples.interactionprotocol.domain.predicate.Abnormal;
import bdi4jade.examples.interactionprotocol.domain.predicate.AnomalousCommunication;
import bdi4jade.examples.interactionprotocol.goal.ChangeServiceProviderGoal;
import bdi4jade.examples.interactionprotocol.goal.ProvideServiceGoal;
import bdi4jade.examples.interactionprotocol.goal.RequestServiceGoal;
import bdi4jade.examples.interactionprotocol.goal.VerifyAbnormalBehaviourGoal;
import bdi4jade.examples.interactionprotocol.plan.ChangeServiceProviderPlanBody;
import bdi4jade.examples.interactionprotocol.plan.NormalizeCommunicationPlanBody;
import bdi4jade.examples.interactionprotocol.plan.ProvideServicePlanBody;
import bdi4jade.examples.interactionprotocol.plan.RequestServicePlanBody;
import bdi4jade.examples.interactionprotocol.plan.SelfHealingPlanBody;
import bdi4jade.examples.interactionprotocol.plan.VerifyInternalOrExternalCausePlanBody;
import bdi4jade.goal.GoalTemplateFactory;
import bdi4jade.plan.DefaultPlan;
import bdi4jade.plan.Plan;
import bdi4jade.reasoning.DefaultBeliefRevisionStrategy;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
/**
* @author jgfaccin
*
*/
public class RemedialCapability extends Capability {
private static final long serialVersionUID = -2145767656215646224L;
// Beliefs
@Belief
private Map<Service, ArrayList<Service>> myServices;
@Belief
private Map<Service, Integer> serviceCost;
@Belief
private Map<Service, Component> currentProviders;
@Belief
private ArrayList<Component> unavailableProviders;
@Belief
private Boolean failure;
@bdi4jade.annotation.Plan
private Plan verifyInternalOrExternalCausePlan;
@bdi4jade.annotation.Plan
private Plan selfHealingPlan;
@bdi4jade.annotation.Plan
private Plan normalizeCommunicationPlan;
@bdi4jade.annotation.Plan
private Plan changeServiceProviderPlan;
@bdi4jade.annotation.Plan
private Plan provideServicePlan;
@bdi4jade.annotation.Plan
private Plan requestServicePlan;
public RemedialCapability(Map<Service, ArrayList<Service>> myServices) {
this.myServices = myServices;
this.serviceCost = new HashMap<Service, Integer>();
this.unavailableProviders = new ArrayList<>();
this.failure = false;
this.currentProviders = new HashMap<Service, Component>();
this.verifyInternalOrExternalCausePlan = new DefaultPlan(VerifyAbnormalBehaviourGoal.class,
VerifyInternalOrExternalCausePlanBody.class);
this.selfHealingPlan = new DefaultPlan(GoalTemplateFactory.hasBeliefOfTypeWithValue(Abnormal.class, false),
SelfHealingPlanBody.class);
this.normalizeCommunicationPlan = new DefaultPlan(
GoalTemplateFactory.hasBeliefOfTypeWithValue(AnomalousCommunication.class, false),
NormalizeCommunicationPlanBody.class);
this.changeServiceProviderPlan = new DefaultPlan(ChangeServiceProviderGoal.class,
ChangeServiceProviderPlanBody.class);
this.provideServicePlan = new DefaultPlan(ProvideServiceGoal.class, ProvideServicePlanBody.class);
this.requestServicePlan = new DefaultPlan(GoalTemplateFactory.goalOfType(RequestServiceGoal.class),
RequestServicePlanBody.class);
setBeliefRevisionStrategy(new RemedialBeliefRevisionStrategy());
}
public ArrayList<Service> getMyServices() {
ArrayList<Service> services = new ArrayList<Service>(this.myServices.keySet());
return services;
}
public ArrayList<Service> getMyServiceDependences(Service service) {
if (this.myServices.containsKey(service)) {
return this.myServices.get(service);
}
return new ArrayList<Service>();
}
public void setCurrentProviders(Map<Service, Component> currentProviders) {
this.currentProviders = currentProviders;
}
public void setServiceCost(Service service, Integer cost) {
this.serviceCost.put(service, cost);
}
public Map<Service, Integer> getServiceCost() {
return this.serviceCost;
}
class RemedialBeliefRevisionStrategy extends DefaultBeliefRevisionStrategy {
private MessageTemplate mtNotification;
private MessageTemplate mtServiceRequest;
@Override
public void reviewBeliefs() {
super.reviewBeliefs();
this.mtNotification = MessageTemplate.MatchPerformative(ACLMessage.INFORM_REF);
ACLMessage notificationMsg = getMyAgent().receive(mtNotification);
if (notificationMsg != null) {
getMyAgent().addGoal(new VerifyAbnormalBehaviourGoal(notificationMsg));
}
this.mtServiceRequest = MessageTemplate.MatchPerformative(ACLMessage.REQUEST);
ACLMessage serviceRequestMsg = getMyAgent().receive(mtServiceRequest);
if (serviceRequestMsg != null) {
Service service = new Service(serviceRequestMsg.getContent());
if (myServices.containsKey(service)) {
getMyAgent().addGoal(new ProvideServiceGoal(serviceRequestMsg));
}
}
}
}
}