00001 import java.io.File;
00002 import java.io.PrintStream;
00003 import java.util.Properties;
00004
00005 import org.python.core.PyObject;
00006 import org.python.util.PythonInterpreter;
00007 import edu.tum.cs.srldb.Database;
00008
00009
00010 public class genDB {
00011
00015 public static void main(String[] args) {
00016 try {
00017 if(args.length < 3) {
00018 System.out.println("\ngenDB: A database generator for MLNs and BLOG models.");
00019 System.out.println("\n usage: genDB [options] <Jython generator script> <output base filename> [parameters to pass on to generator]\n" +
00020 " -m output MLN database (.db)\n" +
00021 " -b output BLOG database (.blogdb)\n" +
00022 " -bm output basic MLN model (.basic.mln)\n" +
00023 " -bb output basic BLN model declarations (.basic.blnd)\n\n" +
00024 " The Jython script must create a Database object named 'db' in the global scope.\n");
00025 return;
00026 }
00027
00028 Properties props = new Properties();
00029
00030
00031 String jythonpath = System.getenv("JYTHONPATH");
00032 if(jythonpath == null) {
00033 System.err.println("Warning: JYTHONPATH environment variable not set. If modules such as 'datagen' cannot be imported, either manually set sys.path in your generator scripts to include the appropriate directories or set this variable to include ProbCog's 'python' directory.");
00034 jythonpath = "";
00035 }
00036 else
00037 jythonpath += File.pathSeparator;
00038 jythonpath += System.getProperty("java.class.path");
00039 props.put("python.path", jythonpath);
00040 Properties sysprops = System.getProperties();
00041 PythonInterpreter.initialize(sysprops, props, null);
00042 PythonInterpreter jython = new PythonInterpreter();
00043
00044
00045 boolean writeBasicBLND = false, writeBasicMLN = false, writeBLOGDB = false, writeMLNDB = false;
00046 int i = 0;
00047 for(; i < args.length; i++) {
00048 if(args[i].equals("-m"))
00049 writeMLNDB = true;
00050 else if(args[i].equals("-b"))
00051 writeBLOGDB = true;
00052 else if(args[i].equals("-bm"))
00053 writeBasicMLN = true;
00054 else if(args[i].equals("-bb"))
00055 writeBasicBLND = true;
00056 else
00057 break;
00058 }
00059
00060 jython.exec("import sys");
00061 String jythonscript = args[i++];
00062 jython.exec("sys.argv.append('" + jythonscript + "')");
00063 String outfilename = args[i++];
00064 for(; i < args.length; i++) {
00065 jython.exec("sys.argv.append('" + args[i] + "')");
00066 }
00067
00068 jython.execfile(jythonscript);
00069 PyObject dbObj = jython.get("db");
00070 if(dbObj == null) {
00071 System.err.println("Error: Generator script does not define 'db' object!");
00072 return;
00073 }
00074 Database db = (Database) dbObj.__tojava__(Database.class);
00075 db.check();
00076 if(writeMLNDB)
00077 db.writeMLNDatabase(new PrintStream(new java.io.File(outfilename + ".db")));
00078 if(writeBLOGDB)
00079 db.writeBLOGDatabase(new PrintStream(new java.io.File(outfilename + ".blogdb")));
00080 if(writeBasicMLN)
00081 db.writeBasicMLN(new PrintStream(new File(outfilename + ".basic.mln")));
00082 if(writeBasicBLND)
00083 db.getDataDictionary().writeBasicBLOGModel(new PrintStream(new File(outfilename + ".basic.blnd")));
00084 System.out.println("done!");
00085 }
00086 catch(Exception e) {
00087 e.printStackTrace();
00088 }
00089 }
00090
00091 }