Created
June 12, 2015 04:02
-
-
Save nwertzberger/e7102633f8fb423526e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ideaheap.decisions.tmt.agents.tmtAgent.behaviours; | |
import com.github.oxo42.stateless4j.StateMachine; | |
import com.github.oxo42.stateless4j.StateMachineConfig; | |
import com.ideaheap.decisions.tmt.dto.TmtAgentState; | |
import com.ideaheap.decisions.tmt.dto.TmtSuggestion; | |
import com.ideaheap.jade.fluent.df.DfServiceFinderFactory; | |
import com.ideaheap.jade.fluent.messages.MessageReceiverException; | |
import com.ideaheap.jade.fluent.messages.MessageReceiverFactory; | |
import com.ideaheap.jade.fluent.messages.MessageSender; | |
import jade.core.AID; | |
import jade.core.behaviours.Behaviour; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import static com.ideaheap.jade.fluent.messages.MessageBuilder.inform; | |
/** | |
* Created by nwertzberger on 5/30/15. | |
*/ | |
public class AskForSuggestions extends Behaviour { | |
private static final Logger logger = LoggerFactory.getLogger(AskForSuggestions.class); | |
enum State { | |
IDLE, | |
FINDING_SUGGESTION_SERVICE, | |
ASKING_FOR_SUGGESTIONS, | |
LISTENING_FOR_SUGGESTION; | |
} | |
enum Trigger { | |
ACTION, | |
SERVICE_FOUND, | |
SERVICE_LOST, | |
SUGGESTION_RECEIVED; | |
} | |
// Behaviors | |
private final MessageSender sender; | |
private final MessageReceiverFactory receiverFactory; | |
private final DfServiceFinderFactory finderFactory; | |
private final PreferenceUpdater updater; | |
private final StateMachine<State, Trigger> stateMachine; | |
// Agent state variables | |
private AID targetSuggestionService; | |
private TmtAgentState agentState; | |
public AskForSuggestions( | |
MessageSender sender, | |
MessageReceiverFactory receiverFactory, | |
DfServiceFinderFactory finderFactory, | |
StateMachineFactory stateMachineFactory, | |
PreferenceUpdater updater, | |
TmtAgentState agentState) { | |
this.sender = sender; | |
this.receiverFactory = receiverFactory; | |
this.finderFactory = finderFactory; | |
this.updater = updater; | |
this.agentState = agentState; | |
this.stateMachine = stateMachineFactory.createStateMachine(State.IDLE, this::createStateMachine); | |
} | |
@Override | |
public void action() { | |
stateMachine.fire(Trigger.ACTION); | |
} | |
@Override | |
public boolean done() { | |
return false; | |
} | |
public StateMachineConfig<State, Trigger> createStateMachine(StateMachineConfig<State, Trigger> config) { | |
config.configure(State.IDLE) | |
.onEntry(this::resetState) | |
.permit(Trigger.ACTION, State.FINDING_SUGGESTION_SERVICE); | |
config.configure(State.FINDING_SUGGESTION_SERVICE) | |
.onEntry(this::findSuggestionService) | |
.permitReentry(Trigger.ACTION) | |
.permit(Trigger.SERVICE_FOUND, State.ASKING_FOR_SUGGESTIONS); | |
config.configure(State.ASKING_FOR_SUGGESTIONS) | |
.onEntry(this::askForSuggestion) | |
.permit(Trigger.ACTION, State.LISTENING_FOR_SUGGESTION); | |
config.configure(State.LISTENING_FOR_SUGGESTION) | |
.onEntry(this::listenForSuggestion) | |
.permitReentry(Trigger.ACTION) | |
.permit(Trigger.SERVICE_LOST, State.FINDING_SUGGESTION_SERVICE) | |
.permit(Trigger.SUGGESTION_RECEIVED, State.ASKING_FOR_SUGGESTIONS); | |
return config; | |
} | |
private void resetState() { | |
targetSuggestionService = null; | |
} | |
private void findSuggestionService() { | |
targetSuggestionService = finderFactory.find().forServiceType("suggestion").findOne(); | |
if (targetSuggestionService != null) { | |
stateMachine.fire(Trigger.SERVICE_FOUND); | |
} | |
} | |
private void askForSuggestion() { | |
sender.send(inform().to(targetSuggestionService).withContent(agentState).build()); | |
} | |
private void listenForSuggestion() { | |
try { | |
receiverFactory.listen(this).forClass( | |
TmtSuggestion.class, (aid, suggestion) -> { | |
updater.updatePreferences(aid, suggestion); | |
stateMachine.fire(Trigger.SUGGESTION_RECEIVED); | |
} | |
); | |
} catch (MessageReceiverException e) { | |
logger.error("Had error with message!", e); | |
stateMachine.fire(Trigger.SERVICE_LOST); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment