All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jason.bb.TextPersistentBB Maven / Gradle / Ivy

Go to download

Jason is a fully-fledged interpreter for an extended version of AgentSpeak, a BDI agent-oriented logic programming language.

There is a newer version: 2.3
Show newest version
package jason.bb;

import jason.asSemantics.Agent;
import jason.asSyntax.Literal;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implementation of BB that stores the agent BB in text files. This
 * implementation is very simple: when the agent starts, load the
 * beliefs in the file; when the agent stops, save the BB in the file.
 * The file name is the agent's name + ".bb".
 */
public class TextPersistentBB extends ChainBBAdapter {
    private static Logger logger = Logger.getLogger(TextPersistentBB.class.getName());

    private File file = null;
    
    public TextPersistentBB() { }
    public TextPersistentBB(BeliefBase next) {
        super(next);
    }

    public void init(Agent ag, String[] args) {
        if (ag != null) {
            try {
                file = new File(ag.getTS().getUserAgArch().getAgName() + ".bb");
                logger.fine("reading from file " + file);
                if (file.exists()) {
                    ag.parseAS(file.getAbsoluteFile());
                    ag.addInitialBelsInBB();
                }
            } catch (Exception e) {
                logger.log(Level.SEVERE,"Error initialising TextPersistentBB.",e);
            }
        }
    }

    public void stop() {
        try {
            logger.fine("writting to file " + file);
            PrintWriter out = new PrintWriter(new FileWriter(file));
            out.println("// BB stored by TextPersistentBB\n");
            for (Literal b: this) {
                if (! b.isRule()) {
                    out.println(b.toString()+".");
                }
            }
            out.close();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error writing BB in file " + file, e);
        }
        nextBB.stop();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy