Created
June 11, 2015 20:56
-
-
Save nwertzberger/91683a881929e5da5eec 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.DesireDefinitions; | |
import com.ideaheap.decisions.tmt.dto.TmtModel; | |
import com.ideaheap.decisions.tmt.dto.TmtOutcome; | |
import com.ideaheap.decisions.tmt.dto.TmtSuggestion; | |
import com.ideaheap.jade.fluent.df.DfServiceFinderFactory; | |
import com.ideaheap.jade.fluent.messages.MessageBuilder; | |
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 java.util.HashMap; | |
import static com.ideaheap.decisions.tmt.dto.TmtSuggestion.SuggestionType.ADD_OUTCOME; | |
import static com.ideaheap.jade.fluent.messages.MessageBuilder.inform; | |
/** | |
* Created by nwertzberger on 6/2/15. | |
*/ | |
public class SuggestToTmtAgents extends Behaviour { | |
private static final Logger logger = LoggerFactory.getLogger(SuggestToTmtAgents.class); | |
enum SuggestState { | |
NotSuggesting, | |
FirstSuggest, | |
Started | |
} | |
enum StateTransition { | |
EndpointFound, | |
ActiveCycle | |
} | |
private final MessageSender sender; | |
private final DfServiceFinderFactory dfServiceFinderFactory; | |
private final MessageReceiverFactory receiverFactory; | |
private final PlannerFactory plannerFactory; | |
private final StateMachine<SuggestState, StateTransition> stateMachine; | |
public SuggestToTmtAgents( | |
MessageSender sender, | |
DfServiceFinderFactory dfServiceFinderFactory, | |
MessageReceiverFactory receiverFactory, | |
PlannerFactory plannerFactory) { | |
this.sender = sender; | |
this.dfServiceFinderFactory = dfServiceFinderFactory; | |
this.receiverFactory = receiverFactory; | |
this.plannerFactory = plannerFactory; | |
this.stateMachine = createStateMachine(new StateMachineConfig<>()); | |
} | |
@Override | |
public void action() { | |
stateMachine.fire(StateTransition.ActiveCycle); | |
} | |
@Override | |
public boolean done() { | |
return false; | |
} | |
private StateMachine<SuggestState, StateTransition> createStateMachine( | |
StateMachineConfig<SuggestState, StateTransition> config) { | |
config.configure(SuggestState.NotSuggesting) | |
.permit(StateTransition.ActiveCycle, SuggestState.FirstSuggest); | |
config.configure(SuggestState.FirstSuggest) | |
.onEntry(this::sendFirstSuggestion) | |
.permitReentry(StateTransition.ActiveCycle) | |
.permit(StateTransition.EndpointFound, SuggestState.Started); | |
config.configure(SuggestState.Started) | |
.onEntry(this::suggestBasedOnResponse) | |
.permitReentry(StateTransition.ActiveCycle); | |
return new StateMachine<>(SuggestState.NotSuggesting, config); | |
} | |
public void sendFirstSuggestion() { | |
logger.info("Trying to do something like suggesting"); | |
AID target = dfServiceFinderFactory.find() | |
.forServiceType("tmt") | |
.findOne(); | |
if (target != null) { | |
stateMachine.fire(StateTransition.EndpointFound); | |
TmtSuggestion suggestion = createSuggestion(target); | |
logger.info("Sent suggestion " + suggestion); | |
} | |
} | |
public void suggestBasedOnResponse() { | |
try { | |
logger.info("waiting for response"); | |
receiverFactory.listen(this).forClass(TmtModel.class, (aid, o) -> createSuggestion(aid)); | |
} catch (MessageReceiverException e) { | |
logger.error("Caught exception", e); | |
} | |
} | |
private TmtSuggestion createSuggestion(AID target) { | |
TmtSuggestion suggestion = new TmtSuggestion() | |
.setSuggestionType(ADD_OUTCOME) | |
.setOutcome( | |
new TmtOutcome() | |
.setDescription("win a prize") | |
.setDesireValuation( | |
new HashMap<String, Double>() {{ | |
put(DesireDefinitions.ACHIEVEMENT, 0.8); | |
}} | |
) | |
.setEfficacy(1.0) | |
.setTemporalProximity(1.0) | |
).setActionName("exercise"); | |
sender.send(inform().to(target).withContent(suggestion).build()); | |
return suggestion; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment