Download this file

OWLExportCLI.java    172 lines (142 with data), 7.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* 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");
File archiFile = new File(inputFilename);
File outputFile = new File(outputFilename);
if (ontologyIRI == null || ontologyIRI.trim().length() == 0) {
ontologyIRI = OWLExport.getDefaultIRI(outputFile);
System.out.println("Ontology IRI not specified, defaulting to " + ontologyIRI + ", computed from file " + outputFile.getName());
}
// load model
IArchimateModel model = loadModel(archiFile);
System.out.println("Loaded model from " + archiFile.getAbsolutePath());
// convert model
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.");
}
}
}