]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/CompilePGraphs.java
7f7171dca54a8b82ece8f27441e7d58c7079eb29
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / CompilePGraphs.java
1 package org.simantics.modeling;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.UnsupportedEncodingException;
9 import java.net.URL;
10 import java.net.URLDecoder;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.eclipse.core.runtime.FileLocator;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21 import org.osgi.framework.Bundle;
22 import org.simantics.Simantics;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.WriteGraph;
27 import org.simantics.db.common.request.IndexRoot;
28 import org.simantics.db.common.request.ObjectsWithType;
29 import org.simantics.db.common.request.ReadRequest;
30 import org.simantics.db.common.request.UniqueRead;
31 import org.simantics.db.common.request.WriteRequest;
32 import org.simantics.db.common.utils.Logger;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.adapter.CopyHandler;
35 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
36 import org.simantics.db.layer0.adapter.impl.DefaultCopyHandler;
37 import org.simantics.db.layer0.adapter.impl.DefaultPasteHandler;
38 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
39 import org.simantics.db.layer0.util.ClipboardUtils;
40 import org.simantics.db.layer0.util.DomainProcessorState;
41 import org.simantics.db.layer0.util.Layer0Utils;
42 import org.simantics.db.layer0.util.ModelTransferableGraphSource;
43 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
44 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
45 import org.simantics.db.layer0.util.SimanticsClipboardImpl;
46 import org.simantics.db.layer0.util.SimanticsKeys;
47 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
48 import org.simantics.db.service.SerialisationSupport;
49 import org.simantics.graph.compiler.CompilationResult;
50 import org.simantics.graph.compiler.ExternalFileLoader;
51 import org.simantics.graph.compiler.GraphCompiler;
52 import org.simantics.graph.compiler.GraphCompilerPreferences;
53 import org.simantics.graph.compiler.ValidationMode;
54 import org.simantics.graph.db.TransferableGraphException;
55 import org.simantics.graph.db.TransferableGraphSource;
56 import org.simantics.graph.db.TransferableGraphs;
57 import org.simantics.graph.diff.Diff;
58 import org.simantics.graph.diff.TransferableGraphDelta1;
59 import org.simantics.graph.representation.Identity;
60 import org.simantics.graph.representation.Root;
61 import org.simantics.graph.representation.TransferableGraph1;
62 import org.simantics.graphfile.ontology.GraphFileResource;
63 import org.simantics.layer0.Layer0;
64 import org.simantics.ltk.ISource;
65 import org.simantics.ltk.Problem;
66 import org.simantics.modeling.internal.Activator;
67 import org.simantics.utils.FileUtils;
68 import org.simantics.utils.datastructures.Pair;
69
70 /**
71  * @author Antti Villberg
72  */
73 public class CompilePGraphs {
74
75     public static interface UserAgent {
76         void reportProblems(CompilationResult result);
77     }
78
79     /**
80      * <code>Simantics/PGraph#compilePGraphs</code> SCL API.
81      * 
82      * @param r
83      * @throws IOException
84      * @throws DatabaseException
85      */
86     public static void compilePGraphs(Resource r) throws IOException, DatabaseException {
87         compilePGraphs(r, null);
88     }
89
90     public static void compilePGraphs(Resource r, UserAgent userAgent) throws IOException, DatabaseException {
91         compilePGraphs(r, userAgent, new NullProgressMonitor());
92     }
93     
94     public static void compilePGraphs(Resource r, UserAgent userAgent, IProgressMonitor monitor) throws IOException, DatabaseException {
95         final Collection<ISource> sources = new ArrayList<>();
96         Collection<TransferableGraph1> dependencies = new ArrayList<>();
97
98         final Pair<String, TransferableGraph1> thisOntology = Simantics.sync(new UniqueRead<Pair<String, TransferableGraph1>>() {
99
100             @Override
101             public Pair<String, TransferableGraph1> perform(ReadGraph graph) throws DatabaseException {
102                 Layer0 L0 = Layer0.getInstance(graph);
103                 Resource parent = graph.getSingleObject(r, L0.PartOf);
104
105                 CopyHandler ch = new DefaultCopyHandler(r) {
106                     protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
107                         Map<Resource, ExtentStatus> preStatus = new HashMap<>();
108                         preStatus.put(r, ExtentStatus.EXTERNAL);
109                         if (!parent.equals(graph.getRootLibrary()))
110                             preStatus.put(parent, ExtentStatus.EXTERNAL);
111                         return new TransferableGraphConfiguration2(null, Collections.emptyList(), preStatus, true, true);
112                     }
113
114                     protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
115                         return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf) {
116                             protected ModelTransferableGraphSource getSource(ReadGraph graph, TransferableGraphConfiguration2 configuration, DomainProcessorState state, File otherStatementsFile, File valueFile) throws DatabaseException {
117                                 return new ModelTransferableGraphSource(graph, configuration, state, otherStatementsFile, valueFile) {
118                                     @Override
119                                     protected Identity getRootIdentity(DomainProcessorState state, SerialisationSupport support, Resource rootLibrary) throws DatabaseException {
120                                         return new Identity(state.ids.get(support.getTransientId(rootLibrary)), new Root("", ""));
121                                     }
122                                 };
123                             }
124                         });
125                     }
126                 };
127
128                 String uri = graph.getURI(r);
129                 SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
130                 ch.copyToClipboard(graph, clipboard);
131                 for (Set<Representation> object : clipboard.getContents()) {
132                     TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
133                     if (tg != null)
134                         return Pair.make(uri, tg);
135                 }
136
137                 return null;
138             }
139         });
140
141         if (thisOntology == null)       
142             throw new DatabaseException("Failed to dump the containing ontology of " + r + " into TransferableGraph1");
143
144         dependencies.add(thisOntology.second);
145         
146         for (Bundle b : Activator.getContext().getBundles()) {
147                         String id = b.getSymbolicName();
148                         String name = (String) b.getHeaders().get("Bundle-Name");
149                         if (name == null) name = id;
150                         if (name.equals(thisOntology.first))
151                                 continue;
152             URL tg = b.getEntry("/graph.tg");
153             if (tg == null) continue;
154             File f = url2file(FileLocator.resolve(tg), b.getSymbolicName());
155             try {
156                 dependencies.add(GraphCompiler.read(f));
157             } catch (Exception e) {
158                 throw new IOException("Failed to read compiled transferable graph as dependency: " + f, e);
159             }
160         }
161
162
163         Simantics.sync(new ReadRequest() {
164             @Override
165             public void run(ReadGraph graph) throws DatabaseException {
166                 Layer0 L0 = Layer0.getInstance(graph);
167                 for (Resource file : graph.syncRequest(new ObjectsWithType(r, L0.ConsistsOf, L0.PGraph))) {
168                     String src = graph.getRelatedValue(file, L0.PGraph_definition, Bindings.STRING);
169                     sources.add(new StringSource(src));
170                 }
171             }
172         });
173
174         GraphCompilerPreferences prefs = new GraphCompilerPreferences();
175         prefs.validate = true;
176         prefs.validateRelationRestrictions = ValidationMode.ERROR;
177         prefs.validateResourceHasType = ValidationMode.IGNORE;
178         final CompilationResult result = Simantics.sync(new UniqueRead<CompilationResult>() {
179             @Override
180             public CompilationResult perform(ReadGraph graph) throws DatabaseException {
181                 final Resource root = graph.syncRequest(new IndexRoot(r));
182                 final String baseURI = graph.getURI(root);
183
184                 GraphFileResource GF = GraphFileResource.getInstance(graph);
185                 ExternalFileLoader fileLoader = fileName -> {
186                     try {
187                         Resource file = graph.getResource(baseURI + "/" + fileName);
188                         return graph.getRelatedValue(file, GF.HasFiledata, Bindings.BYTE_ARRAY);
189                     } catch (DatabaseException e) {
190                         throw new IOException(e);
191                     }
192                 };
193
194                 return GraphCompiler.compile("1.1", sources, dependencies, fileLoader, prefs);
195             }
196         });
197
198         if (!result.getErrors().isEmpty() || !result.getWarnings().isEmpty()) {
199             if (userAgent != null) {
200                 userAgent.reportProblems(result);
201                 return;
202             } else {
203                 StringBuilder error = new StringBuilder();
204                 for (Problem problem : result.getErrors())
205                     error.append(problem.getLocation() + ": " + problem.getDescription() + "\n");
206                 for (Problem problem : result.getWarnings())
207                     error.append(problem.getLocation() + ": " + problem.getDescription() + "\n");
208                 throw new DatabaseException(error.toString());
209             }
210         }
211
212         final Pair<TransferableGraph1, long[]> existing = Simantics.sync(new UniqueRead<Pair<TransferableGraph1, long[]>>() {
213             @Override
214             public Pair<TransferableGraph1, long[]> perform(ReadGraph graph) throws DatabaseException {
215                 Layer0 L0 = Layer0.getInstance(graph);
216                 TransferableGraph1 tg = graph.getPossibleRelatedValue(r, L0.SharedOntology_tg, Bindings.getBindingUnchecked( TransferableGraph1.class ));
217                 if(tg == null) return null;
218                 long[] tgResources = graph.getPossibleRelatedValue(r, L0.SharedOntology_tgResources, Bindings.LONG_ARRAY);
219                 if(tgResources == null) return null;
220                 return Pair.make(tg,  tgResources);
221             }
222         });
223
224         if (existing != null) {
225             Simantics.sync(new WriteRequest() {
226                 @Override
227                 public void perform(WriteGraph graph) throws DatabaseException {
228                     TransferableGraphDelta1 delta = new Diff(existing.first, result.getGraph()).diff();
229                     long[] resourceArray = TransferableGraphs.applyDelta(graph, existing.second, delta);
230
231                     Layer0 L0 = Layer0.getInstance(graph);
232                     graph.claimLiteral(r, L0.SharedOntology_tg, result.getGraph(), Bindings.getBindingUnchecked( TransferableGraph1.class ));
233                     graph.claimLiteral(r, L0.SharedOntology_tgResources, L0.ResourceIdArray, resourceArray, Bindings.LONG_ARRAY);
234
235                     Layer0Utils.addCommentMetadata(graph, "Compiled ontology " + graph.getURI(r));
236                 }
237             });
238         } else {
239             final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(r);
240             try {
241                 DefaultPasteHandler.defaultExecute(result.getGraph(), r, advisor);
242             } catch (TransferableGraphException e) {
243                 // TODO: defaultExecute never actually throws this exception!
244                 throw new DatabaseException(e);
245             }
246
247             Simantics.sync(new WriteRequest() {
248                 @Override
249                 public void perform(WriteGraph graph) throws DatabaseException {
250                     Layer0 L0 = Layer0.getInstance(graph);
251                     graph.claimLiteral(r, L0.SharedOntology_tg, result.getGraph(), Bindings.getBindingUnchecked( TransferableGraph1.class ));
252                     graph.claimLiteral(r, L0.SharedOntology_tgResources, L0.ResourceIdArray, advisor.getResourceIds(), Bindings.LONG_ARRAY);
253
254                     Layer0Utils.addCommentMetadata(graph, "Compiled ontology " + graph.getURI(r));
255                 }
256             });
257         }
258         
259         // Delete index to get rid of floating old instances of the same ontology
260 //        DatabaseIndexing.deleteAllIndexes();
261     }
262
263     private static File extractLib(URL libURL, String libName) throws FileNotFoundException, IOException {
264         String tmpDirStr = System.getProperty("java.io.tmpdir");
265         if (tmpDirStr == null)
266             throw new NullPointerException("java.io.tmpdir property is null");
267         File tmpDir = new File(tmpDirStr);
268         File libFile = new File(tmpDir, libName);
269         return FileUtils.copyResource(libURL, libFile, false);
270     }
271
272     private static File url2file(URL url, String fileName) {
273         if ("file".equals(url.getProtocol())) {
274             try {
275                 File path = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
276                 return path;
277             } catch (UnsupportedEncodingException e) {
278                 Logger.defaultLogError(e);
279             }
280         } else if ("jar".equals(url.getProtocol())) {
281             try {
282                 File libFile = extractLib(url, fileName);
283                 return libFile;
284             } catch (FileNotFoundException e) {
285                 Logger.defaultLogError(e);
286             } catch (IOException e) {
287                 Logger.defaultLogError(e);
288             }
289         } else {
290             System.err.println("Unsupported URL protocol '" + url + "' for FastLZ native library file '" + fileName);
291         }   
292         return null;
293     }
294
295     private static class StringSource implements ISource {
296         private String src;
297         private ByteArrayInputStream baos;
298
299         public StringSource(String s) {
300             this.src = s;
301             this.baos = new ByteArrayInputStream(src.getBytes());
302         }
303
304         @Override
305         public int startPos() {
306             return 0;
307         }
308
309         @Override
310         public int startLine() {
311             return 0;
312         }
313
314         @Override
315         public InputStream open() throws IOException {
316             return baos;
317         }
318
319         @Override
320         public int length() throws IOException {
321             return src.length();
322         }
323
324         @Override
325         public String getName() {
326             return "Source";
327         }
328     }
329
330 }