package ont.diagram.handlers;
import java.net.URI;
import java.net.URL;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.mwe.core.WorkflowRunner;
import org.eclipse.emf.mwe.core.monitor.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.xpand2.XpandExecutionContextImpl;
import org.eclipse.xpand2.output.Outlet;
import org.eclipse.xpand2.output.Output;
import org.eclipse.xpand2.output.OutputImpl;
import org.eclipse.xtend.typesystem.emf.EmfMetaModel;
import org.osgi.framework.Bundle;
import ont.OntPackage;
public class GenerateTurtleAction implements IObjectActionDelegate {
private Shell shell;
private IWorkbenchPart targetPart;
private IFile file;
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
this.targetPart = targetPart;
shell = targetPart.getSite().getShell();
}
public void selectionChanged(IAction action, ISelection selection) {
action.setEnabled(false);
if (selection instanceof IStructuredSelection == false || selection.isEmpty()) {
return;
}
file = (IFile) ((IStructuredSelection) selection).getFirstElement();
action.setEnabled(true);
}
public void run(IAction action) {
try {
EObject source = getInput();
if (source == null) {
String title = "title";
String message = "message";
MessageDialog.openInformation(getShell(), title, NLS.bind(message, file.getFullPath()));
} else {
Bundle bundle = Platform.getBundle("ontology.diagram");
URL url = FileLocator.find(bundle, new Path("src/templates/Generator.mwe"), null);
url = FileLocator.toFileURL(url);
// URL url = FileLocator.toFileURL(new URL("platform:/resource/ontology.diagram/src/templates/Generator.mwe"));
Map<String, String> properties = new HashMap<String, String>();
properties.put("model", URI.createPlatformResourceURI(file.getFullPath().toString(), true).toString());
properties.put("out", Platform.getLocation().toOSString() + file.getParent().getFullPath());
WorkflowRunner runner = new WorkflowRunner();
runner.run(url.getPath(), new NullProgressMonitor(), properties, null);
file.getParent().refreshLocal(IResource.DEPTH_ONE, new org.eclipse.core.runtime.NullProgressMonitor());
MessageDialog.openInformation(
shell,
"Generate Turtle",
"Successful!");
}
} catch (Exception ex) {
handleError(ex);
}
}
private EObject getInput() {
ResourceSetImpl rs = new ResourceSetImpl();
return rs.getEObject(URI.createPlatformResourceURI(file.getFullPath().toString(), true).appendFragment("/"), true);
}
private void handleError(Throwable ex) {
MessageDialog.openError(getShell(), "Transformation failed", MessageFormat.format("{0}: {1}", ex.getClass().getSimpleName(), ex.getMessage() == null ? "no message" : ex.getMessage()));
}
private Shell getShell() {
return targetPart.getSite().getShell();
}
}
«IMPORT http:///ont.ecore»
«DEFINE main FOR Ontology-»
«FILE ontologyDescription.ontLabel + ".txt"-»
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix base <«ontologyDescription.ontURI»#> .
#################################################################
#
# Classes
#
#################################################################
«EXPAND rdfclass FOREACH rDFClasses-»
#################################################################
#
# Properties
#
#################################################################
«EXPAND rdfproperty FOREACH rDFProperties-»
«ENDFILE»
«ENDDEFINE»
«DEFINE rdfclass FOR RDFSClass-»
«classLabel» rdf:type rdfs:Class «EXPAND subclass FOREACH subClassOf-».
«ENDDEFINE-»
«DEFINE subclass FOR RDFSClass-»
;
rdfs:subClassOf «classLabel-»
«ENDDEFINE-»
«DEFINE rdfproperty FOR RDFSProperty-»
«propertyLabel» rdf:type rdf:Property «EXPAND subproperty FOREACH subPropertyOf-» «EXPAND dom FOREACH domain-» «EXPAND ran FOREACH range-».
«ENDDEFINE-»
«DEFINE subproperty FOR RDFSProperty-»
;
rdfs:subPropertyOf «propertyLabel-»
«ENDDEFINE-»
«DEFINE dom FOR RDFSClass-»
;
rdfs:domain «classLabel-»
«ENDDEFINE-»
«DEFINE ran FOR RDFSClass-»
;
rdfs:range «classLabel-»
«ENDDEFINE-»
<?xml version="1.0"?>
<workflow>
<bean class="org.eclipse.emf.mwe.utils.StandaloneSetup">
<platformUri value="../" />
<RegisterEcoreFile value="http:///ont.ecore"/>
</bean>
<component class="org.eclipse.emf.mwe.utils.Reader">
<uri value="${model}" />
<modelSlot value="model" />
<firstElementOnly value="false" />
</component>
<component class="org.eclipse.xpand2.Generator">
<metaModel id="mm" class="org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel"/>
<expand value="templates::TurtleTemplate::main FOR model" />
<outlet path="${}">
<postprocessor class="org.eclipse.xpand2.output.XmlBeautifier" />
</outlet>
</component>
</workflow>