/** * Environment's primary execution function * * <ul> * <li>{@link #processTeamHunts() Processes Team Hunts} * <li>On {@link TurnType#TeamSelect Team Select} turn, calls the {@link AbstractFreeAgentGroup * Free Agent Group} to put all {@link AbstractAgent agents} (who are not in {@link * AbstractGroupAgent groups}) int {@link HuntingTeam teams} * </ul> */ @Override protected void updatePhysicalWorld() { processTeamHunts(); // Deal with ungrouped agents having teams formed if (fAGroup != null && dmodel.getTurnType() == TurnType.TeamSelect) { List<HuntingTeam> teams = fAGroup.selectTeams(dmodel.getUngroupedAgents()); for (HuntingTeam team : teams) { for (String agent : team.getMembers()) { sim.getPlayer(agent).enqueueInput(new HuntOrder(sim.getTime(), team)); logger.log( Level.FINE, "FreeAgentsGroup has created team {0} including {1}", new Object[] {team.hashCode(), nameOf(agent)}); } } } // Energy used on hunting, so this is when food is consumed if (dmodel.getTurnType() == TurnType.GoHunt) { for (Participant agent : sim.players.values()) { if (sim.isParticipantActive(agent.getId())) { if (agent instanceof ise.mace.participants.AbstractAgent) { agent.enqueueInput(new ConsumeFood(dmodel.getTime())); } } } } }
/** * Returns the name of an {@link AbstractAgent agent} or {@link AbstractGroupAgent group} based on * a given id * * @param id The id * @return The name matched with that id (or null if not located) * @see PublicAgentDataModel#getName() * @see PublicGroupDataModel#getName() */ String nameOf(String id) { if (this.isAgentId(id)) { return dmodel.getAgentById(id).getName(); } if (this.isGroupId(id)) { return dmodel.getGroupById(id).getName(); } return null; }
/** * Create a new {@link AbstractGroupAgent group} of a particular class, initialise it, and invite * some {@link AbstractAgent agents} to the group * * @param type The class of group that you wish to create * @param init The initialiser instance to initialise the group with * @return The id of the new group, or null if the group could not be created * @throws IllegalArgumentException If the group class is not in the list of * @throws RuntimeException If the the reflection libraries or constructor throw an exception * {@link #getAllowedGroupTypes() permissible gorup classes}, or if it can not be initialised * with a {@link GroupDataInitialiser} * @see #createGroup(java.lang.Class, ise.mace.models.GroupDataInitialiser) * @see #getAllowedGroupTypes() * @see AbstractGroupAgent * @see GroupDataInitialiser */ String createGroup(Class<? extends AbstractGroupAgent> type, GroupDataInitialiser init) { AbstractGroupAgent g = dmodel.createGroup(type, init); g.initialise(new EnvironmentConnector(this)); sim.addParticipant(g.getId(), g); sim.activateParticipant(g.getId()); return g.getId(); }
/** * Callback for when an agent registers with the environment * * @param registrationObject The registration object. {@link AbstractAgent agents} should use the * {@link RegistrationRequest} class, {@link AbstractGroupAgent groups} should use the {@link * GroupRegistration} class * @return The response to the registration request, or null if the registration failed * @see #deregister(presage.environment.messages.ENVDeRegisterRequest) */ @Override public ENVRegistrationResponse onRegister(ENVRegisterRequest registrationObject) { if (registrationObject instanceof RegistrationRequest) { final RegistrationRequest obj = (RegistrationRequest) registrationObject; if (!dmodel.registerParticipant(obj)) { return null; } } else if (registrationObject instanceof GroupRegistration) { final GroupRegistration obj = (GroupRegistration) registrationObject; if (!dmodel.registerGroup(obj)) { return null; } } UUID id = UUID.randomUUID(); authenticator.put(registrationObject.getParticipantID(), id); return new RegistrationResponse( registrationObject.getParticipantID(), id, new EnvironmentConnection(this)); }
/** * Callback for when a {@link Participant} leaves the environment * * @param deregistrationObject De-register object * @return Whether the de-registration was successful. This will be false if the participant was * not registered with the Environment */ @Override public boolean deregister(ENVDeRegisterRequest deregistrationObject) { if (!authenticator .get(deregistrationObject.getParticipantID()) .equals(deregistrationObject.getParticipantAuthCode())) { logger.log( Level.FINE, "Illegal deregister request from {0}", nameOf(deregistrationObject.getParticipantID())); return false; } boolean succeeded = dmodel.removeParticipant(deregistrationObject.getParticipantID()); logger.log( Level.FINE, "Deregister request from {0} reutrned {1}", new Object[] {nameOf(deregistrationObject.getParticipantID()), succeeded}); return succeeded; }
/** * Processes the {@link Hunt} actions of {@link AbstractAgent agents} that were hunting as part of * {@link HuntingTeam HuntingTeams} whose hunt records were added to {@link #storedHuntResults} */ private void processTeamHunts() { for (HuntingTeam team : storedHuntResults.keySet()) { // Reorganise each team into what they hunted // And who hunted it Map<Food, List<String>> hunters = new HashMap<Food, List<String>>(); for (TeamHuntEvent h : storedHuntResults.get(team)) { if (!hunters.containsKey(h.getFood())) { hunters.put(h.getFood(), new LinkedList<String>()); } hunters.get(h.getFood()).add(h.getAgent()); } // Now, for each food, see if they got a unit on it for (Food f : hunters.keySet()) { List<String> agents = hunters.get(f); double foodGained; int count = 0; while ((count + 1) * f.getHuntersRequired() <= agents.size()) { ++count; } foodGained = count * f.getNutrition() / agents.size(); // Then, for each agent, send the message String groupID = dmodel.getAgentById(agents.get(0)).getGroupId(); if (groupID == null) { for (String agent : agents) { sim.getPlayer(agent) .enqueueInput(new HuntResult(agent, foodGained, foodGained, dmodel.time)); } } else { Participant g = sim.getPlayer(groupID); for (String agent : agents) { g.enqueueInput(new HuntResult(agent, foodGained, 0, dmodel.time)); } } } } storedHuntResults.clear(); }
/** * Gets the group object associated with a particular id * * @param id The id to search for * @return The group object, or null if not found * @see #getGroups() */ PublicGroupDataModel getGroupById(String id) { return dmodel.getGroupById(id); }
/** * Gets the agent data object associated with a particular id, which is safe for being passed to * other agents without giving them too much information * * @param id The id to search for * @return The agent object, or null if not found * @see #getAgents() */ PublicAgentDataModel getAgentById(String id) { return dmodel.getAgentById(id); }
/** * Returns a list of all {@link AbstractAgent agents} that are not currently part of a {@link * AbstractGroupAgent group} * * @return The IDs of all un-grouped agents * @see #getAgentById(java.lang.String) getAgentById */ List<String> getUngroupedAgents() { return dmodel.getUngroupedAgents(); }
/** * Returns the set of all {@link AbstractAgent agents} current alive * * @return IDs of all current agents * @see #getAgentById(java.lang.String) * @see #getGroups() */ Set<String> getAgents() { return dmodel.getAgents(); }
/** * Gets the amount of food consumes by asking someone for advice * * @return Amount of food * @see AbstractAgent#seekAvice(java.lang.String) */ double getFoodConsumedPerAdvice() { return dmodel.getFoodConsumedPerAdvice(); }
/** * Returns the {@link Environment} Environment's id * * @return The Environment's id * @see Environment#getId() */ public String getId() { return dmodel.getId(); }
/** * Returns the list of all classes of group that may be used in this simulation. * * <p>Each of these classes will extend {@link AbstractGroupAgent}. The validity of this classes * is not checked at simulation time - the framework may not be able to initialise them with a * {@link GroupDataInitialiser}. * * <p>The returned classes are also not guaranteed to be concrete implementations. * * <p>Groups of these classes can then be created with {@link #createGroup(java.lang.Class, * ise.mace.models.GroupDataInitialiser) createGroup()} with an appropriate initialiser. * * @return List of all permitted Group classes * @see AbstractGroupAgent * @see GroupDataInitialiser * @see #createGroup(java.lang.Class, ise.mace.models.GroupDataInitialiser) * @see #getGroups() */ List<Class<? extends AbstractGroupAgent>> getAllowedGroupTypes() { return dmodel.getAllowedGroupTypes(); }
/** * Gets the total number of rounds past * * @return Total completed rounds * @see TurnType */ public int getRoundsPassed() { return dmodel.getRoundsPassed(); }
/** * Returns what {@link TurnType turn} it is in the round * * @return The current turn type * @see #getRoundsPassed() */ public TurnType getCurrentTurnType() { return dmodel.getTurnType(); }
/** * Gets the food object associated with a particular id * * @param id The id to search for * @return The food object, or null if not found * @see #availableFoods() */ Food getFoodById(UUID id) { return dmodel.getFoodById(id); }
/** * Finds what food types are available to hunt * * @return set of available food. * @see #getFoodById(java.util.UUID) */ Set<Food> availableFoods() { return dmodel.availableFoods(); }
/** * Returns the set of all {@link AbstractGroupAgent groups} current active * * @return IDs of all current groups * @see #getGroupById(java.lang.String) * @see #getAgents() */ Set<String> getGroups() { return dmodel.getGroups(); }