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