]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.db/src/org/simantics/graph/db/VirtualGraphExport.java
Change L0.Value => L0.Property to fix variable exceptions
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / VirtualGraphExport.java
1 package org.simantics.graph.db;
2
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;
7
8 import java.util.ArrayList;
9
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;
26
27 public class VirtualGraphExport {
28
29     ReadGraph g;
30     VirtualGraph vg;
31     VirtualGraphSupport vgs;
32     Layer0 L0;
33     
34     THashSet<Resource> nameResources = new THashSet<Resource>(); 
35     THashMap<Resource, String> names = new THashMap<Resource, String>();
36     
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();
41     
42     private VirtualGraphExport(ReadGraph g, VirtualGraph vg) {
43         this.g = g;
44         this.vg = vg;
45         this.vgs = g.getService(VirtualGraphSupport.class);
46         this.L0 = Layer0.getInstance(g);
47     }
48     
49     /**
50      * Finds resource names and name resources.
51      */
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));
59                 nameResources.add(o);
60             }
61         } 
62     }
63     
64     /**
65      * If the resource is encountered first time, adds it
66      * to resourceIds map and creates an identity for it.
67      */
68     private void prepareResource(Resource resource) throws DatabaseException {
69         if(!resourceIds.containsKey(resource)) {    
70             int newId = resourceIds.size();
71             resourceIds.put(resource, newId);            
72             
73             String name = names.get(resource);
74             if(name != null) {
75                 Resource parent = g.getPossibleObject(resource, L0.PartOf);                
76                 if(parent != null) {
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));
84                 }
85             }
86         }
87     }
88     
89     /**
90      * Process all statements of the virtual graph and
91      * adds them to integer table that will be part of the
92      * transferable graph,
93      */
94     private void processStatements() throws DatabaseException {
95         for(Statement stat : vgs.listStatements(vg)) {
96             /*
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. 
100              */
101             Resource s = stat.getSubject();
102             if(nameResources.contains(s))
103                 continue;
104             Resource p = stat.getPredicate();
105             if(p.equals(L0.PartOf) || p.equals(L0.ConsistsOf) 
106                     || p.equals(L0.HasName) || p.equals(L0.NameOf))
107                 continue;
108             Resource o = stat.getObject();
109             if(nameResources.contains(o))
110                 continue;
111
112             /*
113              * Adds resources to resourceIds map and generates identities.
114              */
115             prepareResource(s);
116             prepareResource(p);
117             prepareResource(o);
118             
119             /*
120              * Adds a statement
121              */
122             statements.add(resourceIds.get(s));            
123             statements.add(resourceIds.get(p));
124             statements.add(-1);
125             statements.add(resourceIds.get(o));       
126         }
127     }
128     
129     /**
130      * Process all values of the virtual graph.
131      */
132     private void processValues() throws DatabaseException {        
133         for(Resource resourceWithValue : vgs.listValues(vg)) {
134             if(nameResources.contains(resourceWithValue))
135                 continue;
136             Binding binding =
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)
141                     ));
142         }
143     }
144     
145     /**
146      * Creates a virtual graph.
147      */
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()]));
153     }
154     
155     /**
156      * Converts the contents of a virtual graph to a transferable graph.
157      */
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();
164         
165     }
166     
167 }