From 8c7637425667bd9710be0fe6afe437050fea59b7 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Thu, 13 Sep 2018 13:26:37 +0300 Subject: [PATCH] Generate SCL bindings for multi-version ontology references classes. * Additionally, added SCL bindings to XML time/date data types gitlab #5 Change-Id: Ia1b5786becf99b691a66d48b63bd2689cb646d9a --- .../scl/xml/XMLDataTypes.scl | 48 +++ .../sax/ui/wizard/SchemaCombinationPage.java | 336 ++++++++-------- .../simantics/xml/sax/OntologyCombinator.java | 365 +++++++++--------- .../org/simantics/xml/sax/SCLCombinator.java | 49 +++ 4 files changed, 459 insertions(+), 339 deletions(-) create mode 100644 org.simantics.xml.sax.base/scl/xml/XMLDataTypes.scl create mode 100644 org.simantics.xml.sax/src/org/simantics/xml/sax/SCLCombinator.java diff --git a/org.simantics.xml.sax.base/scl/xml/XMLDataTypes.scl b/org.simantics.xml.sax.base/scl/xml/XMLDataTypes.scl new file mode 100644 index 0000000..cf62097 --- /dev/null +++ b/org.simantics.xml.sax.base/scl/xml/XMLDataTypes.scl @@ -0,0 +1,48 @@ +importJava "org.simantics.xml.sax.base.datatypes.literal.Date" where + data XMLDate + + @JavaName getYear + dateYear :: XMLDate -> Integer + @JavaName getMonth + dateMonth :: XMLDate -> Integer + @JavaName getDate + dateDay :: XMLDate -> Integer + @JavaName getTimezoneOffset + dateTimezoneOffset :: XMLDate -> Integer + @JavaName toString + dateAsString :: XMLDate -> String + +importJava "org.simantics.xml.sax.base.datatypes.literal.Time" where + data XMLTime + + @JavaName getHours + timeHours :: XMLTime -> Integer + @JavaName getMinutes + timeMinutes :: XMLTime -> Integer + @JavaName getSeconds + timeSeconds :: XMLTime -> Integer + @JavaName getTimezoneOffset + timeTimezoneOffset :: XMLTime -> Integer + @JavaName toString + timeAsString :: XMLTime -> String + + +importJava "org.simantics.xml.sax.base.datatypes.literal.DateTime" where + data XMLDateTime + + @JavaName getYear + dateTimeYear :: XMLDateTime -> Integer + @JavaName getMonth + dateTimeMonth ::XMLDateTime -> Integer + @JavaName getDate + dateTimeDay :: XMLDateTime -> Integer + @JavaName getHours + dateTimeHours :: XMLDateTime -> Integer + @JavaName getMinutes + dateTimeMinutes :: XMLDateTime -> Integer + @JavaName getSeconds + dateTimeSeconds :: XMLDateTime -> Integer + @JavaName getTimezoneOffset + dateTimeTimezoneOffset :: XMLDateTime -> Integer + @JavaName toString + dateTimeAsString :: XMLDateTime -> String diff --git a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java index 1539f50..dd5d97f 100644 --- a/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java +++ b/org.simantics.xml.sax.ui/src/org/simantics/xml/sax/ui/wizard/SchemaCombinationPage.java @@ -1,158 +1,178 @@ -package org.simantics.xml.sax.ui.wizard; - -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.util.Arrays; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.viewers.CheckboxTableViewer; -import org.eclipse.jface.viewers.IStructuredContentProvider; -import org.eclipse.jface.viewers.ITableLabelProvider; -import org.eclipse.jface.viewers.LabelProvider; -import org.eclipse.jface.viewers.Viewer; -import org.eclipse.jface.wizard.WizardPage; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.SelectionAdapter; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.layout.GridLayout; -import org.eclipse.swt.widgets.Button; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Text; -import org.simantics.xml.sax.OntologyCombinator; - -public class SchemaCombinationPage extends WizardPage { - private Composite composite; - - File outputPlugin; - File srcDir; - - CheckboxTableViewer fileSelector; - Text outputText; - - public SchemaCombinationPage() { - super("XML Schema conversion","Combining schema versions", null); - setPageComplete(false); - } - - @Override - public void setVisible(boolean visible) { - super.setVisible(visible); - if (visible) { - if (outputPlugin == null) { - Label label = new Label(composite, SWT.NONE); - label.setText("Output plug-in has not been set, cannot create combination resources"); - return; - } - String name = outputPlugin.getName(); - name = name.replaceAll("\\.", "/"); - srcDir = new File(outputPlugin.getAbsolutePath() +"/src/" + name); - fileSelector.setInput(srcDir); - } - } - - - @Override - public void createControl(Composite parent) { - composite = new Composite(parent, SWT.NONE); - composite.setLayout(new GridLayout(1,true)); - setControl(composite); - - Label label = new Label(composite, SWT.NONE); - label.setText("Ontology resource files"); - fileSelector = CheckboxTableViewer.newCheckList(composite, SWT.BORDER); - fileSelector.setContentProvider(new FileContentProvider()); - fileSelector.setLabelProvider(new FileLabelProvider()); - - label = new Label(composite, SWT.NONE); - label.setText("Output file"); - outputText = new Text(composite, SWT.BORDER|SWT.SINGLE); - Button button = new Button(composite, SWT.PUSH); - button.setText("Combine"); - button.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - doCombination(); - } - }); - - GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(fileSelector.getControl()); - GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(outputText); - GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(button); - } - - public void setOutputPlugin(File outputPlugin) { - this.outputPlugin = outputPlugin; - } - - private void doCombination() { - String outputName = outputText.getText(); - if (outputName == null || outputName.length() < 2) - return; - if (!outputName.endsWith(".java")) - outputName+=".java"; - - File outputFile = new File(srcDir.getAbsolutePath()+"/"+outputName); - - Object sel[] = fileSelector.getCheckedElements(); - File inputFiles[] = new File[sel.length]; - for (int i = 0; i < sel.length; i++) { - inputFiles[i] = (File)sel[i]; - } - - try { - OntologyCombinator.combine(inputFiles, outputFile); - setPageComplete(true); - } catch (IOException e) { - setErrorMessage(e.getMessage()); - e.printStackTrace(); - } - - } - - private static class FileContentProvider implements IStructuredContentProvider { - @Override - public Object[] getElements(Object inputElement) { - File directory = (File)inputElement; - if (!directory.isDirectory()) - return new Object[0]; - File[] files = directory.listFiles(new FilenameFilter() { - - @Override - public boolean accept(File dir, String name) { - return name.endsWith("java"); - } - }); - return files; - } - - @Override - public void dispose() { - - } - - @Override - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - - } - } - - private static class FileLabelProvider extends LabelProvider implements ITableLabelProvider { - @Override - public Image getColumnImage(Object element, int columnIndex) { - return null; - } - - @Override - public String getColumnText(Object element, int columnIndex) { - File file = (File)element; - if (columnIndex == 0) - return file.getName(); - else - return null; - } - } - -} +package org.simantics.xml.sax.ui.wizard; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.Arrays; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.viewers.CheckboxTableViewer; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.ITableLabelProvider; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Text; +import org.simantics.xml.sax.OntologyCombinator; +import org.simantics.xml.sax.SCLCombinator; + +public class SchemaCombinationPage extends WizardPage { + private Composite composite; + + File outputPlugin; + File srcDir; + + CheckboxTableViewer fileSelector; + Text javaOutputText; + Text sclOutputText; + + public SchemaCombinationPage() { + super("XML Schema conversion","Combining schema versions", null); + setPageComplete(false); + } + + @Override + public void setVisible(boolean visible) { + super.setVisible(visible); + if (visible) { + if (outputPlugin == null) { + Label label = new Label(composite, SWT.NONE); + label.setText("Output plug-in has not been set, cannot create combination resources"); + return; + } + String name = outputPlugin.getName(); + name = name.replaceAll("\\.", "/"); + srcDir = new File(outputPlugin.getAbsolutePath() +"/src/" + name); + fileSelector.setInput(srcDir); + } + } + + + @Override + public void createControl(Composite parent) { + composite = new Composite(parent, SWT.NONE); + composite.setLayout(new GridLayout(1,true)); + setControl(composite); + + Label label = new Label(composite, SWT.NONE); + label.setText("Ontology resource files"); + fileSelector = CheckboxTableViewer.newCheckList(composite, SWT.BORDER); + fileSelector.setContentProvider(new FileContentProvider()); + fileSelector.setLabelProvider(new FileLabelProvider()); + + label = new Label(composite, SWT.NONE); + label.setText("Java Output file"); + javaOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE); + + label = new Label(composite, SWT.NONE); + label.setText("SCL Output file"); + sclOutputText = new Text(composite, SWT.BORDER|SWT.SINGLE); + + Button button = new Button(composite, SWT.PUSH); + button.setText("Combine"); + button.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + doCombination(); + } + }); + + GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(fileSelector.getControl()); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(javaOutputText); + GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(sclOutputText); + GridDataFactory.fillDefaults().align(SWT.RIGHT, SWT.CENTER).applyTo(button); + } + + public void setOutputPlugin(File outputPlugin) { + this.outputPlugin = outputPlugin; + } + + private void doCombination() { + Object sel[] = fileSelector.getCheckedElements(); + File inputFiles[] = new File[sel.length]; + for (int i = 0; i < sel.length; i++) { + inputFiles[i] = (File)sel[i]; + } + + try { + String javaOutputName = javaOutputText.getText(); + if (javaOutputName != null && javaOutputName.length() > 2) { + if (!javaOutputName.endsWith(".java")) + javaOutputName+=".java"; + + File outputFile = new File(srcDir.getAbsolutePath()+"/"+javaOutputName); + OntologyCombinator combinator = new OntologyCombinator(); + combinator.combine(inputFiles, outputFile); + + } + // TODO: SCL code depends on Java, allowing generating just SCL makes sense only for testing purposes. + String sclOutputName = sclOutputText.getText(); + if (sclOutputName != null && sclOutputName.length() > 2) { + if (!sclOutputName.endsWith(".scl")) + sclOutputName+=".scl"; + + File outputFile = new File(srcDir.getAbsolutePath()+"/"+sclOutputName); + OntologyCombinator combinator = new SCLCombinator(); + combinator.combine(inputFiles, outputFile); + } + + setPageComplete(true); + } catch (IOException e) { + setErrorMessage(e.getMessage()); + e.printStackTrace(); + } + + } + + private static class FileContentProvider implements IStructuredContentProvider { + @Override + public Object[] getElements(Object inputElement) { + File directory = (File)inputElement; + if (!directory.isDirectory()) + return new Object[0]; + File[] files = directory.listFiles(new FilenameFilter() { + + @Override + public boolean accept(File dir, String name) { + return name.endsWith("java"); + } + }); + return files; + } + + @Override + public void dispose() { + + } + + @Override + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + + } + } + + private static class FileLabelProvider extends LabelProvider implements ITableLabelProvider { + @Override + public Image getColumnImage(Object element, int columnIndex) { + return null; + } + + @Override + public String getColumnText(Object element, int columnIndex) { + File file = (File)element; + if (columnIndex == 0) + return file.getName(); + else + return null; + } + } + +} diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyCombinator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyCombinator.java index 0d08416..733d027 100644 --- a/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyCombinator.java +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/OntologyCombinator.java @@ -1,181 +1,184 @@ -package org.simantics.xml.sax; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -public class OntologyCombinator { - - - public static void combine(File[] inputFiles, File outputFile) throws IOException{ - Set resources = new HashSet<>(); - List ontologyUris = new ArrayList<>(); - List classNames = new ArrayList<>(); - String packageLine = null; - - - String packageLineStart = "package"; - String resourceLineStart = "public final Resource "; - String uriLineStart = "public static final String "; - - - - for (File inputFile : inputFiles) { - BufferedReader reader = new BufferedReader(new FileReader(inputFile)); - try { - String line; - String uri = null; - String filename = inputFile.getName(); - String classname = filename.substring(0,filename.length()-5); - classNames.add(classname); - while ((line = reader.readLine()) != null) { - line = line.trim(); - if (line.startsWith(resourceLineStart)) { - String res = line.substring(resourceLineStart.length(), line.length()-1); - resources.add(res); - } else if (line.startsWith(uriLineStart)) { - String euri = line.substring(line.indexOf('"')+1, line.length()-2); - if (uri == null) { - uri = euri; - } else { - int max = Math.min(euri.length(), uri.length()); - for (int i = 0; i < max; i++) { - if (euri.charAt(i) != uri.charAt(i)) { - uri = uri.substring(0,i); - break; - } - } - } - } else if (line.startsWith(packageLineStart)) { - packageLine = line; - } - } - if (uri != null && uri.length() > 6) { - if (uri.endsWith("/")) { - uri = uri.substring(0, uri.length()-1); - } - ontologyUris.add(uri); - } - else - throw new IOException("Could not resolve ontology uri for file " + inputFile.getName()); - } finally { - reader.close(); - } - } - List sortedResourcses = new ArrayList<>(); - sortedResourcses.addAll(resources); - Collections.sort(sortedResourcses); - - PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile))); - String className = outputFile.getName(); - int dp = className.indexOf("."); - if (dp > 0) - className = className.substring(0,dp); - - try { - writer.println(packageLine); - writer.println(); - writer.println("import java.lang.reflect.Field;"); - writer.println("import java.util.Collection;"); - writer.println("import java.util.HashMap;"); - writer.println("import java.util.Map;"); - writer.println("import org.simantics.Simantics;"); - writer.println("import org.simantics.db.ReadGraph;"); - writer.println("import org.simantics.db.Resource;"); - writer.println("import org.simantics.db.exception.DatabaseException;"); - writer.println("import org.simantics.db.request.Read;"); - writer.println("import org.simantics.layer0.Layer0;"); - writer.println(); - writer.println(); - writer.println("public class " + className + " {"); - writer.println(" private static boolean init = false;"); - writer.println(" private static Map resourceObjects = new HashMap<>();"); - writer.println(" private static Map loaderObjects = new HashMap<>();"); - writer.println(); - writer.println(" private Resource ontologyResource;"); - writer.println(" private Object resourceObject;"); - writer.println(); - writer.println(" static void init(ReadGraph graph) throws DatabaseException{"); - writer.println(" if (init)"); - writer.println(" return;"); - writer.println(" "); - for (int i = 0; i < inputFiles.length; i++) { - writer.println(" add(graph.getResource(\"" +ontologyUris.get(i) +"\"), " + classNames.get(i) +".getInstance(graph));"); - } - writer.println(" "); - writer.println(" init = true;"); - writer.println(" }"); - writer.println(" "); - writer.println(" public static void add(Resource resource, Object object) {"); - writer.println(" if (resource == null || object == null)"); - writer.println(" throw new IllegalArgumentException();"); - writer.println(" resourceObjects.put(resource, object);"); - writer.println(" loaderObjects.put(resource, new "+className+"(resource, object));"); - writer.println(" }"); - writer.println(" "); - writer.println(" public static "+className+" getOntology(final Resource res) throws DatabaseException {"); - writer.println(" return Simantics.getSession().syncRequest(new Read<"+className+">() {"); - writer.println(" @Override"); - writer.println(" public "+className+" perform(ReadGraph graph) throws DatabaseException {"); - writer.println(" return getOntology(graph, res);"); - writer.println(" }"); - writer.println(" });"); - writer.println(" }"); - writer.println(" "); - writer.println(" public static "+className+" getOntology(ReadGraph graph, Resource res) throws DatabaseException{"); - writer.println(" init(graph);"); - writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); - writer.println(" Collection types = graph.getTypes(res);"); - writer.println(" for (Resource type : types) {"); - writer.println(" Resource part = graph.getPossibleObject(type, L0.PartOf);"); - writer.println(" while (part != null) {"); - writer.println(" if (loaderObjects.containsKey(part))"); - writer.println(" return loaderObjects.get(part);"); - writer.println(" part = graph.getPossibleObject(part, L0.PartOf);"); - writer.println(" }"); - writer.println(" }"); - writer.println(" return null;"); - writer.println(" }"); - writer.println(" "); - writer.println(" private "+className+"(Resource ontologyRes, Object resourceObject) {"); - writer.println(" this.ontologyResource = ontologyRes;"); - writer.println(" this.resourceObject = resourceObject;"); - writer.println(" init();"); - writer.println(" }"); - writer.println(" "); - writer.println(" private Resource get(String name) {"); - writer.println(" try {"); - writer.println(" Field field = resourceObject.getClass().getDeclaredField(name);"); - writer.println(" return (Resource)field.get(resourceObject);"); - writer.println(" } catch (Exception e){"); - writer.println(" return null;"); - writer.println(" }"); - writer.println(" }"); - writer.println(" "); - for (String s : sortedResourcses) { - writer.println(" public Resource "+s+";"); - } - writer.println(" "); - writer.println(" private void init() {"); - for (String s : sortedResourcses) { - writer.println(" "+s+" = get(\""+s+"\");"); - } - writer.println(" }"); - writer.println("}"); - - - } finally { - writer.close(); - } - - } -} +package org.simantics.xml.sax; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class OntologyCombinator { + + String packageLineStart = new String("package"); + String resourceLineStart = "public final Resource "; + String uriLineStart = "public static final String "; + + Set resources = new HashSet<>(); + List ontologyUris = new ArrayList<>(); + List classNames = new ArrayList<>(); + String packageLine = null; + List sortedResourcses; + + protected void process(File[] inputFiles) throws IOException { + for (File inputFile : inputFiles) { + BufferedReader reader = new BufferedReader(new FileReader(inputFile)); + try { + String line; + String uri = null; + String filename = inputFile.getName(); + String classname = filename.substring(0,filename.length()-5); + classNames.add(classname); + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.startsWith(resourceLineStart)) { + String res = line.substring(resourceLineStart.length(), line.length()-1); + resources.add(res); + } else if (line.startsWith(uriLineStart)) { + String euri = line.substring(line.indexOf('"')+1, line.length()-2); + if (uri == null) { + uri = euri; + } else { + int max = Math.min(euri.length(), uri.length()); + for (int i = 0; i < max; i++) { + if (euri.charAt(i) != uri.charAt(i)) { + uri = uri.substring(0,i); + break; + } + } + } + } else if (line.startsWith(packageLineStart)) { + packageLine = line; + } + } + if (uri != null && uri.length() > 6) { + if (uri.endsWith("/")) { + uri = uri.substring(0, uri.length()-1); + } + ontologyUris.add(uri); + } + else + throw new IOException("Could not resolve ontology uri for file " + inputFile.getName()); + } finally { + reader.close(); + } + } + + sortedResourcses = new ArrayList<>(); + sortedResourcses.addAll(resources); + Collections.sort(sortedResourcses); + + } + + public void combine(File[] inputFiles, File outputFile) throws IOException{ + process(inputFiles); + + PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile))); + String className = outputFile.getName(); + int dp = className.indexOf("."); + if (dp > 0) + className = className.substring(0,dp); + + try { + writer.println(packageLine); + writer.println(); + writer.println("import java.lang.reflect.Field;"); + writer.println("import java.util.Collection;"); + writer.println("import java.util.HashMap;"); + writer.println("import java.util.Map;"); + writer.println("import org.simantics.Simantics;"); + writer.println("import org.simantics.db.ReadGraph;"); + writer.println("import org.simantics.db.Resource;"); + writer.println("import org.simantics.db.exception.DatabaseException;"); + writer.println("import org.simantics.db.request.Read;"); + writer.println("import org.simantics.layer0.Layer0;"); + writer.println(); + writer.println(); + writer.println("public class " + className + " {"); + writer.println(" private static boolean init = false;"); + writer.println(" private static Map resourceObjects = new HashMap<>();"); + writer.println(" private static Map loaderObjects = new HashMap<>();"); + writer.println(); + writer.println(" private Resource ontologyResource;"); + writer.println(" private Object resourceObject;"); + writer.println(); + writer.println(" static void init(ReadGraph graph) throws DatabaseException{"); + writer.println(" if (init)"); + writer.println(" return;"); + writer.println(" "); + for (int i = 0; i < inputFiles.length; i++) { + writer.println(" add(graph.getResource(\"" +ontologyUris.get(i) +"\"), " + classNames.get(i) +".getInstance(graph));"); + } + writer.println(" "); + writer.println(" init = true;"); + writer.println(" }"); + writer.println(" "); + writer.println(" public static void add(Resource resource, Object object) {"); + writer.println(" if (resource == null || object == null)"); + writer.println(" throw new IllegalArgumentException();"); + writer.println(" resourceObjects.put(resource, object);"); + writer.println(" loaderObjects.put(resource, new "+className+"(resource, object));"); + writer.println(" }"); + writer.println(" "); + writer.println(" public static "+className+" getOntology(final Resource res) throws DatabaseException {"); + writer.println(" return Simantics.getSession().syncRequest(new Read<"+className+">() {"); + writer.println(" @Override"); + writer.println(" public "+className+" perform(ReadGraph graph) throws DatabaseException {"); + writer.println(" return getOntology(graph, res);"); + writer.println(" }"); + writer.println(" });"); + writer.println(" }"); + writer.println(" "); + writer.println(" public static "+className+" getOntology(ReadGraph graph, Resource res) throws DatabaseException{"); + writer.println(" init(graph);"); + writer.println(" Layer0 L0 = Layer0.getInstance(graph);"); + writer.println(" Collection types = graph.getTypes(res);"); + writer.println(" for (Resource type : types) {"); + writer.println(" Resource part = graph.getPossibleObject(type, L0.PartOf);"); + writer.println(" while (part != null) {"); + writer.println(" if (loaderObjects.containsKey(part))"); + writer.println(" return loaderObjects.get(part);"); + writer.println(" part = graph.getPossibleObject(part, L0.PartOf);"); + writer.println(" }"); + writer.println(" }"); + writer.println(" return null;"); + writer.println(" }"); + writer.println(" "); + writer.println(" private "+className+"(Resource ontologyRes, Object resourceObject) {"); + writer.println(" this.ontologyResource = ontologyRes;"); + writer.println(" this.resourceObject = resourceObject;"); + writer.println(" init();"); + writer.println(" }"); + writer.println(" "); + writer.println(" private Resource get(String name) {"); + writer.println(" try {"); + writer.println(" Field field = resourceObject.getClass().getDeclaredField(name);"); + writer.println(" return (Resource)field.get(resourceObject);"); + writer.println(" } catch (Exception e){"); + writer.println(" return null;"); + writer.println(" }"); + writer.println(" }"); + writer.println(" "); + for (String s : sortedResourcses) { + writer.println(" public Resource "+s+";"); + } + writer.println(" "); + writer.println(" private void init() {"); + for (String s : sortedResourcses) { + writer.println(" "+s+" = get(\""+s+"\");"); + } + writer.println(" }"); + writer.println("}"); + + + } finally { + writer.close(); + } + + } +} diff --git a/org.simantics.xml.sax/src/org/simantics/xml/sax/SCLCombinator.java b/org.simantics.xml.sax/src/org/simantics/xml/sax/SCLCombinator.java new file mode 100644 index 0000000..1a7fd0c --- /dev/null +++ b/org.simantics.xml.sax/src/org/simantics/xml/sax/SCLCombinator.java @@ -0,0 +1,49 @@ +package org.simantics.xml.sax; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; + +/** + * Creates a SCL module for Java combination class. + * + * @see org.simantics.xml.sax.OntologyCombinator + * + * @author luukkainen + * + */ +public class SCLCombinator extends OntologyCombinator{ + + @Override + public void combine(File[] inputFiles, File outputFile) throws IOException { + super.process(inputFiles); + + PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile))); + String className = outputFile.getName(); + int dp = className.indexOf("."); + if (dp > 0) + className = className.substring(0,dp); + + try { + writer.println("import \"Simantics/DB\""); + writer.println(); + writer.println("importJava \"" + packageLine.substring(packageLineStart.length()+1,packageLine.length()-1) + "." + className + "\" where"); + writer.println(" data " + className); + writer.println(" "); + writer.println(" @JavaName getOntology"); + writer.println(" getOntology :: Resource -> Maybe " + className); + writer.println(" "); + for (String s : sortedResourcses) { + writer.println(" @JavaName "+s); + writer.println(" "+s+" :: " + className + " -> Maybe Resource"); + writer.println(" "); + } + + } finally { + writer.close(); + } + } + +} -- 2.47.1