001: package org.ontoware.rdfreactor.generator;
002:
003: import java.io.File;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007: import org.ontoware.rdf2go.Reasoning;
008: import org.ontoware.rdf2go.model.Model;
009: import org.ontoware.rdfreactor.generator.java.JModel;
010:
011: /**
012: * <b>CodeGenerator</b> is more of a 'model generator'
013: *
014: * The main() and generate() methods should be used from external users to start
015: * the generation of a Java class model representing a given RDFS/OWL schema
016: * file.
017: *
018: * @author voelkel
019: *
020: */
021: public class CodeGenerator {
022:
023: public static final String GENERATOR_VERSION = "$Id: CodeGenerator.java 1046 2008-01-26 14:38:26Z max.at.xam.de $";
024:
025: private static Log log = LogFactory.getLog(CodeGenerator.class);
026:
027: public static final String SEMANTICS_RDFS = "rdfs";
028:
029: public static final String SEMANTICS_OWL = "owl";
030:
031: public static final String SEMANTICS_RDFS_AND_OWL = "rdfs+owl";
032:
033: /**
034: * Generate a Java class model from the given RDFS/OWL schema file
035: *
036: * @param args
037: * schemafilename, outdir, packagename, (RDFS|OWL)
038: * @throws Exception
039: */
040: public static void main(String[] args) throws Exception {
041: generate(args[0], args[1], args[2], args[3], true, true, "");
042: }
043:
044: /**
045: * TODO read from configuration file and respect in template This is already
046: * used in the veloctiy template
047: */
048: @SuppressWarnings("unused")
049: private static boolean readOnly = false;
050:
051: /**
052: * @deprecated Use {@link #generate(String,String,String,Reasoning,boolean)} instead
053: */
054: public static void generate(String schemafilename, String outdir,
055: String packagename, Reasoning semantics,
056: boolean skipbuiltins, boolean alwaysWriteToModel)
057: throws Exception {
058: generate(schemafilename, outdir, packagename, semantics,
059: skipbuiltins);
060: }
061:
062: public static void generate(String schemafilename, String outdir,
063: String packagename, Reasoning semantics,
064: boolean skipbuiltins) throws Exception {
065: generate(schemafilename, outdir, packagename, semantics,
066: skipbuiltins, "");
067: }
068:
069: /**
070: * Generate a Java class model from the given RDFS/OWL schema file
071: *
072: * @param schemafilename
073: * a path to an rdf or owl file in N3, NT or XML syntax. File
074: * extension determines parsing.
075: * @param outdir-
076: * e.g './src' or './gen-src'
077: * @param packagename -
078: * e.g. 'org.ontoware.myname.reactor'
079: * @param semantics -
080: * 'rdfs', 'owl' or 'rdfs+owl' (experimental)
081: * @param skipbuiltins
082: * if false, internal helper classes are re-generated. This is
083: * usually not needed.
084: * @param alwaysWriteToModel
085: * if false, public contructors allow for the choice
086: * @throws Exception
087: * @deprecated Use {@link #generate(String,String,String,Reasoning,boolean,String)} instead
088: */
089: public static void generate(String schemafilename, String outdir,
090: String packagename, Reasoning semantics,
091: boolean skipbuiltins, boolean alwaysWriteToModel,
092: String methodnamePrefix) throws Exception {
093: generate(schemafilename, outdir, packagename, semantics,
094: skipbuiltins, methodnamePrefix);
095: }
096:
097: /**
098: * Generate a Java class model from the given RDFS/OWL schema file
099: *
100: * @param schemafilename
101: * a path to an rdf or owl file in N3, NT or XML syntax. File
102: * extension determines parsing.
103: * @param packagename -
104: * e.g. 'org.ontoware.myname.reactor'
105: * @param semantics -
106: * 'rdfs', 'owl' or 'rdfs+owl' (experimental)
107: * @param skipbuiltins
108: * if false, internal helper classes are re-generated. This is
109: * usually not needed.
110: * @param outdir-
111: * e.g './src' or './gen-src'
112: * @throws Exception
113: */
114: public static void generate(String schemafilename, String outdir,
115: String packagename, Reasoning semantics,
116: boolean skipbuiltins, String methodnamePrefix)
117: throws Exception {
118:
119: // first step
120: Model schemaDataModel = loadSchemaDataModel(schemafilename);
121: File outDir = new File(outdir);
122:
123: generate(schemaDataModel, outDir, packagename, semantics,
124: skipbuiltins, methodnamePrefix);
125: }
126:
127: public static void generate(String schemafilename, String outdir,
128: String packagename, String semantics, boolean skipbuiltins,
129: boolean alwaysWriteToModel, String methodnamePrefix)
130: throws Exception {
131:
132: Reasoning reasoning;
133:
134: if (semantics.equalsIgnoreCase(SEMANTICS_RDFS))
135: reasoning = Reasoning.rdfs;
136: else if (semantics.equalsIgnoreCase(SEMANTICS_OWL))
137: reasoning = Reasoning.owl;
138: else if (semantics.equalsIgnoreCase(SEMANTICS_RDFS_AND_OWL))
139: reasoning = Reasoning.rdfsAndOwl;
140: else
141: throw new RuntimeException("Unknown semantics: '"
142: + semantics + "'");
143:
144: generate(schemafilename, outdir, packagename, reasoning,
145: skipbuiltins, methodnamePrefix);
146: }
147:
148: /**
149: *
150: * @param modelWithSchemaData
151: * a Model in which the ontology to generate Java classes form is
152: * loaded
153: * @param outDir
154: * @param packagename
155: * @param semantics
156: * @param skipbuiltins
157: * @param alwaysWriteToModel
158: * @param methodnamePrefix
159: * @throws Exception
160: * @deprecated Use {@link #generate(Model,File,String,Reasoning,boolean,String)} instead
161: */
162: public static void generate(Model modelWithSchemaData, File outDir,
163: String packagename, Reasoning semantics,
164: boolean skipbuiltins, boolean alwaysWriteToModel,
165: String methodnamePrefix) throws Exception {
166: generate(modelWithSchemaData, outDir, packagename, semantics,
167: skipbuiltins, methodnamePrefix);
168: }
169:
170: /**
171: *
172: * @param modelWithSchemaData
173: * a Model in which the ontology to generate Java classes form is
174: * loaded
175: * @param outDir
176: * @param packagename
177: * @param semantics
178: * @param skipbuiltins
179: * @param methodnamePrefix
180: * @throws Exception
181: */
182: public static void generate(Model modelWithSchemaData, File outDir,
183: String packagename, Reasoning semantics,
184: boolean skipbuiltins, String methodnamePrefix)
185: throws Exception {
186:
187: log.info("using semantics: " + semantics);
188: // different semantics mean different ways to create the internal model
189: JModel jm;
190:
191: // second step
192: if (semantics == Reasoning.rdfs) {
193: log.info("MODEL generating RDFS into "
194: + outDir.getAbsolutePath() + " ...");
195: jm = ModelGenerator.createFromRDFS_Schema(
196: modelWithSchemaData, packagename, skipbuiltins);
197: } else if (semantics == Reasoning.owl) {
198: log.info("generating OWL into " + outDir.getAbsolutePath()
199: + " ...");
200: jm = ModelGenerator.createFromOWL(modelWithSchemaData,
201: packagename, skipbuiltins);
202: } else if (semantics == Reasoning.rdfsAndOwl) {
203: log.info("MODEL generating RDFS+OWL mix semantics into "
204: + outDir.getAbsolutePath() + " ...");
205: jm = ModelGenerator.createFromRDFS_AND_OWL(
206: modelWithSchemaData, packagename, skipbuiltins);
207: } else
208: throw new RuntimeException(
209: "Can't handle the semantics of '" + semantics + "'");
210:
211: // third and final step
212: log.info("write using sourceCodeWriter");
213: SourceCodeWriter.write(jm, outDir,
214: SourceCodeWriter.TEMPLATE_CLASS, methodnamePrefix);
215: }
216:
217: /**
218: * load & deanonymize a schema file into a Jena RDF model
219: *
220: * @param schemafilename
221: * @return Jena RDF Model loaded from given filename
222: * @throws Exception
223: */
224: public static Model loadSchemaDataModel(String schemafilename)
225: throws Exception {
226: File schemaFile = new File(schemafilename);
227: log.info("loading from " + schemaFile.getCanonicalPath());
228: Model schemaDataModel = ModelReaderUtils.read(schemafilename);
229: return schemaDataModel;
230:
231: }
232:
233: }
|