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