phat.mason.agents.Agent Maven / Gradle / Ivy
/*
* Copyright (C) 2014 Pablo Campillo-Sanchez
*
* This software has been developed as part of the
* SociAAL project directed by Jorge J. Gomez Sanz
* (http://grasia.fdi.ucm.es/sociaal)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package phat.mason.agents;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;
import phat.mason.PHATSimState;
import phat.mason.agents.automaton.Automaton;
import phat.mason.space.Util;
import phat.structures.houses.House;
import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Bag;
import sim.util.Double3D;
/**
*
* @author Pablo
*/
public abstract class Agent implements Steppable {
protected PhysicsActor physicsActor;
protected Automaton automaton;
private Hashtable listened=new Hashtable();
private static Vector instances=new Vector();
PHATSimState state;
public static void shout(String word,Double3D location){
for (Agent instance:instances)
instance.listened(word,location);
}
public Agent(PhysicsActor physicsActor, PHATSimState state) {
this.physicsActor = physicsActor;
physicsActor.putAgent(this);
instances.add(this);
this.state=state;
initAutomaton();
}
protected Double3D haveIHeard(String word) {
Vector candidates=new Vector();
for (String c:listened.keySet()){
if (c.toLowerCase().indexOf(word.toLowerCase())>=0)
candidates.add(c);
}
if (candidates.isEmpty())
return null;
return listened.get(candidates.firstElement());
}
public void listened(String word, Double3D source){
this.listened.put(word,source);
}
@Override
public void step(SimState ss) {
PHATSimState simState = (PHATSimState) ss;
if(automaton != null) {
automaton.nextState(simState);
}
}
public String getName() {
return physicsActor.getName();
}
public PhysicsActor getPhysicsActor() {
return physicsActor;
}
public void setPhysicsActor(PhysicsActor physicsActor) {
this.physicsActor = physicsActor;
}
public String getCurrentAction() {
if(automaton != null) {
return automaton.getCurrentState().getName();
}
return "";
}
abstract protected void initAutomaton();
public Automaton getAutomaton() {
return automaton;
}
public void setAutomaton(Automaton automaton) {
this.automaton = automaton;
}
public ArrayList getAgentInSameLocation() {
ArrayList otherAgents=new ArrayList ();
Bag bag = physicsActor.world().getObjectsAtLocation(this.getPhysicsActor().getLocation());
for(int i = 0; i < bag.numObjs; i++) {
if(bag.get(i) instanceof PhysicsActor) {
PhysicsActor pa = (PhysicsActor)bag.get(i);
if(pa.agent() != this) {
otherAgents.add(pa.agent());
}
}
}
return otherAgents;
}
public ArrayList getAgentNearThan(float distance) {
ArrayList otherAgents=new ArrayList ();
Double3D loc = getPhysicsActor().getLocation();
Bag bag = getPhysicsActor().world().getAllObjects();
for(int i = 0; i < bag.numObjs; i++) {
if(bag.get(i) instanceof PhysicsActor) {
PhysicsActor pa = (PhysicsActor)bag.get(i);
if(pa.agent() != this && loc.distance(pa.getLocation()) < distance) {
otherAgents.add(pa.agent());
}
}
}
return otherAgents;
}
private boolean sameRoom(PhysicsActor location1, PhysicsActor location2) {
House house = state.getMasonAppState().getHouseAdapter().getHouse();
String room1=house.getRoomForObject(location1.getName());
String room2=house.getRoomForObject(location2.getName());
//Double3D loc = Util.get(masonAppState.getHouseAdapter().getHouse().getCoordenates("Kitchen", "Hob"));
return room1.equals(room2);
}
}