1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.layer0.utils.direct;
14 import java.util.Collection;
15 import java.util.HashSet;
17 import java.util.Stack;
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;
33 public final class GraphUtils {
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);
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);
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);
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);
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);
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);
83 public static void addRelatedScalarString(WriteGraph graph, Resource resource, Resource relation, String string) throws DatabaseException {
84 graph.claim(resource, relation, createScalarString(graph, string));
87 public static void addRelatedScalarInteger(WriteGraph graph, Resource resource, Resource relation, int value) throws DatabaseException {
88 graph.claim(resource, relation, createScalarInteger(graph, value));
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);
99 public static Resource createDoubleArray(WriteGraph graph, double[] array) throws DatabaseException {
100 Layer0 b = Layer0.getInstance(graph);
101 return createDoubleArray(graph, array, b.DoubleArray);
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);
112 public static Resource createIntegerArray(WriteGraph graph, int[] array) throws DatabaseException {
113 Layer0 b = Layer0.getInstance(graph);
114 return createIntegerArray(graph, array, b.IntegerArray);
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);
125 public static Resource createLongArray(WriteGraph graph, long[] array) throws DatabaseException {
126 Layer0 b = Layer0.getInstance(graph);
127 return createLongArray(graph, array, b.LongArray);
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);
138 public static Resource createFloatArray(WriteGraph graph, float[] array) throws DatabaseException {
139 Layer0 b = Layer0.getInstance(graph);
140 return createFloatArray(graph, array, b.FloatArray);
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);
151 public static Resource createBooleanArray(WriteGraph graph, boolean[] array) throws DatabaseException {
152 Layer0 b = Layer0.getInstance(graph);
153 return createBooleanArray(graph, array, b.BooleanArray);
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);
164 public static Resource createStringArray(WriteGraph graph, String[] array) throws DatabaseException {
165 Layer0 b = Layer0.getInstance(graph);
166 return createStringArray(graph, array, b.StringArray);
169 public interface ResourceTester {
170 boolean test(ReadGraph graph, Resource resource) throws DatabaseException ;
173 public static class AcceptAll implements ResourceTester {
176 public boolean test(ReadGraph graph, Resource resource) throws DatabaseException {
182 public static class InstanceOf implements ResourceTester {
184 private final Resource type;
186 public InstanceOf(Resource type) {
191 public boolean test(ReadGraph graph, Resource resource) throws DatabaseException {
192 return graph.isInstanceOf(resource, type);
197 public static class Inherits implements ResourceTester {
199 private final Resource superType;
201 public Inherits(Resource superType) {
202 this.superType = superType;
206 public boolean test(ReadGraph graph, Resource resource) throws DatabaseException {
207 return graph.isInheritedFrom(resource, superType);
212 public static void findResources(ReadGraph graph, Collection<Resource> roots, Resource relation, ResourceTester tester, MultiProcedure<Resource> procedure) throws DatabaseException {
214 Set<Resource> visited = new HashSet<Resource>();
215 Stack<Resource> process = new Stack<Resource>();
217 process.addAll(roots);
218 visited.addAll(roots);
220 while(!process.isEmpty()) {
221 Resource cur = process.pop();
222 for(Resource r : graph.getObjects(cur, relation))
223 if(!visited.contains(r)) {
225 if(tester.test(graph, r))
226 procedure.execute(r);
231 procedure.finished();
236 public static void findResources(ReadGraph graph, Collection<Resource> roots, Resource relation, ResourceTester tester, SyncMultiProcedure<Resource> procedure) throws DatabaseException {
238 Set<Resource> visited = new HashSet<Resource>();
239 Stack<Resource> process = new Stack<Resource>();
241 process.addAll(roots);
242 visited.addAll(roots);
244 while(!process.isEmpty()) {
245 Resource cur = process.pop();
246 for(Resource r : graph.getObjects(cur, relation))
247 if(!visited.contains(r)) {
249 if(tester.test(graph, r))
250 procedure.execute(graph, r);
255 procedure.finished(graph);
260 * @deprecated use {@link NameUtils#getSafeName(ReadGraph, Resource)}
262 public static String getReadableName(ReadGraph graph, Resource resource) throws ValidationException, ServiceException {
263 return NameUtils.getSafeName(graph, resource);
267 * @deprecated use {@link NameUtils#findReservedNames(ReadGraph, String, Resource, Resource, Set)}
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);
274 * @deprecated use {@link NameUtils#findReservedNames(ReadGraph, String, Resource, Resource)
276 public static Set<String> findReservedNames(ReadGraph g, String proposition, Resource container, Resource consistRelation) throws DatabaseException {
277 return NameUtils.findReservedNames(g, proposition, container, consistRelation);
281 * @deprecated use {@link NameUtils#findFreshName(ReadGraph, String, Resource)}
283 public static String findFreshName(ReadGraph g, String proposition, Resource container) throws DatabaseException {
284 return NameUtils.findFreshName(g, proposition, container);
288 * @deprecated use {@link NameUtils#findFreshEscapedName(ReadGraph, String, Resource)}
290 public static String findFreshEscapedName(ReadGraph g, String proposition, Resource container) throws DatabaseException {
291 return NameUtils.findFreshEscapedName(g, proposition, container);
295 * @deprecated use {@link NameUtils#findFreshName(ReadGraph, String, Resource, Resource)}
297 public static String findFreshName(ReadGraph g, String proposition, Resource container, Resource consistRelation) throws DatabaseException {
298 return NameUtils.findFreshName(g, proposition, container, consistRelation);
302 * @deprecated use {@link NameUtils#findFreshEscapedName(ReadGraph, String, Resource, Resource)}
304 public static String findFreshEscapedName(ReadGraph g, String proposition, Resource container, Resource consistRelation) throws DatabaseException {
305 return NameUtils.findFreshEscapedName(g, proposition, container, consistRelation);
309 * @deprecated use {@link NameUtils#findFreshName(ReadGraph, String, Resource, Resource, String)}
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);
316 * @deprecated use {@link NameUtils#findFreshInstanceName(ReadGraph, Resource, Resource)}
318 public static String findFreshInstanceName(ReadGraph g, Resource type, Resource container) throws DatabaseException {
319 return NameUtils.findFreshInstanceName(g, type, container);
323 * @deprecated use {@link NameUtils#findFreshInstanceName(ReadGraph, Resource, Resource, Resource)}
325 public static String findFreshInstanceName(ReadGraph g, Resource type, Resource container, Resource relation) throws DatabaseException {
326 return NameUtils.findFreshInstanceName(g, type, container, relation);
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]);
337 private static Resource getObjectOrCreatePrimitiveValue(WriteGraph g, Object object) throws DatabaseException {
339 throw new NullPointerException("null object");
341 Class<?> clazz = object.getClass();
343 if (object instanceof Resource)
344 return (Resource) object;
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);
371 throw new UnsupportedOperationException("unsupported object type: " + object);
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);
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);
402 * @deprecated use {@link NameUtils#toString(ReadGraph, Statement)}
404 public static String toString(ReadGraph g, Statement stm) throws DatabaseException {
405 return NameUtils.toString(g, stm);
408 // public static Resource getPossiblePath(ReadGraph graph, Resource resource, Resource ... path) throws DatabaseException {
410 // for(Resource p : path) {
411 // resource = graph.getPossibleObject(resource, p);
412 // if(resource == null) return null;
418 public static Resource getPossibleChild(ReadGraph g, Resource parent, String name) throws DatabaseException {
419 return g.syncRequest(new UnescapedChildMapOfResource(parent)).get(name);