/*
* tuProlog - Copyright (C) 2001-2002 aliCE team at deis.unibo.it
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package alice.tuprolog;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import alice.tuprolog.event.OutputEvent;
import alice.tuprolog.event.OutputListener;
/**
* Provides a Prolog virtual machine embedded in a separate thread.
* It needs a theory and optionally a goal.
* It parses the theory, solves the goal and stops.
*
* @see alice.tuprolog.Prolog
*/
public class Agent {
private Prolog core;
private String theoryText;
private InputStream theoryInputStream;
private String goalText;
private OutputListener defaultOutputListener = new OutputListener() {
@Override
public void onOutput(OutputEvent ev) {
System.out.print(ev.getMsg());
}
};
/**
* Builds a prolog agent providing it a theory
*
* @param theory the text representing the theory
*/
public Agent(String theory) {
theoryText = theory;
core = new Prolog();
core.addOutputListener(defaultOutputListener);
}
/**
* Builds a prolog agent providing it a theory and a goal
*/
public Agent(String theory,String goal) {
theoryText = theory;
goalText = goal;
core = new Prolog();
core.addOutputListener(defaultOutputListener);
}
/**
* Constructs the Agent with a theory provided
* by an input stream
*/
public Agent(InputStream is) {
theoryInputStream = is;
core = new Prolog();
core.addOutputListener(defaultOutputListener);
}
/**
* Constructs the Agent with a theory provided
* by an input stream and a goal
*/
public Agent(InputStream is, String goal) {
theoryInputStream = is;
goalText = goal;
core = new Prolog();
core.addOutputListener(defaultOutputListener);
}
/**
* Starts agent execution
*/
final public void spawn() {
new Agent.AgentThread(this).start();
}
/**
* Adds a listener to output events
*
* @param l the listener
*/
public synchronized void addOutputListener(OutputListener l) {
core.addOutputListener(l);
}
/**
* Removes a listener to output events
*
* @param l the listener
*/
public synchronized void removeOutputListener(OutputListener l) {
core.removeOutputListener(l);
}
/**
* Removes all output event listeners
*/
public void removeAllOutputListeners() {
core.removeAllOutputListeners();
}
private void body() {
try {
if (theoryText == null)
core.setTheory(new Theory(theoryInputStream));
else
core.setTheory(new Theory(theoryText));
if (goalText != null)
core.solve(goalText);
} catch (Exception ex) {
System.err.println("Invalid theory or goal.");
ex.printStackTrace();
}
}
/** loads a text file and returns its content as a string */
public static String loadText(String fileName) throws FileNotFoundException {
try {
BufferedInputStream is = new BufferedInputStream(ClassLoader.getSystemResourceAsStream(fileName));
byte[] info = new byte[is.available()];
is.read(info);
return new String(info);
} catch (Exception ex) {
}
// resource not found among system resources: try as a file
try {
FileInputStream is = new FileInputStream(fileName);
byte[] info = new byte[is.available()];
is.read(info);
return new String(info);
} catch (Exception ex) {
}
throw new FileNotFoundException();
}
final class AgentThread extends Thread {
Agent agent;
AgentThread(Agent agent) {
this.agent = agent;
}
@Override
final public void run() {
agent.body();
}
}
public static void main(String args[]) {
if (args.length == 1 || args.length == 2) {
try {
String text = Agent.loadText(args[0]);
if (args.length == 1)
new Agent(text).spawn();
else
new Agent(text, args[1]).spawn();
} catch (Exception ex) {
System.err.println("Invalid theory.");
}
} else {
System.err.println("args: <theory file> { goal }");
System.exit(-1);
}
}
}