1 package org.simantics.modeling;
3 import java.io.ByteArrayInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.UnsupportedEncodingException;
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;
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.compiler.internal.ltk.ISource;
55 import org.simantics.graph.compiler.internal.ltk.Problem;
56 import org.simantics.graph.db.TransferableGraphException;
57 import org.simantics.graph.db.TransferableGraphSource;
58 import org.simantics.graph.db.TransferableGraphs;
59 import org.simantics.graph.diff.Diff;
60 import org.simantics.graph.diff.TransferableGraphDelta1;
61 import org.simantics.graph.representation.Identity;
62 import org.simantics.graph.representation.Root;
63 import org.simantics.graph.representation.TransferableGraph1;
64 import org.simantics.graphfile.ontology.GraphFileResource;
65 import org.simantics.layer0.Layer0;
66 import org.simantics.modeling.internal.Activator;
67 import org.simantics.utils.FileUtils;
68 import org.simantics.utils.datastructures.Pair;
69 import org.slf4j.LoggerFactory;
72 * @author Antti Villberg
74 public class CompilePGraphs {
76 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CompilePGraphs.class);
78 public static interface UserAgent {
79 void reportProblems(CompilationResult result);
83 * <code>Simantics/PGraph#compilePGraphs</code> SCL API.
87 * @throws DatabaseException
89 public static void compilePGraphs(Resource r) throws IOException, DatabaseException {
90 compilePGraphs(r, null);
93 public static void compilePGraphs(Resource r, UserAgent userAgent) throws IOException, DatabaseException {
94 compilePGraphs(r, userAgent, new NullProgressMonitor());
97 public static void compilePGraphs(Resource r, UserAgent userAgent, IProgressMonitor monitor) throws IOException, DatabaseException {
98 final Collection<ISource> sources = new ArrayList<>();
99 Collection<TransferableGraph1> dependencies = new ArrayList<>();
101 final Pair<String, TransferableGraph1> thisOntology = Simantics.sync(new UniqueRead<Pair<String, TransferableGraph1>>() {
104 public Pair<String, TransferableGraph1> perform(ReadGraph graph) throws DatabaseException {
105 Layer0 L0 = Layer0.getInstance(graph);
106 Resource parent = graph.getSingleObject(r, L0.PartOf);
108 CopyHandler ch = new DefaultCopyHandler(r) {
109 protected TransferableGraphConfiguration2 createConfiguration(ReadGraph graph, boolean cut) throws DatabaseException {
110 Map<Resource, ExtentStatus> preStatus = new HashMap<>();
111 preStatus.put(r, ExtentStatus.EXTERNAL);
112 if (!parent.equals(graph.getRootLibrary()))
113 preStatus.put(parent, ExtentStatus.EXTERNAL);
114 return new TransferableGraphConfiguration2(null, Collections.emptyList(), preStatus, true, true);
117 protected TransferableGraphSource computeSource(ReadGraph graph, TransferableGraphConfiguration2 conf) throws DatabaseException {
118 return graph.syncRequest(new ModelTransferableGraphSourceRequest(conf) {
119 protected ModelTransferableGraphSource getSource(ReadGraph graph, TransferableGraphConfiguration2 configuration, DomainProcessorState state, File otherStatementsFile, File valueFile) throws DatabaseException {
120 return new ModelTransferableGraphSource(graph, configuration, state, otherStatementsFile, valueFile) {
122 protected Identity getRootIdentity(DomainProcessorState state, SerialisationSupport support, Resource rootLibrary) throws DatabaseException {
123 return new Identity(state.ids.get(support.getTransientId(rootLibrary)), new Root("", ""));
131 String uri = graph.getURI(r);
132 SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
133 ch.copyToClipboard(graph, clipboard, monitor);
134 for (Set<Representation> object : clipboard.getContents()) {
135 TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
137 return Pair.make(uri, tg);
144 if (thisOntology == null)
145 throw new DatabaseException("Failed to dump the containing ontology of " + r + " into TransferableGraph1");
147 dependencies.add(thisOntology.second);
149 for (Bundle b : Activator.getContext().getBundles()) {
150 String id = b.getSymbolicName();
151 String name = (String) b.getHeaders().get("Bundle-Name");
152 if (name == null) name = id;
153 if (name.equals(thisOntology.first))
155 URL tg = b.getEntry("/graph.tg");
156 if (tg == null) continue;
157 File f = url2file(FileLocator.resolve(tg), b.getSymbolicName());
159 dependencies.add(GraphCompiler.read(f));
160 } catch (Exception e) {
161 throw new IOException("Failed to read compiled transferable graph as dependency: " + f, e);
166 Simantics.sync(new ReadRequest() {
168 public void run(ReadGraph graph) throws DatabaseException {
169 Layer0 L0 = Layer0.getInstance(graph);
170 for (Resource file : graph.syncRequest(new ObjectsWithType(r, L0.ConsistsOf, L0.PGraph))) {
171 String src = graph.getRelatedValue(file, L0.PGraph_definition, Bindings.STRING);
172 sources.add(new StringSource(src));
177 GraphCompilerPreferences prefs = new GraphCompilerPreferences();
178 prefs.validate = true;
179 prefs.validateRelationRestrictions = ValidationMode.ERROR;
180 prefs.validateResourceHasType = ValidationMode.IGNORE;
181 final CompilationResult result = Simantics.sync(new UniqueRead<CompilationResult>() {
183 public CompilationResult perform(ReadGraph graph) throws DatabaseException {
184 final Resource root = graph.syncRequest(new IndexRoot(r));
185 final String baseURI = graph.getURI(root);
187 GraphFileResource GF = GraphFileResource.getInstance(graph);
188 ExternalFileLoader fileLoader = fileName -> {
190 Resource file = graph.getResource(baseURI + "/" + fileName);
191 return graph.getRelatedValue(file, GF.HasFiledata, Bindings.BYTE_ARRAY);
192 } catch (DatabaseException e) {
193 throw new IOException(e);
197 return GraphCompiler.compile("1.1", sources, dependencies, fileLoader, prefs);
201 if (!result.getErrors().isEmpty() || !result.getWarnings().isEmpty()) {
202 if (userAgent != null) {
203 userAgent.reportProblems(result);
206 StringBuilder error = new StringBuilder();
207 for (Problem problem : result.getErrors())
208 error.append(problem.getLocation() + ": " + problem.getDescription() + "\n");
209 for (Problem problem : result.getWarnings())
210 error.append(problem.getLocation() + ": " + problem.getDescription() + "\n");
211 throw new DatabaseException(error.toString());
215 final Pair<TransferableGraph1, long[]> existing = Simantics.sync(new UniqueRead<Pair<TransferableGraph1, long[]>>() {
217 public Pair<TransferableGraph1, long[]> perform(ReadGraph graph) throws DatabaseException {
218 Layer0 L0 = Layer0.getInstance(graph);
219 TransferableGraph1 tg = graph.getPossibleRelatedValue(r, L0.SharedOntology_tg, Bindings.getBindingUnchecked( TransferableGraph1.class ));
220 if(tg == null) return null;
221 long[] tgResources = graph.getPossibleRelatedValue(r, L0.SharedOntology_tgResources, Bindings.LONG_ARRAY);
222 if(tgResources == null) return null;
223 return Pair.make(tg, tgResources);
227 if (existing != null) {
228 Simantics.sync(new WriteRequest() {
230 public void perform(WriteGraph graph) throws DatabaseException {
231 TransferableGraphDelta1 delta = new Diff(existing.first, result.getGraph()).diff();
232 long[] resourceArray = TransferableGraphs.applyDelta(graph, existing.second, delta);
234 Layer0 L0 = Layer0.getInstance(graph);
235 graph.claimLiteral(r, L0.SharedOntology_tg, result.getGraph(), Bindings.getBindingUnchecked( TransferableGraph1.class ));
236 graph.claimLiteral(r, L0.SharedOntology_tgResources, L0.ResourceIdArray, resourceArray, Bindings.LONG_ARRAY);
238 Layer0Utils.addCommentMetadata(graph, "Compiled ontology " + graph.getURI(r));
242 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(r);
244 DefaultPasteHandler.defaultExecute(result.getGraph(), r, advisor);
245 } catch (TransferableGraphException e) {
246 // TODO: defaultExecute never actually throws this exception!
247 throw new DatabaseException(e);
250 Simantics.sync(new WriteRequest() {
252 public void perform(WriteGraph graph) throws DatabaseException {
253 Layer0 L0 = Layer0.getInstance(graph);
254 graph.claimLiteral(r, L0.SharedOntology_tg, result.getGraph(), Bindings.getBindingUnchecked( TransferableGraph1.class ));
255 graph.claimLiteral(r, L0.SharedOntology_tgResources, L0.ResourceIdArray, advisor.getResourceIds(), Bindings.LONG_ARRAY);
257 Layer0Utils.addCommentMetadata(graph, "Compiled ontology " + graph.getURI(r));
262 // Delete index to get rid of floating old instances of the same ontology
263 // DatabaseIndexing.deleteAllIndexes();
266 private static File extractLib(URL libURL, String libName) throws FileNotFoundException, IOException {
267 String tmpDirStr = System.getProperty("java.io.tmpdir");
268 if (tmpDirStr == null)
269 throw new NullPointerException("java.io.tmpdir property is null");
270 File tmpDir = new File(tmpDirStr);
271 File libFile = new File(tmpDir, libName);
272 return FileUtils.copyResource(libURL, libFile, false);
275 private static File url2file(URL url, String fileName) {
276 if ("file".equals(url.getProtocol())) {
278 File path = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
280 } catch (UnsupportedEncodingException e) {
281 Logger.defaultLogError(e);
283 } else if ("jar".equals(url.getProtocol())) {
285 File libFile = extractLib(url, fileName);
287 } catch (FileNotFoundException e) {
288 Logger.defaultLogError(e);
289 } catch (IOException e) {
290 Logger.defaultLogError(e);
293 LOGGER.warn("Unsupported URL protocol");
298 private static class StringSource implements ISource {
300 private ByteArrayInputStream baos;
302 public StringSource(String s) {
304 this.baos = new ByteArrayInputStream(src.getBytes());
308 public int startPos() {
313 public int startLine() {
318 public InputStream open() throws IOException {
323 public int length() throws IOException {
328 public String getName() {