Search in sources :

Example 1 with ICommandHandler

use of com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler in project malmo by Microsoft.

the class AgentQuitFromReachingCommandQuotaImplementation method prepare.

@Override
public void prepare(MissionInit missionInit) {
    // We need to see the commands as they come in, so we can calculate the quota usage.
    // To do this we create our own command handler and insert it at the root of the command chain.
    // This is also how the ObservationFromRecentCommands and RewardForSendingCommands handlers work.
    // It's slightly dirty behaviour, but it's cleaner than the other options!
    MissionBehaviour mb = parentBehaviour();
    ICommandHandler oldch = mb.commandHandler;
    CommandGroup newch = new CommandGroup() {

        protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
            // See if this command gets handled by the legitimate handlers:
            boolean handled = super.onExecute(verb, parameter, missionInit);
            if (// Yes, so record it:
            handled) {
                checkQuotas(verb, parameter);
            }
            return handled;
        }
    };
    newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
    if (oldch != null)
        newch.addCommandHandler(oldch);
    mb.commandHandler = newch;
}
Also used : MissionInit(com.microsoft.Malmo.Schemas.MissionInit) ICommandHandler(com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)

Example 2 with ICommandHandler

use of com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler in project malmo by Microsoft.

the class RewardForSendingCommandImplementation method prepare.

@Override
public void prepare(MissionInit missionInit) {
    super.prepare(missionInit);
    // We need to see the commands as they come in, so we can calculate the
    // reward.
    // To do this we create our own command handler and insert it at the
    // root of the command chain.
    // This is also how the ObservationFromRecentCommands handler works.
    // It's slightly dirty behaviour, but it's cleaner than the other
    // options!
    MissionBehaviour mb = parentBehaviour();
    ICommandHandler oldch = mb.commandHandler;
    CommandGroup newch = new CommandGroup() {

        protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
            // See if this command gets handled by the legitimate handlers:
            boolean handled = super.onExecute(verb, parameter, missionInit);
            if (// Yes, so record it:
            handled) {
                synchronized (RewardForSendingCommandImplementation.this.commandTally) {
                    RewardForSendingCommandImplementation.this.commandTally++;
                }
            }
            return handled;
        }
    };
    newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
    if (oldch != null)
        newch.addCommandHandler(oldch);
    mb.commandHandler = newch;
}
Also used : MissionInit(com.microsoft.Malmo.Schemas.MissionInit) ICommandHandler(com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)

Example 3 with ICommandHandler

use of com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler in project malmo by Microsoft.

the class ObservationFromRecentCommandsImplementation method writeObservationsToJSON.

@Override
public void writeObservationsToJSON(JsonObject json, MissionInit missionInit) {
    if (!hookedIntoCommandChain) {
        // We need to see the commands as they come in, so we can determine which ones to echo back in the observation message.
        // To do this we create our own command handler and insert it at the root of the command chain.
        // It's slightly dirty behaviour, but it saves
        // a) adding special code into ProjectMalmo.java just to allow for this ObservationProducer to work, and
        // b) requiring the user to add a special command handler themselves at the right point in the XML.
        MissionBehaviour mb = parentBehaviour();
        ICommandHandler oldch = mb.commandHandler;
        CommandGroup newch = new CommandGroup() {

            protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
                // See if this command gets handled by the legitimate handlers:
                boolean handled = super.onExecute(verb, parameter, missionInit);
                if (// Yes, so record it:
                handled)
                    ObservationFromRecentCommandsImplementation.this.addHandledCommand(verb, parameter);
                return handled;
            }
        };
        newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
        if (oldch != null)
            newch.addCommandHandler(oldch);
        mb.commandHandler = newch;
        this.hookedIntoCommandChain = true;
    }
    synchronized (this.recentCommandList) {
        // Have any commands been processed since we last sent a burst of observations?
        if (this.recentCommandList.size() != 0) {
            // Yes, so build up a JSON array:
            JsonArray commands = new JsonArray();
            for (String s : this.recentCommandList) {
                commands.add(new JsonPrimitive(s));
            }
            json.add("CommandsSinceLastObservation", commands);
        }
        this.recentCommandList.clear();
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) MissionInit(com.microsoft.Malmo.Schemas.MissionInit) ICommandHandler(com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)

Example 4 with ICommandHandler

use of com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler in project malmo by Microsoft.

the class RewardForSendingMatchingChatMessageImplementation method prepare.

/**
 * Called once before the mission starts - use for any necessary
 * initialisation.
 *
 * @param missionInit
 */
@Override
public void prepare(MissionInit missionInit) {
    super.prepare(missionInit);
    // We need to see chat commands as they come in.
    // Following the example of RewardForSendingCommandImplementation.
    MissionBehaviour mb = parentBehaviour();
    ICommandHandler oldch = mb.commandHandler;
    CommandGroup newch = new CommandGroup() {

        protected boolean onExecute(String verb, String parameter, MissionInit missionInit) {
            // See if this command gets handled by the legitimate handlers:
            boolean handled = super.onExecute(verb, parameter, missionInit);
            if (// Yes, so check if we need to produce a reward
            handled && verb.equalsIgnoreCase(ChatCommand.CHAT.value())) {
                Iterator<Map.Entry<Pattern, Float>> patternIt = patternMap.entrySet().iterator();
                while (patternIt.hasNext()) {
                    Map.Entry<Pattern, Float> entry = patternIt.next();
                    Matcher m = entry.getKey().matcher(parameter);
                    if (m.matches()) {
                        String distribution = distributionMap.get(entry.getKey());
                        addAndShareCachedReward(RewardForSendingMatchingChatMessageImplementation.this.params.getDimension(), entry.getValue(), distribution);
                    }
                }
            }
            return handled;
        }
    };
    newch.setOverriding((oldch != null) ? oldch.isOverriding() : true);
    if (oldch != null)
        newch.addCommandHandler(oldch);
    mb.commandHandler = newch;
}
Also used : MissionInit(com.microsoft.Malmo.Schemas.MissionInit) ICommandHandler(com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)

Aggregations

ICommandHandler (com.microsoft.Malmo.MissionHandlerInterfaces.ICommandHandler)4 MissionInit (com.microsoft.Malmo.Schemas.MissionInit)4 JsonArray (com.google.gson.JsonArray)1 JsonPrimitive (com.google.gson.JsonPrimitive)1