1 package org.simantics.graph.db;
3 import gnu.trove.list.array.TIntArrayList;
4 import gnu.trove.map.hash.THashMap;
5 import gnu.trove.map.hash.TObjectIntHashMap;
6 import gnu.trove.set.hash.THashSet;
8 import java.util.ArrayList;
10 import org.simantics.databoard.Bindings;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.binding.mutable.Variant;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.Statement;
16 import org.simantics.db.VirtualGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.service.VirtualGraphSupport;
19 import org.simantics.graph.representation.External;
20 import org.simantics.graph.representation.Identity;
21 import org.simantics.graph.representation.IdentityDefinition;
22 import org.simantics.graph.representation.Internal;
23 import org.simantics.graph.representation.TransferableGraph1;
24 import org.simantics.graph.representation.Value;
25 import org.simantics.layer0.Layer0;
27 public class VirtualGraphExport {
31 VirtualGraphSupport vgs;
34 THashSet<Resource> nameResources = new THashSet<Resource>();
35 THashMap<Resource, String> names = new THashMap<Resource, String>();
37 TObjectIntHashMap<Resource> resourceIds = new TObjectIntHashMap<Resource>();
38 ArrayList<Identity> identities = new ArrayList<Identity>();
39 ArrayList<Value> values = new ArrayList<Value>();
40 TIntArrayList statements = new TIntArrayList();
42 private VirtualGraphExport(ReadGraph g, VirtualGraph vg) {
45 this.vgs = g.getService(VirtualGraphSupport.class);
46 this.L0 = Layer0.getInstance(g);
50 * Finds resource names and name resources.
52 private void processNames() throws DatabaseException {
53 for(Statement stat : vgs.listStatements(vg)) {
54 Resource p = stat.getPredicate();
55 if(p.equals(L0.HasName)) {
56 Resource s = stat.getSubject();
57 Resource o = stat.getObject();
58 names.put(s, (String)g.getValue(o));
65 * If the resource is encountered first time, adds it
66 * to resourceIds map and creates an identity for it.
68 private void prepareResource(Resource resource) throws DatabaseException {
69 if(!resourceIds.containsKey(resource)) {
70 int newId = resourceIds.size();
71 resourceIds.put(resource, newId);
73 String name = names.get(resource);
75 Resource parent = g.getPossibleObject(resource, L0.PartOf);
77 prepareResource(parent);
78 int parentId = resourceIds.get(parent);
79 IdentityDefinition def =
80 resource.isPersistent()
81 ? new External(parentId, name)
82 : new Internal(parentId, name);
83 identities.add(new Identity(newId, def));
90 * Process all statements of the virtual graph and
91 * adds them to integer table that will be part of the
94 private void processStatements() throws DatabaseException {
95 for(Statement stat : vgs.listStatements(vg)) {
97 * Skips the statement if its subject or object is
98 * a name literal or if its predicate is ConsistsOf,
99 * HasName or inverse of these relations.
101 Resource s = stat.getSubject();
102 if(nameResources.contains(s))
104 Resource p = stat.getPredicate();
105 if(p.equals(L0.PartOf) || p.equals(L0.ConsistsOf)
106 || p.equals(L0.HasName) || p.equals(L0.NameOf))
108 Resource o = stat.getObject();
109 if(nameResources.contains(o))
113 * Adds resources to resourceIds map and generates identities.
122 statements.add(resourceIds.get(s));
123 statements.add(resourceIds.get(p));
125 statements.add(resourceIds.get(o));
130 * Process all values of the virtual graph.
132 private void processValues() throws DatabaseException {
133 for(Resource resourceWithValue : vgs.listValues(vg)) {
134 if(nameResources.contains(resourceWithValue))
137 Bindings.getBeanBinding(g.getDataType(resourceWithValue));
138 Object value = g.getValue(resourceWithValue, binding);
139 values.add(new Value(resourceIds.get(resourceWithValue),
140 new Variant(binding, value)
146 * Creates a virtual graph.
148 private TransferableGraph1 getTransferableGraph() {
149 return new TransferableGraph1(resourceIds.size(),
150 identities.toArray(new Identity[identities.size()]),
151 statements.toArray(),
152 values.toArray(new Value[values.size()]));
156 * Converts the contents of a virtual graph to a transferable graph.
158 public static TransferableGraph1 export(ReadGraph g, VirtualGraph vg) throws DatabaseException {
159 VirtualGraphExport export = new VirtualGraphExport(g, vg);
160 export.processNames();
161 export.processStatements();
162 export.processValues();
163 return export.getTransferableGraph();