/**
* TimeHero - http://timehero.sf.net
*
* @license See LICENSE
*/
package it.timehero.agent;
import it.timehero.util.AgentInputController;
import it.timehero.world.WorldEngine;
import jason.RevisionFailedException;
import jason.architecture.AgArch;
import jason.asSemantics.ActionExec;
import jason.asSemantics.Agent;
import jason.asSyntax.Literal;
import jason.bb.DefaultBeliefBase;
import jason.runtime.Settings;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Esempio di classe che interagisce con Jason
*/
public class SimpleJasonAgent extends AgArch implements Runnable {
private boolean playermove = false;
private static Logger logger = Logger.getLogger(SimpleJasonAgent.class.getName());
private AgentInputController inputController;
private int cyclenr;
private String ID;
private WorldEngine we;
private String playerDirection;
public SimpleJasonAgent(WorldEngine we,String ID) {
inputController = new AgentInputController(we);
this.we= we;
this.ID=ID;
// console logging Agenti
//RunCentralisedMAS.setupLogger();
// set up the Jason agent
try {
Agent ag = new Agent();
setTS(ag.initAg(this, new DefaultBeliefBase(), "data/agent/example/demo.asl", new Settings()));
} catch (Exception ex) {
logger.log(Level.SEVERE, "Init error", ex);
}
}
public void run() {
logger.info("Starting Agent "+getAgName());
try {
while (isRunning()) {
// calls the Jason engine to perform one reasoning cycle
//logger.info("Reasoning....");
getTS().reasoningCycle();
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Run error", e);
}
}
public String getAgName() {
return "Bob";
}
// this method just add some perception for the agent
@Override
public List<Literal> perceive() {
cyclenr++;
//System.out.println("In perceive " + cyclenr);
double xpos=0, ypos=0;
String str = new String("user_position(" + xpos + "," + ypos + ")[source(self)]");
//Log.info(str);
try {
getTS().getAg().delBel(Literal.parseLiteral(str));
} catch (RevisionFailedException e) {
e.printStackTrace();
}
xpos = we.getPlayerActor().getEntity().getX();
ypos = we.getPlayerActor().getEntity().getY();
List<Literal> l = new ArrayList<Literal>();
l.add(Literal.parseLiteral("user_position(" + xpos + "," + ypos + ")"));
getTS().getLogger().info("user_position(" + xpos + "," + ypos + ")");
return l;
}
// this method get the agent actions
@Override
public void act(ActionExec action, List<ActionExec> feedback) {
//System.out.println("In act " + cyclenr);
getTS().getLogger().info("Agent " + getAgName() + " is doing: " + action.getActionTerm());
if (action.getActionTerm().getFunctor().equals(new String("stand"))) {
//userTurn = true;
//getTS().getLogger().info("Agent " + getAgName() + " stands in its position (" + xpos + "," + ypos + ")");
}
else if (action.getActionTerm().getFunctor().equals(new String("move"))) {
String str = new String("own_position(" + action.getActionTerm().getTerm(0) + "," + action.getActionTerm().getTerm(1) + ")[source(self)]");
//double xpos = Double.parseDouble(action.getActionTerm().getTerm(0).toString());
//double ypos = Double.parseDouble(action.getActionTerm().getTerm(1).toString());
try {
getTS().getAg().delBel(Literal.parseLiteral(str));
//getTS().getLogger().info("To delete " + str);
} catch (RevisionFailedException e) {
getTS().getLogger().info("Agent " + getAgName() + " not able to delete "+ str);
e.printStackTrace();
}
String direction = new String(action.getActionTerm().getTerm(2).toString());
//System.out.println("The direction of the agent is: " + direction);
// notifico la volont� di movimento dell'agente
int delta= -1;
inputController.getInput(direction,delta,ID);
/*
* Here the agent needs to be aware of the action success/failure
*/
/*try {
getTS().getAg().addBel(Literal.parseLiteral(new String("own_position(" + xpos + "," + ypos + ")")));
getTS().getLogger().info("Agent " + getAgName() + " moved to " + "own_position(" + xpos + "," + ypos + ")");
} catch (RevisionFailedException e) {
getTS().getLogger().info("Agent " + getAgName() + " not able to add belief ");
e.printStackTrace();
}*/
//userTurn = true;
}
// set that the execution was ok
action.setResult(true);
feedback.add(action);
}
public void setPlayerMove(String direction){
playermove= true;
playerDirection = direction;
}
@Override
public boolean canSleep() {
return true;
}
@Override
public boolean isRunning() {
return true;
}
// a very simple implementation of sleep
@Override
public void sleep() {
/*
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
*/
}
// Not used methods
// This simple agent does not need messages/control/...
@Override
public void sendMsg(jason.asSemantics.Message m) throws Exception {
}
@Override
public void broadcast(jason.asSemantics.Message m) throws Exception {
}
@Override
public void checkMail() {
}
}