]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGToGraphMap.java
(refs #7563) Mark parents of new resource optional in graph.tg
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / TGToGraphMap.java
1 package org.simantics.graph.db;
2
3 import java.util.Map;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.WriteOnlyGraph;
10 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
11 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.exception.ResourceNotFoundException;
14 import org.simantics.db.service.SerialisationSupport;
15 import org.simantics.graph.representation.External;
16 import org.simantics.graph.representation.Identity;
17 import org.simantics.graph.representation.IdentityDefinition;
18 import org.simantics.graph.representation.Internal;
19 import org.simantics.graph.representation.Optional;
20 import org.simantics.graph.representation.Root;
21 import org.simantics.graph.representation.TransferableGraph1;
22 import org.simantics.graph.representation.Value;
23
24 public class TGToGraphMap {
25         private TransferableGraph1 tg;
26         private Resource[] resources;
27         
28         private Resource RootLibrary;
29         private Resource String;
30         //private Resource Library;
31         
32         private Resource InstanceOf;
33         private Resource ConsistsOf;
34         private Resource PartOf;
35         private Resource HasName;
36         private Resource NameOf;
37         
38         private void findBuiltins(WriteOnlyGraph g) throws DatabaseException {
39                 RootLibrary = g.getBuiltin("http:/");
40                 String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
41                 //Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
42                 InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
43                 ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
44                 PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
45                 HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
46                 NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
47         }
48         
49         private void findBuiltins(ReadGraph g) throws DatabaseException {
50                 RootLibrary = g.getBuiltin("http:/");
51                 String = g.getBuiltin(CoreInitialization.LAYER0 + "String");
52                 //Library = g.getBuiltin(CoreInitialization.LAYER0 + "Library");
53                 InstanceOf = g.getBuiltin(CoreInitialization.LAYER0 + "InstanceOf");
54                 ConsistsOf = g.getBuiltin(CoreInitialization.LAYER0 + "ConsistsOf");
55                 PartOf = g.getBuiltin(CoreInitialization.LAYER0 + "PartOf");
56                 HasName = g.getBuiltin(CoreInitialization.LAYER0 + "HasName");
57                 NameOf = g.getBuiltin(CoreInitialization.LAYER0 + "NameOf");
58         }
59         
60         public TGToGraphMap(TransferableGraph1 tg) {
61                 this.tg = tg;
62                 this.resources = new Resource[tg.resourceCount];
63         }       
64         
65         public long[] getResources(SerialisationSupport serializer) throws DatabaseException {
66                 final int count = resources.length;
67                 long[] resourceIds = new long[count];
68                 for(int i=0;i<count;++i)
69                         resourceIds[i] = serializer.getRandomAccessId(resources[i]);
70                 return resourceIds;
71         }       
72         
73         public Resource[] getResources() {
74                 return resources;
75         }
76         
77         private void claimChild(WriteOnlyGraph graph, Resource parent, String name, Resource child) throws DatabaseException {
78                 //graph.claim(parent, ConsistsOf, PartOf, child);
79                 Resource nameResource = graph.newResource();
80                 graph.claim(nameResource, InstanceOf, null, String);
81                 graph.claimValue(nameResource, name, Bindings.STRING);
82                 graph.claim(child, HasName, NameOf, nameResource);
83         }
84         
85         public void addMappedOldResources(
86                         SerialisationSupport serializer,
87                         int[] oldToNew,
88                         Resource[] oldResources) throws DatabaseException {
89                 assert(oldToNew.length == oldResources.length);
90                 for(int i=0;i<oldToNew.length;++i) {
91                         int id = oldToNew[i];
92                         if(id >= 0)
93                                 resources[id] = oldResources[i];
94                 }
95         }
96         
97         public void addOldResources(
98                         SerialisationSupport serializer, 
99                         long[] oldResources) throws DatabaseException {
100                 assert(oldResources.length == resources.length);
101                 for(int i=0;i<oldResources.length;++i) {
102                         resources[i] = serializer.getResource(oldResources[i]);
103                 }
104         }
105
106         public void prepare(ReadGraph graph) throws DatabaseException {
107                 if(RootLibrary == null)
108                         findBuiltins(graph);
109                 Resource[] resources = this.resources;          
110                 
111                 for(Identity identity : tg.identities) {
112                         IdentityDefinition definition = identity.definition;
113                         if(definition instanceof External) {
114                                 External def = (External)definition;
115                                 Resource parent = resources[def.parent];
116                                 Resource child = graph.syncRequest(
117                                                 new UnescapedChildMapOfResource(parent), 
118                                                 new TransientCacheAsyncListener<Map<String, Resource>>())
119                                                 .get(def.name);
120                                 if(child == null)
121                                         throw new ResourceNotFoundException(def.name);
122                                 resources[identity.resource] = child;
123                         }
124                         else if(definition instanceof Root) {
125                                 resources[identity.resource] = RootLibrary;             
126                         }
127                         else if(definition instanceof Optional) {
128                                 Optional def = (Optional)definition;
129                                 Resource parent = resources[def.parent];
130                                 if(parent == null)
131                                         continue;
132                                 Resource child = graph.syncRequest(
133                                                 new UnescapedChildMapOfResource(parent),
134                                                 new TransientCacheAsyncListener<Map<String, Resource>>())
135                                                 .get(def.name);
136                                 resources[identity.resource] = child; // might be null
137                         }
138                 }
139         }
140         
141         public void claim(WriteOnlyGraph graph) throws DatabaseException {
142                 if(RootLibrary == null)
143                         findBuiltins(graph);
144                 Resource[] resources = this.resources;
145                         
146                 // Create blank resources
147                 for(int i=0;i<resources.length;++i)
148                         if(resources[i] == null)
149                                 resources[i] = graph.newResource();
150                 
151                 // Internal identities          
152                 for(Identity identity : tg.identities) {
153                         IdentityDefinition definition = identity.definition;
154                         if(definition instanceof Internal) {
155                                 Internal def = (Internal)definition;
156                                 claimChild(graph, resources[def.parent], def.name, resources[identity.resource]);
157                         }                       
158                 }               
159                 
160                 // Write statements
161                 int[] statements = tg.statements;
162                 for(int i=0;i<statements.length;i+=4) {
163                         int inv = statements[i+2];
164                         graph.claim(
165                                         resources[statements[i]],
166                                         resources[statements[i+1]],
167                                         inv < 0 ? null : resources[inv],
168                                         resources[statements[i+3]]
169                                         );
170                 }
171                                 
172                 // Write values
173                 for(Value value : tg.values)
174                     graph.claimValue(resources[value.resource], 
175                             value.value.getValue(), value.value.getBinding());
176         }
177         
178         public boolean checkClaim(ReadGraph graph) throws DatabaseException {
179                 
180                 if(RootLibrary == null)
181                         findBuiltins(graph);
182                 
183                 Resource[] resources = this.resources;
184                         
185                 // Create blank resources
186                 for(int i=0;i<resources.length;++i)
187                         if(resources[i] == null)
188                                 return true;
189                 
190                 // Internal identities          
191                 for(Identity identity : tg.identities) {
192                         IdentityDefinition definition = identity.definition;
193                         if(definition instanceof Internal) {
194                                 return true;
195                         }                       
196                 }               
197                 
198                 if(tg.statements.length > 0) return true;
199                 if(tg.values.length > 0) return true;
200                 
201                 return false;
202                 
203         }
204
205         public void deny(WriteGraph graph) throws DatabaseException {
206                 if(RootLibrary == null)
207                         findBuiltins((ReadGraph)graph);
208                 Resource[] resources = this.resources;
209                 
210                 // Internal identities          
211                 for(Identity identity : tg.identities) {
212                         IdentityDefinition definition = identity.definition;
213                         if(definition instanceof Internal) {
214 //                              Internal def = (Internal)definition;
215                                 /*graph.deny(resources[identity.resource],
216                                                 PartOf,
217                                                 resources[def.parent]
218                                                 );*/
219                                 graph.deny(resources[identity.resource], HasName);
220                         }
221                 }               
222                 
223                 // Deny statements
224                 int[] statements = tg.statements;
225                 for(int i=0;i<statements.length;i+=4) {
226                         int inv = statements[i+2];
227                         graph.deny(
228                                         resources[statements[i]],
229                                         resources[statements[i+1]],
230                                         inv < 0 ? null : resources[inv],
231                                         resources[statements[i+3]]
232                                         );
233                 }
234                                 
235                 // Deny values
236                 for(Value value : tg.values)
237                         graph.denyValue(resources[value.resource]);
238         }
239
240         public boolean checkDeny(ReadGraph graph) throws DatabaseException {
241                 
242                 if(RootLibrary == null)
243                         findBuiltins((ReadGraph)graph);
244                 
245                 // Internal identities          
246                 for(Identity identity : tg.identities) {
247                         IdentityDefinition definition = identity.definition;
248                         if(definition instanceof Internal) {
249                                 return true;
250                         }
251                 }               
252                 
253                 if(tg.statements.length > 0) return true;
254                 if(tg.values.length > 0) return true;
255                 
256                 return false;
257                                 
258         }
259         
260 }