]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.spreadsheet.graph.creator;\r
13 \r
14 import java.io.IOException;\r
15 import java.util.Collection;\r
16 \r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.db.ReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.ResourceArray;\r
22 import org.simantics.db.common.request.Queries;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.service.SerialisationSupport;\r
25 import org.simantics.layer0.Layer0;\r
26 import org.simantics.simulation.ontology.SimulationResource;\r
27 import org.simantics.spreadsheet.ModelCellManager.CellCreationData;\r
28 import org.simantics.spreadsheet.common.cell.Parsers;\r
29 import org.simantics.spreadsheet.graph.request.RowsColumnsIndex;\r
30 import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
31 import org.simantics.structural.stubs.StructuralResource2;\r
32 import org.simantics.ui.dnd.ResourceTransferUtils;\r
33 import org.simantics.utils.datastructures.Pair;\r
34 \r
35 public class ResourceArrayCellCreator extends CellCreatorBase {\r
36 \r
37     public ResourceArrayCellCreator(Resource realization) {\r
38         super(realization);\r
39     }\r
40 \r
41         private Pair<String, String> splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException {\r
42                 int position = path.indexOf("/", startPosition);\r
43                 if(position == -1) return null;\r
44                 String prefix = path.substring(0, position);\r
45 //              System.out.println("prefix=" + prefix);\r
46                 Resource r = graph.getResource(prefix);\r
47                 if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) {\r
48                         return new Pair<String, String>(path.substring(0, position), path.substring(position+1, path.length())); \r
49                 } else {\r
50                         return splitModelAndStructure(graph, path, position + 1);\r
51                 }\r
52         }\r
53 \r
54         private Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {\r
55                 return splitModelAndStructure(graph, path, 8);\r
56         }\r
57 \r
58         String match(Pair<String, String> base, String value) {\r
59 \r
60                 if(!value.startsWith(base.first)) return null;\r
61                 \r
62                 \r
63                 System.out.println("match " + base + " - " + value);\r
64 //              String[] baseTokens = base.split("/");\r
65 //              String[] valueTokens = value.split("/");\r
66 //\r
67 //              int valueIndex = 0;\r
68 //              \r
69 //              for(int baseIndex = 0; baseIndex < baseTokens.length;) {\r
70 //                      base.\r
71 //                      if()\r
72 //              }\r
73 //              \r
74 //              Diagram/\r
75 //              ConfigurationValues/Diagram/Valve1/FSET/Flowrate\r
76                 return null;\r
77                 \r
78         }\r
79         \r
80     String toPath(ReadGraph graph, Pair<String, String> base, String serialisation) throws DatabaseException {\r
81 \r
82         Layer0 b = Layer0.getInstance(graph);\r
83         SerialisationSupport support = graph.getService(SerialisationSupport.class);\r
84         \r
85         System.out.println("baseUri=" + base);\r
86         \r
87         try {\r
88                 \r
89             ResourceArray[] arrays = ResourceTransferUtils.readStringTransferable(\r
90                     support.getResourceSerializer(), serialisation).toResourceArrayArray();\r
91             \r
92             StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
93             \r
94             String structure = null;\r
95             String property = null;\r
96             \r
97             for(ResourceArray array : arrays) {\r
98                 for(Resource resource : array) {\r
99                         if(graph.isInstanceOf(resource, b.Literal)) {\r
100 \r
101                                 String uri_ = graph.syncRequest(Queries.uri(resource));\r
102                                 System.out.println("value uri=" + uri_);\r
103                                 String match = match(base, uri_);\r
104                                 if(match != null) return match;\r
105                                 \r
106                         }\r
107                         \r
108 //                      Set<Resource> types = graph.getTypes(resource);\r
109 //                      if(types.contains(sr.Component) && !types.contains(sr.Composite)) {\r
110 //                              String uri = graph.syncRequest(Queries.uri(resource));\r
111 //                              if(uri != null) {\r
112 //                              Pair<String, String> modelAndStructure = splitModelAndStructure(graph, uri);\r
113 //                              if(modelAndStructure != null) {\r
114 ////                                    System.out.println("model=" + modelAndStructure.first);\r
115 ////                                    System.out.println("structure=" + modelAndStructure.second);\r
116 //                                      structure = modelAndStructure.second;\r
117 //                              }\r
118 //                              }\r
119 //                      }\r
120 //                      if(types.contains(b.Relation)) {\r
121 //                              String name = URIStringUtils.escape(graph.adapt(resource, String.class));\r
122 ////                            System.out.println("property=" + name);\r
123 //                              property = name;\r
124 //                      }\r
125                         \r
126                 }\r
127             }\r
128             \r
129 //            if(structure != null && property != null) {\r
130 //              return structure + "/" + property;\r
131 //            } else {\r
132 //              return null;\r
133 //            }\r
134             \r
135 //            if(arrays.length != 1) return null;\r
136 \r
137 //            final ResourceArray parameterArray = arrays[0];\r
138 //            if(parameterArray.size() < 3) return null;\r
139 \r
140 //            for(Resource r : parameterArray.resources)\r
141 //                System.out.println("ResourceArray: " + graph.adapt(r, String.class));\r
142             \r
143 //            if(parameterArray.resources.length == 6) {\r
144 //\r
145 //                Resource unit = parameterArray.resources[0];\r
146 //                Resource module = parameterArray.resources[1];\r
147 //                Resource property = parameterArray.resources[4];\r
148 //                \r
149 //                String unitName = graph.adapt(unit, String.class);\r
150 //                String moduleName = graph.adapt(module, String.class);\r
151 //                String propertyName = graph.adapt(property, String.class);\r
152 //                \r
153 //                return unitName + "/" + moduleName + "/" + propertyName;\r
154 //                \r
155 //            }\r
156 //            \r
157 //            if(parameterArray.resources.length == 4) {\r
158 //\r
159 //                Resource stream = parameterArray.resources[0];\r
160 //                Resource property = parameterArray.resources[2];\r
161 //                \r
162 //                String streamName = graph.getRelatedValue(stream, b.HasName, StringJavaBinding.INSTANCE);\r
163 //                String propertyName = graph.adapt(property, String.class);\r
164 //                \r
165 //                return streamName + "/" + propertyName;\r
166 //                \r
167 //            }\r
168             \r
169         } catch (IllegalArgumentException e) {\r
170             e.printStackTrace();\r
171         } catch (IOException e) {\r
172             e.printStackTrace();\r
173         }\r
174         \r
175         return null;\r
176         \r
177     }\r
178 \r
179     @Override\r
180     public void create(WriteGraph graph, final RowsColumnsIndex rowIndex, final RowsColumnsIndex columnIndex, final Collection<CellCreationData> data) throws DatabaseException {\r
181 \r
182         Layer0 l0 = Layer0.getInstance(graph);\r
183         SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
184 \r
185         Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);\r
186         Resource model = graph.getPossibleObject(spreadsheet, l0.PartOf);\r
187         \r
188         String baseUri = graph.syncRequest(Queries.uri(model));\r
189         Pair<String, String> base = splitModelAndStructure(graph, baseUri);\r
190 \r
191         for(CellCreationData datum : data) {\r
192 \r
193             Resource newCell = graph.newResource();\r
194             graph.claim(newCell, l0.InstanceOf, null, sr.PropertyCell2);\r
195 \r
196             String serialised = getProperty(datum, Parsers.RESOURCE_PATH_PROPERTY, Bindings.STRING);\r
197             String label = toPath(graph, base, serialised);\r
198 \r
199             Resource rowResource = rowIndex.getResource(datum.row);\r
200             Resource columnResource = columnIndex.getResource(datum.column);\r
201             graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);\r
202             graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);\r
203             graph.addLiteral(newCell, sr.Expression, sr.ExpressionOf, l0.String, label, Bindings.STRING);\r
204             graph.claim(spreadsheet, l0.ConsistsOf, l0.PartOf, newCell);\r
205             \r
206             \r
207 //            VariableReference reference = graph.syncRequest(new PathToReference(properties, path));\r
208 //            if(reference == null) continue;\r
209 //            Resource ref = properties.create(graph, reference);\r
210 //\r
211 //            Resource rowResource = rowIndex.getResource(datum.row);\r
212 //            Resource columnResource = columnIndex.getResource(datum.column);\r
213 //\r
214 //            graph.claim(newCell, sr.HasPropertyReference, ref);\r
215 //            graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);\r
216 //            graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);\r
217 //            graph.addValue(newCell, sr.HasWidth, sr.WidthOf, b.Integer, 1, IntegerJavaBinding.INSTANCE);\r
218 //            graph.addValue(newCell, sr.HasHeight, sr.HeightOf, b.Integer, 1, IntegerJavaBinding.INSTANCE);\r
219 //            graph.claim(spreadsheet, b.ConsistsOf, b.PartOf, newCell);\r
220             \r
221         }\r
222 \r
223     }\r
224 \r
225 \r
226 }\r