001: package org.ontoware.rdf2go.impl.jena24;
002:
003: import java.util.Properties;
004:
005: import org.ontoware.rdf2go.ModelFactory;
006: import org.ontoware.rdf2go.Reasoning;
007: import org.ontoware.rdf2go.exception.ModelRuntimeException;
008: import org.ontoware.rdf2go.impl.AbstractModelFactory;
009: import org.ontoware.rdf2go.model.Model;
010: import org.ontoware.rdf2go.model.ModelSet;
011: import org.ontoware.rdf2go.model.node.URI;
012:
013: import com.hp.hpl.jena.db.DBConnection;
014: import com.hp.hpl.jena.db.IDBConnection;
015: import com.hp.hpl.jena.ontology.OntModelSpec;
016: import com.hp.hpl.jena.rdf.model.ModelMaker;
017:
018: public class ModelFactoryImpl extends AbstractModelFactory implements
019: ModelFactory {
020:
021: public static final String BACKEND = "back-end";
022:
023: public static final String MEMORY = "memory";
024:
025: public static final String DATABASE = "database";
026:
027: public static final String SQL_DRIVERNAME = "sql_drivername";
028:
029: public static final String DB_TYPE = "db_type";
030:
031: public static final String DB_CONNECT_STRING = "db_connect_string";
032:
033: public static final String DB_USER = "db_user";
034:
035: public static final String DB_PASSWD = "db_passwd";
036:
037: public static final String FILE = "file";
038:
039: public static final String FILENAME = "filename";
040:
041: public Model createModel(Properties p) throws ModelRuntimeException {
042: com.hp.hpl.jena.rdf.model.Model model;
043:
044: String backend = p.getProperty(BACKEND);
045:
046: // default to in-memory model
047: if (backend == null)
048: backend = MEMORY;
049:
050: Reasoning reasoning = AbstractModelFactory.getReasoning(p);
051:
052: if (backend == null || backend.equalsIgnoreCase(MEMORY)) {
053: model = com.hp.hpl.jena.rdf.model.ModelFactory
054: .createDefaultModel();
055: assert model != null;
056: } else if (backend.equalsIgnoreCase(DATABASE)) {
057: String sql_drivername = p.getProperty(SQL_DRIVERNAME);
058: String db_type = p.getProperty(DB_TYPE);
059: String connect_string = p.getProperty(DB_CONNECT_STRING);
060: String user = p.getProperty(DB_USER);
061: String password = p.getProperty(DB_PASSWD);
062:
063: if (sql_drivername == null)
064: throw new RuntimeException(
065: "Please specify a sql_drivername in your property file!");
066: if (db_type == null)
067: throw new RuntimeException(
068: "Please specify a db_type in your property file!");
069: if (connect_string == null)
070: throw new RuntimeException(
071: "Please specify a connect_string in your property file!");
072: if (user == null)
073: throw new RuntimeException(
074: "Please specify a db_user in your property file!");
075: if (password == null)
076: throw new RuntimeException(
077: "Please specify a db_passwd in your property file!");
078:
079: ModelMaker maker;
080: try {
081: maker = getDBModelMaker(sql_drivername, connect_string,
082: user, password, db_type);
083: } catch (Exception e) {
084: throw new ModelRuntimeException(e);
085: }
086:
087: model = maker.createDefaultModel();
088:
089: } else if (backend.equalsIgnoreCase(FILE)) {
090: String filename = p.getProperty(FILENAME);
091: if (filename == null) {
092: throw new RuntimeException(
093: "Please specify a filename in your property file!");
094: }
095: ModelMaker maker = getFileModelMaker(filename);
096: model = maker.createDefaultModel();
097: } else
098: throw new IllegalArgumentException(
099: "Illegal back-end type: " + backend);
100:
101: // reasoning
102:
103: switch (reasoning) {
104: case rdfsAndOwl:
105: case owl:
106: com.hp.hpl.jena.rdf.model.Model owlModel = (com.hp.hpl.jena.rdf.model.Model) com.hp.hpl.jena.rdf.model.ModelFactory
107: .createOntologyModel(
108: OntModelSpec.OWL_DL_MEM_RDFS_INF, model);
109: return new ModelImplJena24(owlModel);
110: case rdfs:
111: com.hp.hpl.jena.rdf.model.Model rdfsModel = (com.hp.hpl.jena.rdf.model.Model) com.hp.hpl.jena.rdf.model.ModelFactory
112: .createRDFSModel(model);
113: return new ModelImplJena24(rdfsModel);
114: default:
115: return new ModelImplJena24(model);
116: }
117:
118: }
119:
120: public Model createModel(URI contextURI)
121: throws ModelRuntimeException {
122: com.hp.hpl.jena.rdf.model.Model model = com.hp.hpl.jena.rdf.model.ModelFactory
123: .createDefaultModel();
124: return new ModelImplJena24(contextURI, model);
125: }
126:
127: private ModelMaker getDBModelMaker(String SQLDriver,
128: String DB_connectString, String user, String password,
129: String db_type) throws Exception {
130: Class.forName(SQLDriver);
131: IDBConnection conn = new DBConnection(DB_connectString, user,
132: password, db_type);
133: return com.hp.hpl.jena.rdf.model.ModelFactory
134: .createModelRDBMaker(conn);
135: }
136:
137: private ModelMaker getFileModelMaker(String filename) {
138: return com.hp.hpl.jena.rdf.model.ModelFactory
139: .createFileModelMaker(filename);
140: }
141:
142: public ModelSet createModelSet(Properties p)
143: throws ModelRuntimeException {
144: // TODO not available in Jena, TODO: implement using NG4J
145: throw new UnsupportedOperationException(
146: "not available in Jena, TODO: implement using NG4J");
147: }
148:
149: }
|