Package it.timehero.agent

Source Code of it.timehero.agent.SimpleJasonAgent

/**
* 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 static Logger logger = Logger.getLogger(SimpleJasonAgent.class.getName());
    private int cyclenr;

    private String ID;
    private AgentInputController inputController;
    private String playerDirection;
       
    private boolean playermove = false;
  private WorldEngine we;
   
    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);
        }
    }

    // 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);
       
    }

    @Override
    public void broadcast(jason.asSemantics.Message m) throws Exception {
    }

    @Override
    public boolean canSleep() {
        return true;
    }

    @Override
    public void checkMail() {
    }

    public String getAgName() {
        return "Bob";
       
    }
   
    @Override
    public boolean isRunning() {
        return true;
    }

    // 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;
     
    }

    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);
        }
    }
   
    // Not used methods
    // This simple agent does not need messages/control/...
    @Override
    public void sendMsg(jason.asSemantics.Message m) throws Exception {
    }

    public void setPlayerMove(String direction){
      playermove= true;
      playerDirection = direction;
    }

    // a very simple implementation of sleep
    @Override
    public void sleep() {
      /*
      try {
      Thread.sleep(2000);
    } catch (InterruptedException e) {}
    */
    }

}
TOP

Related Classes of it.timehero.agent.SimpleJasonAgent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.