/**
* Copyright (c) 2013/2014 Verein zur Foerderung der IT-Sicherheit in Oesterreich (SBA).
* The work has been developed in the TIMBUS Project and the above-mentioned are Members of the TIMBUS Consortium.
* TIMBUS is supported by the European Union under the 7th Framework Programme for research and technological
* development and demonstration activities (FP7/2007-2013) under grant agreement no. 269940.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including without
* limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTIBITLY, or FITNESS FOR A PARTICULAR
* PURPOSE. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise,
* unless required by applicable law or agreed to in writing, shall any Contributor be liable for damages, including
* any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this
* License or out of the use or inability to use the Work.
* See the License for the specific language governing permissions and limitation under the License.
*/
package net.timbusproject.context.converter;
import java.io.File;
import java.io.IOException;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.osgi.util.NLS;
import com.archimatetool.editor.model.IArchiveManager;
import com.archimatetool.editor.model.compatibility.IncompatibleModelException;
import com.archimatetool.editor.model.compatibility.ModelCompatibility;
import com.archimatetool.editor.model.impl.EditorModelManager;
import com.archimatetool.model.IArchimateModel;
import com.archimatetool.model.IArchimatePackage;
import com.archimatetool.model.util.ArchimateResourceFactory;
/**
* Export an Archimate Model to OWL (Web Ontology Language) as standalone application
*
* @author Rudolf Mayer (rmayer@sba-research.org)
*/
public class OWLExportCLI {
/** Adapted from {@link EditorModelManager#loadModel(File)}, to work in a stand-alone setting */
public static IArchimateModel loadModel(File file) {
if (file == null || !file.exists()) {
return null;
}
ResourceSet resourceSet = ArchimateResourceFactory.createResourceSet();
// Changed from EditorModelManager#loadModel(File): need register the Archimate package, if that's not done yet
if (!resourceSet.getPackageRegistry().containsKey(IArchimatePackage.eNS_URI)) {
resourceSet.getPackageRegistry().put(IArchimatePackage.eNS_URI, IArchimatePackage.eINSTANCE);
}
// to allow backwards compability, also register the OLD URI
String OLD_eNS_URI = "http://www.bolton.ac.uk/archimate";
if (!resourceSet.getPackageRegistry().containsKey(OLD_eNS_URI)) {
resourceSet.getPackageRegistry().put(OLD_eNS_URI, IArchimatePackage.eINSTANCE);
}
// Ascertain if this is an archive file
boolean useArchiveFormat = IArchiveManager.FACTORY.isArchiveFile(file);
// Create the Resource
Resource resource = resourceSet.createResource(useArchiveFormat ? IArchiveManager.FACTORY
.createArchiveModelURI(file) : URI.createFileURI(file.getAbsolutePath()));
// Load the model file
try {
resource.load(null);
} catch (IOException ex) {
// Error occured loading model. Was it a disaster?
try {
ModelCompatibility.checkErrors(resource);
}
// Incompatible, don't load it
catch (IncompatibleModelException ex1) {
System.err.println("Messages.EditorModelManager_2" + ": "
+ NLS.bind("Messages.EditorModelManager_3", file) + "\n" + ex1.getMessage());
return null;
}
}
// Changed: skip "Once loaded - Check version number compatibility with user"
// Changed: skip the following, as we don't have the extension registry set up
// // And then fix any backward compatibility issues
// try {
// ModelCompatibility.fixCompatibility(resource);
// } catch (CompatibilityHandlerException ex) {
// }
IArchimateModel model = (IArchimateModel) resource.getContents().get(0);
model.setFile(file);
model.setDefaults();
return model;
}
public static void main(String[] args) throws IOException, ParseException {
// create CLI Options
Options options = new Options();
Option optionInput = new Option("i", "input", true, "The Archi archimate model to convert");
optionInput.setType(new String());
optionInput.setRequired(true);
options.addOption(optionInput);
Option optionOutput = new Option("o", "output", true, "The name of the OWL output file");
optionOutput.setType(new String());
optionOutput.setRequired(true);
options.addOption(optionOutput);
options.addOption(new Option("v", "view", true,
"The layout of the view to be exported. If ommitted, the layered view will be tried next. "
+ "If the layered view is not found, and there is only one view, that view will be used."));
options.addOption(new Option(null, "iri", true,
"The IRI of the generated ontology. If not specified, a default IRI will be generated"));
// parse options
CommandLineParser parser = new BasicParser();
CommandLine cmd;
try {
cmd = parser.parse(options, args);
} catch (Exception e) {
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(OWLExportPlugin.class.getName(), options);
return;
}
String inputFilename = cmd.getOptionValue("input");
String outputFilename = cmd.getOptionValue("output");
String ontologyIRI = cmd.getOptionValue("iri");
// load model
File archiFile = new File(inputFilename);
IArchimateModel model = loadModel(archiFile);
System.out.println("Loaded model from " + archiFile.getAbsolutePath());
// convert model
File outputFile = new File(outputFilename);
OWLExport.exportToOwl(model, outputFile, ontologyIRI);
System.out.println("Wrote converted file to " + outputFile.getAbsolutePath());
// also export the layout
File layoutFile = OWLExport.getDefaultLayoutFile(outputFile);
if (OWLExport.exportLayout(model, layoutFile, ontologyIRI, cmd.getOptionValue("view"))) {
System.out.println("Wrote layout file to " + layoutFile.getAbsolutePath());
} else {
System.out.println("Didn't export layout.");
}
}
}