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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.spreadsheet.graph.creator;
\r
14 import java.io.IOException;
\r
15 import java.util.Collection;
\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
35 public class ResourceArrayCellCreator extends CellCreatorBase {
\r
37 public ResourceArrayCellCreator(Resource realization) {
\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
50 return splitModelAndStructure(graph, path, position + 1);
\r
54 private Pair<String, String> splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException {
\r
55 return splitModelAndStructure(graph, path, 8);
\r
58 String match(Pair<String, String> base, String value) {
\r
60 if(!value.startsWith(base.first)) return null;
\r
63 System.out.println("match " + base + " - " + value);
\r
64 // String[] baseTokens = base.split("/");
\r
65 // String[] valueTokens = value.split("/");
\r
67 // int valueIndex = 0;
\r
69 // for(int baseIndex = 0; baseIndex < baseTokens.length;) {
\r
75 // ConfigurationValues/Diagram/Valve1/FSET/Flowrate
\r
80 String toPath(ReadGraph graph, Pair<String, String> base, String serialisation) throws DatabaseException {
\r
82 Layer0 b = Layer0.getInstance(graph);
\r
83 SerialisationSupport support = graph.getService(SerialisationSupport.class);
\r
85 System.out.println("baseUri=" + base);
\r
89 ResourceArray[] arrays = ResourceTransferUtils.readStringTransferable(
\r
90 support.getResourceSerializer(), serialisation).toResourceArrayArray();
\r
92 StructuralResource2 sr = StructuralResource2.getInstance(graph);
\r
94 String structure = null;
\r
95 String property = null;
\r
97 for(ResourceArray array : arrays) {
\r
98 for(Resource resource : array) {
\r
99 if(graph.isInstanceOf(resource, b.Literal)) {
\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
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
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
129 // if(structure != null && property != null) {
\r
130 // return structure + "/" + property;
\r
135 // if(arrays.length != 1) return null;
\r
137 // final ResourceArray parameterArray = arrays[0];
\r
138 // if(parameterArray.size() < 3) return null;
\r
140 // for(Resource r : parameterArray.resources)
\r
141 // System.out.println("ResourceArray: " + graph.adapt(r, String.class));
\r
143 // if(parameterArray.resources.length == 6) {
\r
145 // Resource unit = parameterArray.resources[0];
\r
146 // Resource module = parameterArray.resources[1];
\r
147 // Resource property = parameterArray.resources[4];
\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
153 // return unitName + "/" + moduleName + "/" + propertyName;
\r
157 // if(parameterArray.resources.length == 4) {
\r
159 // Resource stream = parameterArray.resources[0];
\r
160 // Resource property = parameterArray.resources[2];
\r
162 // String streamName = graph.getRelatedValue(stream, b.HasName, StringJavaBinding.INSTANCE);
\r
163 // String propertyName = graph.adapt(property, String.class);
\r
165 // return streamName + "/" + propertyName;
\r
169 } catch (IllegalArgumentException e) {
\r
170 e.printStackTrace();
\r
171 } catch (IOException e) {
\r
172 e.printStackTrace();
\r
180 public void create(WriteGraph graph, final RowsColumnsIndex rowIndex, final RowsColumnsIndex columnIndex, final Collection<CellCreationData> data) throws DatabaseException {
\r
182 Layer0 l0 = Layer0.getInstance(graph);
\r
183 SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);
\r
185 Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);
\r
186 Resource model = graph.getPossibleObject(spreadsheet, l0.PartOf);
\r
188 String baseUri = graph.syncRequest(Queries.uri(model));
\r
189 Pair<String, String> base = splitModelAndStructure(graph, baseUri);
\r
191 for(CellCreationData datum : data) {
\r
193 Resource newCell = graph.newResource();
\r
194 graph.claim(newCell, l0.InstanceOf, null, sr.PropertyCell2);
\r
196 String serialised = getProperty(datum, Parsers.RESOURCE_PATH_PROPERTY, Bindings.STRING);
\r
197 String label = toPath(graph, base, serialised);
\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
207 // VariableReference reference = graph.syncRequest(new PathToReference(properties, path));
\r
208 // if(reference == null) continue;
\r
209 // Resource ref = properties.create(graph, reference);
\r
211 // Resource rowResource = rowIndex.getResource(datum.row);
\r
212 // Resource columnResource = columnIndex.getResource(datum.column);
\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