00001 package edu.tum.cs.probcog;
00002
00003 import java.io.File;
00004 import java.io.IOException;
00005 import java.util.HashMap;
00006
00007 import javax.xml.parsers.SAXParserFactory;
00008
00009 import org.xml.sax.Attributes;
00010 import org.xml.sax.ErrorHandler;
00011 import org.xml.sax.SAXException;
00012 import org.xml.sax.SAXParseException;
00013 import org.xml.sax.helpers.DefaultHandler;
00014
00015 import edu.tum.cs.logic.parser.ParseException;
00016
00017 public class ModelPool {
00018 protected HashMap<String, Model> pool;
00019 protected File poolPath;
00020
00021 public ModelPool(String poolFilename) throws IOException, ParseException, Exception {
00022 pool = new HashMap<String, Model>();
00023
00024 File poolFile = new File(poolFilename);
00025 poolPath = poolFile.getParentFile();
00026
00027 SAXParserFactory factory = SAXParserFactory.newInstance();
00028 factory.setValidating(true);
00029 factory.newSAXParser().parse(poolFile, new PoolReader());
00030 }
00031
00032 public Model getModel(String name) {
00033 return pool.get(name);
00034 }
00035
00039 protected class PoolReader extends DefaultHandler implements ErrorHandler {
00040 protected class ModelData {
00041 String name, type, path;
00042 HashMap<String, String> files;
00043 HashMap<String, String> params;
00044 HashMap<String, String> constantMap;
00045
00046 public ModelData() {
00047 files = new HashMap<String,String>();
00048 params = new HashMap<String,String>();
00049 constantMap = new HashMap<String,String>();
00050 }
00051
00052 private void checkFileTypes(String[] requiredTypes) throws Exception {
00053 for(String t : requiredTypes) {
00054 if(!files.containsKey(t))
00055 throw new Exception(String.format("Missing file of type '%s' for model '%s'", t, name));
00056 }
00057 }
00058
00059 public Model instantiate() throws Exception {
00060 Model m;
00061 if(name == null)
00062 throw new Exception("Model has no 'name' attribute.");
00063 System.out.println("Loading model " + name + "...");
00064
00065 File fPath;
00066 if(path == null)
00067 fPath = poolPath;
00068 else
00069 fPath = new File(path);
00070 if(!fPath.isAbsolute())
00071 fPath = new File(poolPath, path);
00072
00073 if(type.equals("BLN")) {
00074 checkFileTypes(new String[]{"network", "decls", "logic"});
00075 m = new BLNModel(name, new File(fPath, files.get("decls")).getPath(), new File(fPath, files.get("network")).getPath(), new File(fPath, files.get("logic")).getPath());
00076 }
00077 else if(type.equals("MLN")) {
00078 checkFileTypes(new String[]{"network"});
00079 m = new MLNModel(name, new File(fPath, files.get("network")).getPath());
00080 }
00081 else
00082 throw new Exception(String.format("Unknown model type '%s'", type));
00083 m.setConstantMap(constantMap);
00084 return m;
00085 }
00086 }
00087
00088 ModelData currentModel;
00089
00090 public void startElement(String uri, String name, String qName, Attributes attrs) {
00091 if(qName.equals("model")) {
00092 currentModel = new ModelData();
00093 for(int i = 0; i < attrs.getLength(); i++) {
00094 String attrName = attrs.getQName(i);
00095 if(attrName.equals("name"))
00096 currentModel.name = attrs.getValue(i);
00097 else if(attrName.equals("type"))
00098 currentModel.type = attrs.getValue(i);
00099 else if(attrName.equals("path"))
00100 currentModel.path = attrs.getValue(i);
00101 else
00102 throw new RuntimeException(String.format("Unhandled attribute '%s' of model.", attrName));
00103 }
00104 }
00105 else if(qName.equals("file")) {
00106 String type = attrs.getValue("type");
00107 String filename = attrs.getValue("name");
00108 currentModel.files.put(type, filename);
00109 }
00110 else if(qName.equals("constantMap")) {
00111 String from = attrs.getValue("from");
00112 String to = attrs.getValue("to");
00113 currentModel.constantMap.put(from, to);
00114 }
00115 }
00116
00117 public void endElement(String uri, String name, String qName) {
00118 try {
00119 if(qName.equals("model"))
00120 pool.put(currentModel.name, currentModel.instantiate());
00121 }
00122 catch (Exception e) {
00123 throw new RuntimeException(e.getMessage());
00124 }
00125 }
00126
00127 public void warning(SAXParseException e) throws SAXException {
00128 throw e;
00129 }
00130
00131 public void error(SAXParseException e) throws SAXException {
00132 throw e;
00133 }
00134
00135 public void fatalError(SAXParseException e) throws SAXException {
00136 throw e;
00137 }
00138 }
00139 }