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