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