Behavior.java
package prey;
 
/**
 * @author HS
 * 
 */
public abstract class Behavior extends Thread {
    private String name; // The name of the behavior. Used to compare behaviors.
    private int priority; // The (unique) priority of the behavior is used when
                            // two behaviors have the same motivation.
    private boolean running = false; // Setting running to false prevents the
                                        // behavior from performing its action.
    private boolean selfInterruptible; // Some behaviors have to be able to
                                        // interrupt itself (e.g. AvoidObstacle)
                                        // and some should not (e.g. EatFood).
 
    /**
     * @param name
     *            the name of the behavior
     * @param priority
     *            the priority of the behavior
     * @param selfInterruptible
     *            if the behavior is self-interruptible
     */
    public Behavior(String name, int priority, boolean selfInterruptible) {
        this.name = name;
        this.priority = priority;
        this.selfInterruptible = selfInterruptible;
        this.setDaemon(true);
        this.start();
    }
 
    /**
     * @param name
     *            the name of the behavior
     * @param priority
     *            the priority of the behavior
     */
    public Behavior(String name, int priority) {
        this.name = name;
        this.priority = priority;
        this.selfInterruptible = false;
        this.setDaemon(true);
        this.start();
    }
 
    /**
     * @return the name of the behavior
     */
    public String getName() {
        return name;
    }
 
    /**
     * @return the priority of the behavior
     */
    public int getBehaviorPriority() {
        return priority;
    }
 
    /**
     * @return true if the behavior is running
     */
    public boolean isRunning() {
        return running;
    }
 
    /**
     * The behavior tells itself that its action is done.
     */
    protected void actionDone() {
        running = false;
    }
 
    /**
     * Starts the action of the behavior. Called by the Arbitrator.
     */
    public void startAction() {
        running = true;
    }
 
    /**
     * Stops the action of the behavior and interrupts the Mover. Called by the
     * Arbitrator.
     */
    public void stopAction() {
        running = false;
        Mover.interruptBehavior();
    }
 
    /**
     * @param selfInterruptible
     *            true if the behavior is self interruptible
     */
    public void setSelfinterruptible(boolean selfInterruptible) {
        this.selfInterruptible = selfInterruptible;
    }
 
    /**
     * @return true if the behavior is self interruptible
     */
    public boolean isSelfInterruptible() {
        return selfInterruptible;
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Thread#run()
     */
    public void run() {
        // The thread is running all the time, but the behavior is only
        // performing its action when the behavior is running.
        while (true) {
            if (isRunning()) {
                try {
                    action();
                } catch (InterruptBehaviorException e) {
                }
            } else {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    /**
     * Used instead of sleep. This makes it possible to interrupt the sleep.
     * 
     * @param time
     *            duration to sleep
     * @throws InterruptBehaviorException
     *             to stop the entire action
     */
    protected void delay(int time) throws InterruptBehaviorException {
        try {
            int step = 10;
            for (int i = 0; i < time; i += step) {
                Mover.interrupted();
                Thread.sleep(step);
            }
        } catch (InterruptedException e) {}
    }
 
    /**
     * The getMotivation must be implemented in each individual behavior.
     * 
     * @return the current motivation as an int from 0 to 100
     */
    public abstract int getMotivation();
 
    /**
     * The action method is the behaviors actual behavior, so for the behavior
     * AvoidBorder the action is to avoid the border and so on. Everything in
     * the action must be either interruptible such as delay() and calls to the
     * Mover, or have practically no running time. 
     * 
     * If action is a series of maneuvers it should be ended by actionDone().
     * 
     * @throws InterruptBehaviorException
     *             if the action is at some point interrupted
     */
    public abstract void action() throws InterruptBehaviorException;
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License