]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/ResourceArrayCellCreator.java.keep
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / creator / ResourceArrayCellCreator.java.keep
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/ResourceArrayCellCreator.java.keep b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/ResourceArrayCellCreator.java.keep
new file mode 100644 (file)
index 0000000..1742521
--- /dev/null
@@ -0,0 +1,226 @@
+/*******************************************************************************\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