1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.layer0.utils.representation.representations;
\r
14 import java.io.IOException;
\r
15 import java.lang.reflect.Array;
\r
16 import java.util.List;
\r
18 import org.simantics.databoard.Bindings;
\r
19 import org.simantics.databoard.Databoard;
\r
20 import org.simantics.databoard.binding.Binding;
\r
21 import org.simantics.databoard.binding.mutable.Variant;
\r
22 import org.simantics.databoard.parser.DataValuePrinter;
\r
23 import org.simantics.databoard.type.Datatype;
\r
24 import org.simantics.db.ReadGraph;
\r
25 import org.simantics.db.Resource;
\r
26 import org.simantics.db.common.utils.NameUtils;
\r
27 import org.simantics.db.common.utils.OrderedSetUtils;
\r
28 import org.simantics.db.exception.BindingException;
\r
29 import org.simantics.db.exception.DatabaseException;
\r
30 import org.simantics.db.exception.DoesNotContainValueException;
\r
31 import org.simantics.db.exception.ServiceException;
\r
32 import org.simantics.db.exception.ValidationException;
\r
33 import org.simantics.layer0.Layer0;
\r
34 import org.simantics.layer0.utils.representation.StringRepresentation2;
\r
36 public class DefaultStringRepresentation2 implements StringRepresentation2 {
\r
38 private static final int MAX_ARRAY_VALUE_LENGTH_SHOWN = 32;
\r
42 public DefaultStringRepresentation2(Resource r) {
\r
47 public String get(ReadGraph g) throws DatabaseException {
\r
48 return fullValueToString(g, r);
\r
52 public String get(ReadGraph g, int index) throws DatabaseException {
\r
53 Layer0 L0 = Layer0.getInstance(g);
\r
54 if (g.isInstanceOf(r, L0.OrderedSet)) {
\r
55 List<Resource> l = OrderedSetUtils.toList(g, r);
\r
56 return fullValueToString(g, l.get(index));
\r
59 Object value = g.getValue(r);
\r
60 return Array.get(value, index).toString();
\r
64 public String get(ReadGraph g, int start, int size) throws DatabaseException {
\r
65 Object value = g.getValue(r);
\r
66 int valueSize = Array.getLength(value);
\r
67 int end = start+size;
\r
68 if (start > valueSize || end > valueSize)
\r
69 throw new IndexOutOfBoundsException("value size is " + valueSize + ", requested range [" + start + "-" + (end-1) + "]");
\r
71 StringBuilder b = new StringBuilder();
\r
72 boolean first = true;
\r
73 for (int i = start; i < end; ++i) {
\r
78 b.append(Array.get(value, i));
\r
80 return b.toString();
\r
84 public int getArraySize(ReadGraph g) {
\r
86 Object value = g.getValue(r);
\r
87 return Array.getLength(value);
\r
88 } catch (DatabaseException e) {
\r
97 * @throws BindingException
\r
99 public static String fullValueToString(ReadGraph graph, Resource resource) throws ValidationException, ServiceException, BindingException {
\r
100 String representation = null;
\r
102 // First preference is name
\r
103 final Layer0 L0 = Layer0.getInstance(graph);
\r
104 if (graph.isInstanceOf(resource, L0.Relation)) {
\r
105 representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);
\r
106 if (representation == null)
\r
107 representation = graph.getPossibleRelatedValue(resource, L0.HasName);
\r
108 if (representation == null) {
\r
109 Resource inverse = graph.getPossibleInverse(resource);
\r
110 if (inverse != null) {
\r
111 representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);
\r
112 if (representation == null)
\r
113 representation = graph.getPossibleRelatedValue(inverse, L0.HasName);
\r
114 if (representation != null)
\r
115 representation = "Inverse Of " + representation;
\r
117 if (representation == null) {
\r
118 Resource single = graph.getPossibleObject(resource, L0.SubrelationOf);
\r
119 if(single != null) {
\r
120 String singleValue = fullValueToString(graph, single);
\r
121 representation = "<R " + singleValue;
\r
126 if (representation == null) {
\r
128 if (graph.isInstanceOf(resource, L0.Literal)) {
\r
129 representation = literalStr(graph, resource);
\r
131 boolean first = true;
\r
132 // Try name property/properties
\r
133 for (Resource label : graph.getObjects(resource, L0.HasLabel)) {
\r
135 representation += ", ";
\r
139 if(graph.isInstanceOf(label, L0.Literal))
\r
140 representation = literalStr(graph, label);
\r
142 if (representation == null) {
\r
143 for (Resource name : graph.getObjects(resource, L0.HasName)) {
\r
145 representation += ", ";
\r
149 representation = literalStr(graph, name);
\r
152 if (representation == null) {
\r
154 representation = ": ";
\r
156 for (Resource t : graph.getPrincipalTypes(resource)) {
\r
157 String typeName = graph.getPossibleRelatedValue(t, L0.HasName);
\r
158 if (typeName == null)
\r
159 typeName = "[unnamed type]";
\r
163 representation += ", ";
\r
164 representation += typeName;
\r
168 } catch (DoesNotContainValueException e) {
\r
169 throw new ValidationException(e);
\r
173 return representation;
\r
177 static Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
\r
179 private static String literalStr(ReadGraph graph, Resource resource) throws DoesNotContainValueException,
\r
180 ValidationException, ServiceException, BindingException {
\r
182 Layer0 L0 = Layer0.getInstance(graph);
\r
184 if (graph.isInstanceOf(resource, L0.Variant)) {
\r
185 return arrayStr(graph, resource, "variant");
\r
186 } else if (graph.isInstanceOf(resource, L0.String)) {
\r
187 return arrayStr(graph, resource, "string");
\r
188 } else if (graph.isInstanceOf(resource, L0.Integer)) {
\r
189 return arrayStr(graph, resource, "integer");
\r
190 } else if (graph.isInstanceOf(resource, L0.Long)) {
\r
191 return arrayStr(graph, resource, "long");
\r
192 } else if (graph.isInstanceOf(resource, L0.Boolean)) {
\r
193 return arrayStr(graph, resource, "boolean");
\r
194 } else if (graph.isInstanceOf(resource, L0.Double)) {
\r
195 return arrayStr(graph, resource, "double");
\r
196 } else if (graph.isInstanceOf(resource, L0.Float)) {
\r
197 return arrayStr(graph, resource, "float");
\r
198 } else if (graph.isInstanceOf(resource, L0.Byte)) {
\r
199 return arrayStr(graph, resource, "byte");
\r
200 } else if (graph.isInstanceOf(resource, L0.StringArray)) {
\r
201 return arrayStr(graph, resource, "string");
\r
202 } else if (graph.isInstanceOf(resource, L0.IntegerArray)) {
\r
203 return arrayStr(graph, resource, "integer");
\r
204 } else if (graph.isInstanceOf(resource, L0.LongArray)) {
\r
205 return arrayStr(graph, resource, "long");
\r
206 } else if (graph.isInstanceOf(resource, L0.BooleanArray)) {
\r
207 return arrayStr(graph, resource, "boolean");
\r
208 } else if (graph.isInstanceOf(resource, L0.DoubleArray)) {
\r
209 return arrayStr(graph, resource, "double");
\r
210 } else if (graph.isInstanceOf(resource, L0.FloatArray)) {
\r
211 return arrayStr(graph, resource, "float");
\r
212 } else if (graph.isInstanceOf(resource, L0.ByteArray)) {
\r
213 return arrayStr(graph, resource, "byte");
\r
214 } else if (graph.isInstanceOf(resource, L0.Literal)) {
\r
216 // Print value using its datatype
\r
217 Datatype dt = graph.getPossibleRelatedValue(resource, L0.HasDataType, dataTypeBinding);
\r
219 Binding dtb = Bindings.getBinding(dt);
\r
220 //System.out.println("datatype: " + dt);
\r
221 Object value = graph.getPossibleValue(resource, dtb);
\r
222 if (value != null) {
\r
223 return DataValuePrinter.writeValueSingleLine(dtb, value);
\r
226 return arrayStr(graph, resource, "unknown");
\r
228 return "[no value: " + NameUtils.getSafeName(graph, resource, true) + "]";
\r
229 } catch (IOException e) {
\r
230 throw new ServiceException(e);
\r
231 } catch (org.simantics.databoard.binding.error.BindingException e) {
\r
232 throw new ServiceException(e);
\r
235 return "[unsupported literal type: " + NameUtils.getSafeName(graph, resource, true) + "]";
\r
238 private static String arrayStr(ReadGraph graph, Resource resource, String type) throws DoesNotContainValueException, ValidationException, ServiceException {
\r
239 if ("variant".equals(type)) {
\r
241 Databoard db = graph.getService(Databoard.class);
\r
242 Variant variant = graph.getPossibleValue(resource, db.VARIANT);
\r
243 if (variant != null)
\r
244 return variant.toString();
\r
245 } catch (BindingException e) {
\r
246 return "BindingException: "+e.getMessage();
\r
249 Object value = graph.getPossibleValue(resource);
\r
251 return "no value : " + type + "[]";
\r
252 // return value.toString();
\r
254 Class<?> vclass = value.getClass();
\r
255 boolean isArray = vclass.isArray();
\r
258 length = Array.getLength(value);
\r
261 return value.toString();
\r
263 return String.valueOf(Array.get(value, 0));
\r
264 if (length > MAX_ARRAY_VALUE_LENGTH_SHOWN)
\r
265 return type + "[" + length + "]";
\r
267 StringBuilder b = new StringBuilder();
\r
268 boolean first = true;
\r
269 for (int i = 0; i < length; ++i) {
\r
274 b.append(Array.get(value, i));
\r
276 return b.toString();
\r