]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/direct/GraphUtils.java
Templates for creating SCL values
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / direct / GraphUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.layer0.utils.direct;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
17 import java.util.Stack;
18
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Statement;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
24 import org.simantics.db.common.utils.NameUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.exception.ServiceException;
27 import org.simantics.db.exception.ValidationException;
28 import org.simantics.db.procedure.MultiProcedure;
29 import org.simantics.db.procedure.SyncMultiProcedure;
30 import org.simantics.layer0.Layer0;
31
32
33 public final class GraphUtils {
34
35     public static Resource createScalarString(WriteGraph graph, String string) throws DatabaseException {
36         Layer0 b = Layer0.getInstance(graph);
37         Resource ret = graph.newResource();
38         graph.claim(ret, b.InstanceOf, null, b.String);
39         graph.claimValue(ret, string);
40         return ret;
41     }
42
43     public static Resource createScalarInteger(WriteGraph graph, int value) throws DatabaseException {
44         Layer0 b = Layer0.getInstance(graph);
45         Resource ret = graph.newResource();
46         graph.claim(ret, b.InstanceOf, null, b.Integer);
47         graph.claimValue(ret, value);
48         return ret;
49     }
50
51     public static Resource createScalarLong(WriteGraph graph, long value) throws DatabaseException {
52         Layer0 b = Layer0.getInstance(graph);
53         Resource ret = graph.newResource();
54         graph.claim(ret, b.InstanceOf, null, b.Long);
55         graph.claimValue(ret, value);
56         return ret;
57     }
58
59     public static Resource createScalarFloat(WriteGraph graph, float value) throws DatabaseException {
60         Layer0 b = Layer0.getInstance(graph);
61         Resource ret = graph.newResource();
62         graph.claim(ret, b.InstanceOf, null, b.Float);
63         graph.claimValue(ret, value);
64         return ret;
65     }
66
67     public static Resource createScalarBoolean(WriteGraph graph, boolean value) throws DatabaseException {
68         Layer0 b = Layer0.getInstance(graph);
69         Resource ret = graph.newResource();
70         graph.claim(ret, b.InstanceOf, null, b.Boolean);
71         graph.claimValue(ret, value);
72         return ret;
73     }
74
75     public static Resource createScalarDouble(WriteGraph graph, double value) throws DatabaseException {
76         Layer0 b = Layer0.getInstance(graph);
77         Resource ret = graph.newResource();
78         graph.claim(ret, b.InstanceOf, null, b.Double);
79         graph.claimValue(ret, value);
80         return ret;
81     }
82
83     public static void addRelatedScalarString(WriteGraph graph, Resource resource, Resource relation, String string) throws DatabaseException {
84         graph.claim(resource, relation, createScalarString(graph, string));
85     }
86
87     public static void addRelatedScalarInteger(WriteGraph graph, Resource resource, Resource relation, int value) throws DatabaseException {
88         graph.claim(resource, relation, createScalarInteger(graph, value));
89     }
90
91     public static Resource createDoubleArray(WriteGraph graph, double[] array, Resource type) throws DatabaseException {
92         Resource ret = graph.newResource();
93         Layer0 b = Layer0.getInstance(graph);
94         graph.claim(ret, b.InstanceOf, null, type);
95         graph.claimValue(ret, array);
96         return ret;
97     }
98
99     public static Resource createDoubleArray(WriteGraph graph, double[] array) throws DatabaseException {
100         Layer0 b = Layer0.getInstance(graph);
101         return createDoubleArray(graph, array, b.DoubleArray);
102     }
103
104     public static Resource createIntegerArray(WriteGraph graph, int[] array, Resource type) throws DatabaseException {
105         Resource ret = graph.newResource();
106         Layer0 b = Layer0.getInstance(graph);
107         graph.claim(ret, b.InstanceOf, null, type);
108         graph.claimValue(ret, array);
109         return ret;
110     }
111
112     public static Resource createIntegerArray(WriteGraph graph, int[] array) throws DatabaseException {
113         Layer0 b = Layer0.getInstance(graph);
114         return createIntegerArray(graph, array, b.IntegerArray);
115     }
116
117     public static Resource createLongArray(WriteGraph graph, long[] array, Resource type) throws DatabaseException {
118         Resource ret = graph.newResource();
119         Layer0 b = Layer0.getInstance(graph);
120         graph.claim(ret, b.InstanceOf, null, type);
121         graph.claimValue(ret, array);
122         return ret;
123     }
124
125     public static Resource createLongArray(WriteGraph graph, long[] array) throws DatabaseException {
126         Layer0 b = Layer0.getInstance(graph);
127         return createLongArray(graph, array, b.LongArray);
128     }
129
130     public static Resource createFloatArray(WriteGraph graph, float[] array, Resource type) throws DatabaseException {
131         Resource ret = graph.newResource();
132         Layer0 b = Layer0.getInstance(graph);
133         graph.claim(ret, b.InstanceOf, null, type);
134         graph.claimValue(ret, array);
135         return ret;
136     }
137
138     public static Resource createFloatArray(WriteGraph graph, float[] array) throws DatabaseException {
139         Layer0 b = Layer0.getInstance(graph);
140         return createFloatArray(graph, array, b.FloatArray);
141     }
142
143     public static Resource createBooleanArray(WriteGraph graph, boolean[] array, Resource type) throws DatabaseException {
144         Resource ret = graph.newResource();
145         Layer0 b = Layer0.getInstance(graph);
146         graph.claim(ret, b.InstanceOf, null, type);
147         graph.claimValue(ret, array);
148         return ret;
149     }
150
151     public static Resource createBooleanArray(WriteGraph graph, boolean[] array) throws DatabaseException {
152         Layer0 b = Layer0.getInstance(graph);
153         return createBooleanArray(graph, array, b.BooleanArray);
154     }
155
156     public static Resource createStringArray(WriteGraph graph, String[] array, Resource type) throws DatabaseException {
157         Resource ret = graph.newResource();
158         Layer0 b = Layer0.getInstance(graph);
159         graph.claim(ret, b.InstanceOf, null, type);
160         graph.claimValue(ret, array);
161         return ret;
162     }
163
164     public static Resource createStringArray(WriteGraph graph, String[] array) throws DatabaseException {
165         Layer0 b = Layer0.getInstance(graph);
166         return createStringArray(graph, array, b.StringArray);
167     }
168
169     public interface ResourceTester {
170         boolean test(ReadGraph graph, Resource resource) throws DatabaseException ;
171     }
172
173     public static class AcceptAll implements ResourceTester {
174
175         @Override
176         public boolean test(ReadGraph graph, Resource resource) throws DatabaseException {
177             return true;
178         }
179
180     }
181
182     public static class InstanceOf implements ResourceTester {
183
184         private final Resource type;
185
186         public InstanceOf(Resource type) {
187             this.type = type;
188         }
189
190         @Override
191         public boolean test(ReadGraph graph, Resource resource) throws DatabaseException {
192             return graph.isInstanceOf(resource, type);
193         }
194
195     }
196
197     public static class Inherits implements ResourceTester {
198
199         private final Resource superType;
200
201         public Inherits(Resource superType) {
202             this.superType = superType;
203         }
204
205         @Override
206         public boolean test(ReadGraph graph, Resource resource) throws DatabaseException {
207             return graph.isInheritedFrom(resource, superType);
208         }
209
210     }
211
212     public static void findResources(ReadGraph graph, Collection<Resource> roots, Resource relation, ResourceTester tester, MultiProcedure<Resource> procedure) throws DatabaseException {
213
214         Set<Resource> visited = new HashSet<Resource>();
215         Stack<Resource> process = new Stack<Resource>();
216
217         process.addAll(roots);
218         visited.addAll(roots);
219
220         while(!process.isEmpty()) {
221             Resource cur = process.pop();
222             for(Resource r : graph.getObjects(cur, relation))
223                 if(!visited.contains(r)) {
224                     visited.add(r);
225                     if(tester.test(graph, r))
226                         procedure.execute(r);
227                     process.add(r);
228                 }
229         }
230
231         procedure.finished();
232
233     }
234
235
236     public static void findResources(ReadGraph graph, Collection<Resource> roots, Resource relation, ResourceTester tester, SyncMultiProcedure<Resource> procedure) throws DatabaseException {
237
238         Set<Resource> visited = new HashSet<Resource>();
239         Stack<Resource> process = new Stack<Resource>();
240
241         process.addAll(roots);
242         visited.addAll(roots);
243
244         while(!process.isEmpty()) {
245             Resource cur = process.pop();
246             for(Resource r : graph.getObjects(cur, relation))
247                 if(!visited.contains(r)) {
248                     visited.add(r);
249                     if(tester.test(graph, r))
250                         procedure.execute(graph, r);
251                     process.add(r);
252                 }
253         }
254
255         procedure.finished(graph);
256
257     }
258
259     /**
260      * @deprecated use {@link NameUtils#getSafeName(ReadGraph, Resource)}
261      */
262     public static String getReadableName(ReadGraph graph, Resource resource) throws ValidationException, ServiceException {
263         return NameUtils.getSafeName(graph, resource);
264     }
265
266     /**
267      * @deprecated use {@link NameUtils#findReservedNames(ReadGraph, String, Resource, Resource, Set)}
268      */
269     public static Set<String> findReservedNames(ReadGraph g, String proposition, Resource container, Resource consistRelation, Set<String> result) throws DatabaseException {
270         return NameUtils.findReservedNames(g, proposition, container, consistRelation, result);
271     }
272
273     /**
274      * @deprecated use {@link NameUtils#findReservedNames(ReadGraph, String, Resource, Resource)
275      */
276     public static Set<String> findReservedNames(ReadGraph g, String proposition, Resource container, Resource consistRelation) throws DatabaseException {
277         return NameUtils.findReservedNames(g, proposition, container, consistRelation);
278     }
279
280     /**
281      * @deprecated use {@link NameUtils#findFreshName(ReadGraph, String, Resource)}
282      */
283     public static String findFreshName(ReadGraph g, String proposition, Resource container) throws DatabaseException {
284         return NameUtils.findFreshName(g, proposition, container);
285     }
286
287     /**
288      * @deprecated use {@link NameUtils#findFreshEscapedName(ReadGraph, String, Resource)}
289      */
290     public static String findFreshEscapedName(ReadGraph g, String proposition, Resource container) throws DatabaseException {
291         return NameUtils.findFreshEscapedName(g, proposition, container);
292     }
293
294     /**
295      * @deprecated use {@link NameUtils#findFreshName(ReadGraph, String, Resource, Resource)}
296      */
297     public static String findFreshName(ReadGraph g, String proposition, Resource container, Resource consistRelation) throws DatabaseException {
298         return NameUtils.findFreshName(g, proposition, container, consistRelation);
299     }
300
301     /**
302      * @deprecated use {@link NameUtils#findFreshEscapedName(ReadGraph, String, Resource, Resource)}
303      */
304     public static String findFreshEscapedName(ReadGraph g, String proposition, Resource container, Resource consistRelation) throws DatabaseException {
305         return NameUtils.findFreshEscapedName(g, proposition, container, consistRelation);
306     }
307
308     /**
309      * @deprecated use {@link NameUtils#findFreshName(ReadGraph, String, Resource, Resource, String)}
310      */
311     public static String findFreshName(ReadGraph g, String proposition, Resource container, Resource consistRelation, String nameFormat) throws DatabaseException {
312         return NameUtils.findFreshName(g, proposition, container, consistRelation, nameFormat);
313     }
314
315     /**
316      * @deprecated use {@link NameUtils#findFreshInstanceName(ReadGraph, Resource, Resource)}
317      */
318     public static String findFreshInstanceName(ReadGraph g, Resource type, Resource container) throws DatabaseException {
319         return NameUtils.findFreshInstanceName(g, type, container);
320     }
321
322     /**
323      * @deprecated use {@link NameUtils#findFreshInstanceName(ReadGraph, Resource, Resource, Resource)}
324      */
325     public static String findFreshInstanceName(ReadGraph g, Resource type, Resource container, Resource relation) throws DatabaseException {
326         return NameUtils.findFreshInstanceName(g, type, container, relation);
327     }
328
329     public static Resource create(WriteGraph g, Resource ... predicateObjectPairs) throws DatabaseException {
330         assert(predicateObjectPairs.length % 2 == 0);
331         Resource resource = g.newResource();
332         for(int i=0;i<predicateObjectPairs.length;i+=2)
333             g.claim(resource, predicateObjectPairs[i], predicateObjectPairs[i+1]);
334         return resource;
335     }
336
337     private static Resource getObjectOrCreatePrimitiveValue(WriteGraph g, Object object) throws DatabaseException {
338         if (object == null)
339             throw new NullPointerException("null object");
340
341         Class<?> clazz = object.getClass();
342
343         if (object instanceof Resource)
344             return (Resource) object;
345
346         if (String.class.equals(clazz))
347             return createScalarString(g, (String) object);
348         else if (String[].class.equals(clazz))
349             return createStringArray(g, (String[]) object);
350         else if (Integer.class.equals(clazz))
351             return createScalarInteger(g, (Integer) object);
352         else if (int[].class.equals(clazz))
353             return createIntegerArray(g, (int[]) object);
354         else if (Long.class.equals(clazz))
355             return createScalarLong(g, (Long) object);
356         else if (long[].class.equals(clazz))
357             return createLongArray(g, (long[]) object);
358         else if (Float.class.equals(clazz))
359             return createScalarFloat(g, (Float) object);
360         else if (float[].class.equals(clazz))
361             return createFloatArray(g, (float[]) object);
362         else if (Double.class.equals(clazz))
363             return createScalarDouble(g, (Double) object);
364         else if (double[].class.equals(clazz))
365             return createDoubleArray(g, (double[]) object);
366         else if (Boolean.class.equals(clazz))
367             return createScalarBoolean(g, (Boolean) object);
368         else if (boolean[].class.equals(clazz))
369             return createBooleanArray(g, (boolean[]) object);
370
371         throw new UnsupportedOperationException("unsupported object type: " + object);
372     }
373
374     public static Resource create(WriteGraph g, Object ... predicateObjectPairs) throws DatabaseException {
375         assert(predicateObjectPairs.length % 2 == 0);
376         Resource resource = g.newResource();
377         for(int i=0;i<predicateObjectPairs.length;i+=2) {
378             Resource predicate = (Resource)predicateObjectPairs[i];
379             Object _object = predicateObjectPairs[i+1];
380             Resource object = getObjectOrCreatePrimitiveValue(g, _object);
381             g.claim(resource, predicate, object);
382         }
383         return resource;
384     }
385
386     public static Resource create2(WriteGraph g, Resource type, Object ... predicateObjectPairs) throws DatabaseException {
387         assert(predicateObjectPairs.length % 2 == 0);
388         assert(type != null);
389         Resource resource = g.newResource();
390         Layer0 b = Layer0.getInstance(g);
391         g.claim(resource, b.InstanceOf, null, type);
392         for(int i=0;i<predicateObjectPairs.length;i+=2) {
393             Resource predicate = (Resource)predicateObjectPairs[i];
394             Object _object = predicateObjectPairs[i+1];
395             Resource object = getObjectOrCreatePrimitiveValue(g, _object);
396             g.claim(resource, predicate, object);
397         }
398         return resource;
399     }
400
401     /**
402      * @deprecated use {@link NameUtils#toString(ReadGraph, Statement)}
403      */
404     public static String toString(ReadGraph g, Statement stm) throws DatabaseException {
405         return NameUtils.toString(g, stm);
406     }
407
408 //    public static Resource getPossiblePath(ReadGraph graph, Resource resource, Resource ... path) throws DatabaseException {
409 //
410 //        for(Resource p : path) {
411 //            resource = graph.getPossibleObject(resource, p);
412 //            if(resource == null) return null;
413 //        }
414 //        return resource;
415 //
416 //    }
417     
418     public static Resource getPossibleChild(ReadGraph g, Resource parent, String name) throws DatabaseException {
419         return g.syncRequest(new UnescapedChildMapOfResource(parent)).get(name);
420     }
421 }