1 package org.simantics.db.layer0.util;
3 import java.io.IOException;
4 import java.util.Arrays;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.RequestProcessor;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.request.UniqueRead;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.util.SimanticsClipboard.Representation;
13 import org.simantics.graph.db.TransferableGraphs;
14 import org.simantics.graph.representation.TransferableGraph1;
15 import org.simantics.utils.datastructures.hints.IHintContext.Key;
17 public class TGRepresentation implements Representation {
19 protected boolean ignoreVirtualResources = false;
20 protected Resource[] resources;
21 private TransferableGraph1 value = null;
22 private TransferableGraphConfiguration2 configuration;
24 public TGRepresentation(Resource ... resources) {
25 this.resources = resources;
28 public TGRepresentation(boolean ignoreVirtualResources, Resource ... resources) {
29 this.ignoreVirtualResources = ignoreVirtualResources;
30 this.resources = resources;
33 public TGRepresentation(TransferableGraphConfiguration2 configuration) {
34 this.configuration = configuration;
37 public TGRepresentation(TransferableGraph1 value) {
43 return SimanticsKeys.KEY_TRANSFERABLE_GRAPH;
46 public TransferableGraph1 compute(ReadGraph graph, Map<String,Object> hints) throws DatabaseException {
48 if(configuration == null) {
49 configuration = new TransferableGraphConfiguration2(graph, Arrays.asList(resources), ignoreVirtualResources, false);
50 configuration.exclusionFunction = TGRepresentationUtils.computeExclusionFunction(graph, resources, hints);
53 try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(configuration))) {
54 return TransferableGraphs.create(graph, source);
55 } catch (IOException e) {
56 throw new DatabaseException(e);
60 @SuppressWarnings("unchecked")
62 public <T> T getValue(RequestProcessor processor, Map<String,Object> hints) throws DatabaseException {
66 value = processor.syncRequest(new UniqueRead<TransferableGraph1>() {
69 public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {
70 return compute(graph, hints);