-/*******************************************************************************\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.spreadsheet.graph.creator;\r
-\r
-import java.io.IOException;\r
-import java.util.Collection;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.ResourceArray;\r
-import org.simantics.db.common.request.Queries;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.service.SerialisationSupport;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.simulation.ontology.SimulationResource;\r
-import org.simantics.spreadsheet.ModelCellManager.CellCreationData;\r
-import org.simantics.spreadsheet.common.cell.Parsers;\r
-import org.simantics.spreadsheet.graph.request.RowsColumnsIndex;\r
-import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.ui.dnd.ResourceTransferUtils;\r
-import org.simantics.utils.datastructures.Pair;\r
-\r
-public class ResourceArrayCellCreator extends CellCreatorBase {\r
-\r
- public ResourceArrayCellCreator(Resource realization) {\r
- super(realization);\r
- }\r
-\r
- private Pair<String, String> splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException {\r
- int position = path.indexOf("/", startPosition);\r
- if(position == -1) return null;\r
- String prefix = path.substring(0, position);\r
-// System.out.println("prefix=" + prefix);\r
- Resource r = graph.getResource(prefix);\r
- if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {\r
- return new Pair<String, String>(path.substring(0, position), path.substring(position+1, path.length())); \r
- } else {\r
- return splitModelAndStructure(graph, path, position + 1);\r
- }\r
- }\r
-\r
- private Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {\r
- return splitModelAndStructure(graph, path, 8);\r
- }\r
-\r
- String match(Pair<String, String> base, String value) {\r
-\r
- if(!value.startsWith(base.first)) return null;\r
- \r
- \r
- System.out.println("match " + base + " - " + value);\r
-// String[] baseTokens = base.split("/");\r
-// String[] valueTokens = value.split("/");\r
-//\r
-// int valueIndex = 0;\r
-// \r
-// for(int baseIndex = 0; baseIndex < baseTokens.length;) {\r
-// base.\r
-// if()\r
-// }\r
-// \r
-// Diagram/\r
-// ConfigurationValues/Diagram/Valve1/FSET/Flowrate\r
- return null;\r
- \r
- }\r
- \r
- String toPath(ReadGraph graph, Pair<String, String> base, String serialisation) throws DatabaseException {\r
-\r
- Layer0 b = Layer0.getInstance(graph);\r
- SerialisationSupport support = graph.getService(SerialisationSupport.class);\r
- \r
- System.out.println("baseUri=" + base);\r
- \r
- try {\r
- \r
- ResourceArray[] arrays = ResourceTransferUtils.readStringTransferable(\r
- support.getResourceSerializer(), serialisation).toResourceArrayArray();\r
- \r
- StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
- \r
- String structure = null;\r
- String property = null;\r
- \r
- for(ResourceArray array : arrays) {\r
- for(Resource resource : array) {\r
- if(graph.isInstanceOf(resource, b.Literal)) {\r
-\r
- String uri_ = graph.syncRequest(Queries.uri(resource));\r
- System.out.println("value uri=" + uri_);\r
- String match = match(base, uri_);\r
- if(match != null) return match;\r
- \r
- }\r
- \r
-// Set<Resource> types = graph.getTypes(resource);\r
-// if(types.contains(sr.Component) && !types.contains(sr.Composite)) {\r
-// String uri = graph.syncRequest(Queries.uri(resource));\r
-// if(uri != null) {\r
-// Pair<String, String> modelAndStructure = splitModelAndStructure(graph, uri);\r
-// if(modelAndStructure != null) {\r
-//// System.out.println("model=" + modelAndStructure.first);\r
-//// System.out.println("structure=" + modelAndStructure.second);\r
-// structure = modelAndStructure.second;\r
-// }\r
-// }\r
-// }\r
-// if(types.contains(b.Relation)) {\r
-// String name = URIStringUtils.escape(graph.adapt(resource, String.class));\r
-//// System.out.println("property=" + name);\r
-// property = name;\r
-// }\r
- \r
- }\r
- }\r
- \r
-// if(structure != null && property != null) {\r
-// return structure + "/" + property;\r
-// } else {\r
-// return null;\r
-// }\r
- \r
-// if(arrays.length != 1) return null;\r
-\r
-// final ResourceArray parameterArray = arrays[0];\r
-// if(parameterArray.size() < 3) return null;\r
-\r
-// for(Resource r : parameterArray.resources)\r
-// System.out.println("ResourceArray: " + graph.adapt(r, String.class));\r
- \r
-// if(parameterArray.resources.length == 6) {\r
-//\r
-// Resource unit = parameterArray.resources[0];\r
-// Resource module = parameterArray.resources[1];\r
-// Resource property = parameterArray.resources[4];\r
-// \r
-// String unitName = graph.adapt(unit, String.class);\r
-// String moduleName = graph.adapt(module, String.class);\r
-// String propertyName = graph.adapt(property, String.class);\r
-// \r
-// return unitName + "/" + moduleName + "/" + propertyName;\r
-// \r
-// }\r
-// \r
-// if(parameterArray.resources.length == 4) {\r
-//\r
-// Resource stream = parameterArray.resources[0];\r
-// Resource property = parameterArray.resources[2];\r
-// \r
-// String streamName = graph.getRelatedValue(stream, b.HasName, StringJavaBinding.INSTANCE);\r
-// String propertyName = graph.adapt(property, String.class);\r
-// \r
-// return streamName + "/" + propertyName;\r
-// \r
-// }\r
- \r
- } catch (IllegalArgumentException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
- }\r
- \r
- return null;\r
- \r
- }\r
-\r
- @Override\r
- public void create(WriteGraph graph, final RowsColumnsIndex rowIndex, final RowsColumnsIndex columnIndex, final Collection<CellCreationData> data) throws DatabaseException {\r
-\r
- Layer0 l0 = Layer0.getInstance(graph);\r
- SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
-\r
- Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);\r
- Resource model = graph.getPossibleObject(spreadsheet, l0.PartOf);\r
- \r
- String baseUri = graph.syncRequest(Queries.uri(model));\r
- Pair<String, String> base = splitModelAndStructure(graph, baseUri);\r
-\r
- for(CellCreationData datum : data) {\r
-\r
- Resource newCell = graph.newResource();\r
- graph.claim(newCell, l0.InstanceOf, null, sr.PropertyCell2);\r
-\r
- String serialised = getProperty(datum, Parsers.RESOURCE_PATH_PROPERTY, Bindings.STRING);\r
- String label = toPath(graph, base, serialised);\r
-\r
- Resource rowResource = rowIndex.getResource(datum.row);\r
- Resource columnResource = columnIndex.getResource(datum.column);\r
- graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);\r
- graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);\r
- graph.addLiteral(newCell, sr.Expression, sr.ExpressionOf, l0.String, label, Bindings.STRING);\r
- graph.claim(spreadsheet, l0.ConsistsOf, l0.PartOf, newCell);\r
- \r
- \r
-// VariableReference reference = graph.syncRequest(new PathToReference(properties, path));\r
-// if(reference == null) continue;\r
-// Resource ref = properties.create(graph, reference);\r
-//\r
-// Resource rowResource = rowIndex.getResource(datum.row);\r
-// Resource columnResource = columnIndex.getResource(datum.column);\r
-//\r
-// graph.claim(newCell, sr.HasPropertyReference, ref);\r
-// graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);\r
-// graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);\r
-// graph.addValue(newCell, sr.HasWidth, sr.WidthOf, b.Integer, 1, IntegerJavaBinding.INSTANCE);\r
-// graph.addValue(newCell, sr.HasHeight, sr.HeightOf, b.Integer, 1, IntegerJavaBinding.INSTANCE);\r
-// graph.claim(spreadsheet, b.ConsistsOf, b.PartOf, newCell);\r
- \r
- }\r
-\r
- }\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.spreadsheet.graph.creator;
+
+import java.io.IOException;
+import java.util.Collection;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.ResourceArray;
+import org.simantics.db.common.request.Queries;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.SerialisationSupport;
+import org.simantics.layer0.Layer0;
+import org.simantics.simulation.ontology.SimulationResource;
+import org.simantics.spreadsheet.ModelCellManager.CellCreationData;
+import org.simantics.spreadsheet.common.cell.Parsers;
+import org.simantics.spreadsheet.graph.request.RowsColumnsIndex;
+import org.simantics.spreadsheet.resource.SpreadsheetResource;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.ui.dnd.ResourceTransferUtils;
+import org.simantics.utils.datastructures.Pair;
+
+public class ResourceArrayCellCreator extends CellCreatorBase {
+
+ public ResourceArrayCellCreator(Resource realization) {
+ super(realization);
+ }
+
+ private Pair<String, String> splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException {
+ int position = path.indexOf("/", startPosition);
+ if(position == -1) return null;
+ String prefix = path.substring(0, position);
+// System.out.println("prefix=" + prefix);
+ Resource r = graph.getResource(prefix);
+ if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {
+ return new Pair<String, String>(path.substring(0, position), path.substring(position+1, path.length()));
+ } else {
+ return splitModelAndStructure(graph, path, position + 1);
+ }
+ }
+
+ private Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {
+ return splitModelAndStructure(graph, path, 8);
+ }
+
+ String match(Pair<String, String> base, String value) {
+
+ if(!value.startsWith(base.first)) return null;
+
+
+ System.out.println("match " + base + " - " + value);
+// String[] baseTokens = base.split("/");
+// String[] valueTokens = value.split("/");
+//
+// int valueIndex = 0;
+//
+// for(int baseIndex = 0; baseIndex < baseTokens.length;) {
+// base.
+// if()
+// }
+//
+// Diagram/
+// ConfigurationValues/Diagram/Valve1/FSET/Flowrate
+ return null;
+
+ }
+
+ String toPath(ReadGraph graph, Pair<String, String> base, String serialisation) throws DatabaseException {
+
+ Layer0 b = Layer0.getInstance(graph);
+ SerialisationSupport support = graph.getService(SerialisationSupport.class);
+
+ System.out.println("baseUri=" + base);
+
+ try {
+
+ ResourceArray[] arrays = ResourceTransferUtils.readStringTransferable(
+ support.getResourceSerializer(), serialisation).toResourceArrayArray();
+
+ StructuralResource2 sr = StructuralResource2.getInstance(graph);
+
+ String structure = null;
+ String property = null;
+
+ for(ResourceArray array : arrays) {
+ for(Resource resource : array) {
+ if(graph.isInstanceOf(resource, b.Literal)) {
+
+ String uri_ = graph.syncRequest(Queries.uri(resource));
+ System.out.println("value uri=" + uri_);
+ String match = match(base, uri_);
+ if(match != null) return match;
+
+ }
+
+// Set<Resource> types = graph.getTypes(resource);
+// if(types.contains(sr.Component) && !types.contains(sr.Composite)) {
+// String uri = graph.syncRequest(Queries.uri(resource));
+// if(uri != null) {
+// Pair<String, String> modelAndStructure = splitModelAndStructure(graph, uri);
+// if(modelAndStructure != null) {
+//// System.out.println("model=" + modelAndStructure.first);
+//// System.out.println("structure=" + modelAndStructure.second);
+// structure = modelAndStructure.second;
+// }
+// }
+// }
+// if(types.contains(b.Relation)) {
+// String name = URIStringUtils.escape(graph.adapt(resource, String.class));
+//// System.out.println("property=" + name);
+// property = name;
+// }
+
+ }
+ }
+
+// if(structure != null && property != null) {
+// return structure + "/" + property;
+// } else {
+// return null;
+// }
+
+// if(arrays.length != 1) return null;
+
+// final ResourceArray parameterArray = arrays[0];
+// if(parameterArray.size() < 3) return null;
+
+// for(Resource r : parameterArray.resources)
+// System.out.println("ResourceArray: " + graph.adapt(r, String.class));
+
+// if(parameterArray.resources.length == 6) {
+//
+// Resource unit = parameterArray.resources[0];
+// Resource module = parameterArray.resources[1];
+// Resource property = parameterArray.resources[4];
+//
+// String unitName = graph.adapt(unit, String.class);
+// String moduleName = graph.adapt(module, String.class);
+// String propertyName = graph.adapt(property, String.class);
+//
+// return unitName + "/" + moduleName + "/" + propertyName;
+//
+// }
+//
+// if(parameterArray.resources.length == 4) {
+//
+// Resource stream = parameterArray.resources[0];
+// Resource property = parameterArray.resources[2];
+//
+// String streamName = graph.getRelatedValue(stream, b.HasName, StringJavaBinding.INSTANCE);
+// String propertyName = graph.adapt(property, String.class);
+//
+// return streamName + "/" + propertyName;
+//
+// }
+
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+
+ }
+
+ @Override
+ public void create(WriteGraph graph, final RowsColumnsIndex rowIndex, final RowsColumnsIndex columnIndex, final Collection<CellCreationData> data) throws DatabaseException {
+
+ Layer0 l0 = Layer0.getInstance(graph);
+ SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
+
+ Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);
+ Resource model = graph.getPossibleObject(spreadsheet, l0.PartOf);
+
+ String baseUri = graph.syncRequest(Queries.uri(model));
+ Pair<String, String> base = splitModelAndStructure(graph, baseUri);
+
+ for(CellCreationData datum : data) {
+
+ Resource newCell = graph.newResource();
+ graph.claim(newCell, l0.InstanceOf, null, sr.PropertyCell2);
+
+ String serialised = getProperty(datum, Parsers.RESOURCE_PATH_PROPERTY, Bindings.STRING);
+ String label = toPath(graph, base, serialised);
+
+ Resource rowResource = rowIndex.getResource(datum.row);
+ Resource columnResource = columnIndex.getResource(datum.column);
+ graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);
+ graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);
+ graph.addLiteral(newCell, sr.Expression, sr.ExpressionOf, l0.String, label, Bindings.STRING);
+ graph.claim(spreadsheet, l0.ConsistsOf, l0.PartOf, newCell);
+
+
+// VariableReference reference = graph.syncRequest(new PathToReference(properties, path));
+// if(reference == null) continue;
+// Resource ref = properties.create(graph, reference);
+//
+// Resource rowResource = rowIndex.getResource(datum.row);
+// Resource columnResource = columnIndex.getResource(datum.column);
+//
+// graph.claim(newCell, sr.HasPropertyReference, ref);
+// graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);
+// graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);
+// graph.addValue(newCell, sr.HasWidth, sr.WidthOf, b.Integer, 1, IntegerJavaBinding.INSTANCE);
+// graph.addValue(newCell, sr.HasHeight, sr.HeightOf, b.Integer, 1, IntegerJavaBinding.INSTANCE);
+// graph.claim(spreadsheet, b.ConsistsOf, b.PartOf, newCell);
+
+ }
+
+ }
+
+
+}