-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.layer0.utils.representation.representations;\r
-\r
-import java.io.IOException;\r
-import java.lang.reflect.Array;\r
-import java.util.List;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.Databoard;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.databoard.parser.DataValuePrinter;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.common.utils.OrderedSetUtils;\r
-import org.simantics.db.exception.BindingException;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.DoesNotContainValueException;\r
-import org.simantics.db.exception.ServiceException;\r
-import org.simantics.db.exception.ValidationException;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.representation.StringRepresentation2;\r
-\r
-public class DefaultStringRepresentation2 implements StringRepresentation2 {\r
-\r
- private static final int MAX_ARRAY_VALUE_LENGTH_SHOWN = 32;\r
-\r
- Resource r;\r
-\r
- public DefaultStringRepresentation2(Resource r) {\r
- this.r = r;\r
- }\r
-\r
- @Override\r
- public String get(ReadGraph g) throws DatabaseException {\r
- return fullValueToString(g, r);\r
- }\r
-\r
- @Override\r
- public String get(ReadGraph g, int index) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(g);\r
- if (g.isInstanceOf(r, L0.OrderedSet)) {\r
- List<Resource> l = OrderedSetUtils.toList(g, r);\r
- return fullValueToString(g, l.get(index));\r
- }\r
-\r
- Object value = g.getValue(r);\r
- return Array.get(value, index).toString();\r
- }\r
-\r
- @Override\r
- public String get(ReadGraph g, int start, int size) throws DatabaseException {\r
- Object value = g.getValue(r);\r
- int valueSize = Array.getLength(value);\r
- int end = start+size;\r
- if (start > valueSize || end > valueSize)\r
- throw new IndexOutOfBoundsException("value size is " + valueSize + ", requested range [" + start + "-" + (end-1) + "]");\r
-\r
- StringBuilder b = new StringBuilder();\r
- boolean first = true;\r
- for (int i = start; i < end; ++i) {\r
- if (!first) {\r
- b.append(',');\r
- }\r
- first = false;\r
- b.append(Array.get(value, i));\r
- }\r
- return b.toString();\r
- }\r
-\r
- @Override\r
- public int getArraySize(ReadGraph g) {\r
- try {\r
- Object value = g.getValue(r);\r
- return Array.getLength(value);\r
- } catch (DatabaseException e) {\r
- return -1;\r
- }\r
- }\r
-\r
- /**\r
- * @param graph\r
- * @param resource\r
- * @return\r
- * @throws BindingException\r
- */\r
- public static String fullValueToString(ReadGraph graph, Resource resource) throws ValidationException, ServiceException, BindingException {\r
- String representation = null;\r
-\r
- // First preference is name\r
- final Layer0 L0 = Layer0.getInstance(graph);\r
- if (graph.isInstanceOf(resource, L0.Relation)) {\r
- representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);\r
- if (representation == null)\r
- representation = graph.getPossibleRelatedValue(resource, L0.HasName);\r
- if (representation == null) {\r
- Resource inverse = graph.getPossibleInverse(resource);\r
- if (inverse != null) {\r
- representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);\r
- if (representation == null)\r
- representation = graph.getPossibleRelatedValue(inverse, L0.HasName);\r
- if (representation != null)\r
- representation = "Inverse Of " + representation;\r
- }\r
- if (representation == null) {\r
- Resource single = graph.getPossibleObject(resource, L0.SubrelationOf);\r
- if(single != null) {\r
- String singleValue = fullValueToString(graph, single);\r
- representation = "<R " + singleValue;\r
- }\r
- }\r
- }\r
- }\r
- if (representation == null) {\r
- try {\r
- if (graph.isInstanceOf(resource, L0.Literal)) {\r
- representation = literalStr(graph, resource);\r
- } else {\r
- boolean first = true;\r
- // Try name property/properties\r
- for (Resource label : graph.getObjects(resource, L0.HasLabel)) {\r
- if (!first) {\r
- representation += ", ";\r
- } else {\r
- first = false;\r
- }\r
- if(graph.isInstanceOf(label, L0.Literal))\r
- representation = literalStr(graph, label);\r
- }\r
- if (representation == null) {\r
- for (Resource name : graph.getObjects(resource, L0.HasName)) {\r
- if (!first) {\r
- representation += ", ";\r
- } else {\r
- first = false;\r
- }\r
- representation = literalStr(graph, name);\r
- }\r
- }\r
- if (representation == null) {\r
- // Types\r
- representation = ": ";\r
- first = true;\r
- for (Resource t : graph.getPrincipalTypes(resource)) {\r
- String typeName = graph.getPossibleRelatedValue(t, L0.HasName);\r
- if (typeName == null)\r
- typeName = "[unnamed type]";\r
- if (first)\r
- first = false;\r
- else\r
- representation += ", ";\r
- representation += typeName;\r
- }\r
- }\r
- }\r
- } catch (DoesNotContainValueException e) {\r
- throw new ValidationException(e);\r
- }\r
- }\r
-\r
- return representation;\r
-\r
- }\r
-\r
- static Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);\r
-\r
- private static String literalStr(ReadGraph graph, Resource resource) throws DoesNotContainValueException,\r
- ValidationException, ServiceException, BindingException {\r
-\r
- Layer0 L0 = Layer0.getInstance(graph);\r
-\r
- if (graph.isInstanceOf(resource, L0.Variant)) {\r
- return arrayStr(graph, resource, "variant");\r
- } else if (graph.isInstanceOf(resource, L0.String)) {\r
- return arrayStr(graph, resource, "string");\r
- } else if (graph.isInstanceOf(resource, L0.Integer)) {\r
- return arrayStr(graph, resource, "integer");\r
- } else if (graph.isInstanceOf(resource, L0.Long)) {\r
- return arrayStr(graph, resource, "long");\r
- } else if (graph.isInstanceOf(resource, L0.Boolean)) {\r
- return arrayStr(graph, resource, "boolean");\r
- } else if (graph.isInstanceOf(resource, L0.Double)) {\r
- return arrayStr(graph, resource, "double");\r
- } else if (graph.isInstanceOf(resource, L0.Float)) {\r
- return arrayStr(graph, resource, "float");\r
- } else if (graph.isInstanceOf(resource, L0.Byte)) {\r
- return arrayStr(graph, resource, "byte");\r
- } else if (graph.isInstanceOf(resource, L0.StringArray)) {\r
- return arrayStr(graph, resource, "string");\r
- } else if (graph.isInstanceOf(resource, L0.IntegerArray)) {\r
- return arrayStr(graph, resource, "integer");\r
- } else if (graph.isInstanceOf(resource, L0.LongArray)) {\r
- return arrayStr(graph, resource, "long");\r
- } else if (graph.isInstanceOf(resource, L0.BooleanArray)) {\r
- return arrayStr(graph, resource, "boolean");\r
- } else if (graph.isInstanceOf(resource, L0.DoubleArray)) {\r
- return arrayStr(graph, resource, "double");\r
- } else if (graph.isInstanceOf(resource, L0.FloatArray)) {\r
- return arrayStr(graph, resource, "float");\r
- } else if (graph.isInstanceOf(resource, L0.ByteArray)) {\r
- return arrayStr(graph, resource, "byte");\r
- } else if (graph.isInstanceOf(resource, L0.Literal)) {\r
- try {\r
- // Print value using its datatype\r
- Datatype dt = graph.getPossibleRelatedValue(resource, L0.HasDataType, dataTypeBinding);\r
- if (dt != null) {\r
- Binding dtb = Bindings.getBinding(dt);\r
- //System.out.println("datatype: " + dt);\r
- Object value = graph.getPossibleValue(resource, dtb);\r
- if (value != null) {\r
- return DataValuePrinter.writeValueSingleLine(dtb, value);\r
- }\r
- } else {\r
- return arrayStr(graph, resource, "unknown");\r
- }\r
- return "[no value: " + NameUtils.getSafeName(graph, resource, true) + "]";\r
- } catch (IOException e) {\r
- throw new ServiceException(e);\r
- } catch (org.simantics.databoard.binding.error.BindingException e) {\r
- throw new ServiceException(e);\r
- }\r
- }\r
- return "[unsupported literal type: " + NameUtils.getSafeName(graph, resource, true) + "]";\r
- }\r
-\r
- private static String arrayStr(ReadGraph graph, Resource resource, String type) throws DoesNotContainValueException, ValidationException, ServiceException {\r
- if ("variant".equals(type)) {\r
- try {\r
- Databoard db = graph.getService(Databoard.class);\r
- Variant variant = graph.getPossibleValue(resource, db.VARIANT);\r
- if (variant != null)\r
- return variant.toString();\r
- } catch (BindingException e) {\r
- return "BindingException: "+e.getMessage();\r
- }\r
- }\r
- Object value = graph.getPossibleValue(resource);\r
- if (value == null)\r
- return "no value : " + type + "[]";\r
-// return value.toString();\r
-\r
- Class<?> vclass = value.getClass();\r
- boolean isArray = vclass.isArray();\r
- int length = 1;\r
- if (isArray)\r
- length = Array.getLength(value);\r
-\r
- if (!isArray)\r
- return value.toString();\r
- if (length == 1)\r
- return String.valueOf(Array.get(value, 0));\r
- if (length > MAX_ARRAY_VALUE_LENGTH_SHOWN)\r
- return type + "[" + length + "]";\r
-\r
- StringBuilder b = new StringBuilder();\r
- boolean first = true;\r
- for (int i = 0; i < length; ++i) {\r
- if (!first) {\r
- b.append(',');\r
- }\r
- first = false;\r
- b.append(Array.get(value, i));\r
- }\r
- return b.toString();\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.layer0.utils.representation.representations;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.List;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.Databoard;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.databoard.parser.DataValuePrinter;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.common.utils.OrderedSetUtils;
+import org.simantics.db.exception.BindingException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.DoesNotContainValueException;
+import org.simantics.db.exception.ServiceException;
+import org.simantics.db.exception.ValidationException;
+import org.simantics.layer0.Layer0;
+import org.simantics.layer0.utils.representation.StringRepresentation2;
+
+public class DefaultStringRepresentation2 implements StringRepresentation2 {
+
+ private static final int MAX_ARRAY_VALUE_LENGTH_SHOWN = 32;
+
+ Resource r;
+
+ public DefaultStringRepresentation2(Resource r) {
+ this.r = r;
+ }
+
+ @Override
+ public String get(ReadGraph g) throws DatabaseException {
+ return fullValueToString(g, r);
+ }
+
+ @Override
+ public String get(ReadGraph g, int index) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(g);
+ if (g.isInstanceOf(r, L0.OrderedSet)) {
+ List<Resource> l = OrderedSetUtils.toList(g, r);
+ return fullValueToString(g, l.get(index));
+ }
+
+ Object value = g.getValue(r);
+ return Array.get(value, index).toString();
+ }
+
+ @Override
+ public String get(ReadGraph g, int start, int size) throws DatabaseException {
+ Object value = g.getValue(r);
+ int valueSize = Array.getLength(value);
+ int end = start+size;
+ if (start > valueSize || end > valueSize)
+ throw new IndexOutOfBoundsException("value size is " + valueSize + ", requested range [" + start + "-" + (end-1) + "]");
+
+ StringBuilder b = new StringBuilder();
+ boolean first = true;
+ for (int i = start; i < end; ++i) {
+ if (!first) {
+ b.append(',');
+ }
+ first = false;
+ b.append(Array.get(value, i));
+ }
+ return b.toString();
+ }
+
+ @Override
+ public int getArraySize(ReadGraph g) {
+ try {
+ Object value = g.getValue(r);
+ return Array.getLength(value);
+ } catch (DatabaseException e) {
+ return -1;
+ }
+ }
+
+ /**
+ * @param graph
+ * @param resource
+ * @return
+ * @throws BindingException
+ */
+ public static String fullValueToString(ReadGraph graph, Resource resource) throws ValidationException, ServiceException, BindingException {
+ String representation = null;
+
+ // First preference is name
+ final Layer0 L0 = Layer0.getInstance(graph);
+ if (graph.isInstanceOf(resource, L0.Relation)) {
+ representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);
+ if (representation == null)
+ representation = graph.getPossibleRelatedValue(resource, L0.HasName);
+ if (representation == null) {
+ Resource inverse = graph.getPossibleInverse(resource);
+ if (inverse != null) {
+ representation = graph.getPossibleRelatedValue(resource, L0.HasLabel);
+ if (representation == null)
+ representation = graph.getPossibleRelatedValue(inverse, L0.HasName);
+ if (representation != null)
+ representation = "Inverse Of " + representation;
+ }
+ if (representation == null) {
+ Resource single = graph.getPossibleObject(resource, L0.SubrelationOf);
+ if(single != null) {
+ String singleValue = fullValueToString(graph, single);
+ representation = "<R " + singleValue;
+ }
+ }
+ }
+ }
+ if (representation == null) {
+ try {
+ if (graph.isInstanceOf(resource, L0.Literal)) {
+ representation = literalStr(graph, resource);
+ } else {
+ boolean first = true;
+ // Try name property/properties
+ for (Resource label : graph.getObjects(resource, L0.HasLabel)) {
+ if (!first) {
+ representation += ", ";
+ } else {
+ first = false;
+ }
+ if(graph.isInstanceOf(label, L0.Literal))
+ representation = literalStr(graph, label);
+ }
+ if (representation == null) {
+ for (Resource name : graph.getObjects(resource, L0.HasName)) {
+ if (!first) {
+ representation += ", ";
+ } else {
+ first = false;
+ }
+ representation = literalStr(graph, name);
+ }
+ }
+ if (representation == null) {
+ // Types
+ representation = ": ";
+ first = true;
+ for (Resource t : graph.getPrincipalTypes(resource)) {
+ String typeName = graph.getPossibleRelatedValue(t, L0.HasName);
+ if (typeName == null)
+ typeName = "[unnamed type]";
+ if (first)
+ first = false;
+ else
+ representation += ", ";
+ representation += typeName;
+ }
+ }
+ }
+ } catch (DoesNotContainValueException e) {
+ throw new ValidationException(e);
+ }
+ }
+
+ return representation;
+
+ }
+
+ static Binding dataTypeBinding = Bindings.getBindingUnchecked(Datatype.class);
+
+ private static String literalStr(ReadGraph graph, Resource resource) throws DoesNotContainValueException,
+ ValidationException, ServiceException, BindingException {
+
+ Layer0 L0 = Layer0.getInstance(graph);
+
+ if (graph.isInstanceOf(resource, L0.Variant)) {
+ return arrayStr(graph, resource, "variant");
+ } else if (graph.isInstanceOf(resource, L0.String)) {
+ return arrayStr(graph, resource, "string");
+ } else if (graph.isInstanceOf(resource, L0.Integer)) {
+ return arrayStr(graph, resource, "integer");
+ } else if (graph.isInstanceOf(resource, L0.Long)) {
+ return arrayStr(graph, resource, "long");
+ } else if (graph.isInstanceOf(resource, L0.Boolean)) {
+ return arrayStr(graph, resource, "boolean");
+ } else if (graph.isInstanceOf(resource, L0.Double)) {
+ return arrayStr(graph, resource, "double");
+ } else if (graph.isInstanceOf(resource, L0.Float)) {
+ return arrayStr(graph, resource, "float");
+ } else if (graph.isInstanceOf(resource, L0.Byte)) {
+ return arrayStr(graph, resource, "byte");
+ } else if (graph.isInstanceOf(resource, L0.StringArray)) {
+ return arrayStr(graph, resource, "string");
+ } else if (graph.isInstanceOf(resource, L0.IntegerArray)) {
+ return arrayStr(graph, resource, "integer");
+ } else if (graph.isInstanceOf(resource, L0.LongArray)) {
+ return arrayStr(graph, resource, "long");
+ } else if (graph.isInstanceOf(resource, L0.BooleanArray)) {
+ return arrayStr(graph, resource, "boolean");
+ } else if (graph.isInstanceOf(resource, L0.DoubleArray)) {
+ return arrayStr(graph, resource, "double");
+ } else if (graph.isInstanceOf(resource, L0.FloatArray)) {
+ return arrayStr(graph, resource, "float");
+ } else if (graph.isInstanceOf(resource, L0.ByteArray)) {
+ return arrayStr(graph, resource, "byte");
+ } else if (graph.isInstanceOf(resource, L0.Literal)) {
+ try {
+ // Print value using its datatype
+ Datatype dt = graph.getPossibleRelatedValue(resource, L0.HasDataType, dataTypeBinding);
+ if (dt != null) {
+ Binding dtb = Bindings.getBinding(dt);
+ //System.out.println("datatype: " + dt);
+ Object value = graph.getPossibleValue(resource, dtb);
+ if (value != null) {
+ return DataValuePrinter.writeValueSingleLine(dtb, value);
+ }
+ } else {
+ return arrayStr(graph, resource, "unknown");
+ }
+ return "[no value: " + NameUtils.getSafeName(graph, resource, true) + "]";
+ } catch (IOException e) {
+ throw new ServiceException(e);
+ } catch (org.simantics.databoard.binding.error.BindingException e) {
+ throw new ServiceException(e);
+ }
+ }
+ return "[unsupported literal type: " + NameUtils.getSafeName(graph, resource, true) + "]";
+ }
+
+ private static String arrayStr(ReadGraph graph, Resource resource, String type) throws DoesNotContainValueException, ValidationException, ServiceException {
+ if ("variant".equals(type)) {
+ try {
+ Databoard db = graph.getService(Databoard.class);
+ Variant variant = graph.getPossibleValue(resource, db.VARIANT);
+ if (variant != null)
+ return variant.toString();
+ } catch (BindingException e) {
+ return "BindingException: "+e.getMessage();
+ }
+ }
+ Object value = graph.getPossibleValue(resource);
+ if (value == null)
+ return "no value : " + type + "[]";
+// return value.toString();
+
+ Class<?> vclass = value.getClass();
+ boolean isArray = vclass.isArray();
+ int length = 1;
+ if (isArray)
+ length = Array.getLength(value);
+
+ if (!isArray)
+ return value.toString();
+ if (length == 1)
+ return String.valueOf(Array.get(value, 0));
+ if (length > MAX_ARRAY_VALUE_LENGTH_SHOWN)
+ return type + "[" + length + "]";
+
+ StringBuilder b = new StringBuilder();
+ boolean first = true;
+ for (int i = 0; i < length; ++i) {
+ if (!first) {
+ b.append(',');
+ }
+ first = false;
+ b.append(Array.get(value, i));
+ }
+ return b.toString();
+ }
+
+}