/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.spreadsheet.graph.creator; import java.io.IOException; import java.util.Collection; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.ResourceArray; import org.simantics.db.common.request.Queries; import org.simantics.db.exception.DatabaseException; import org.simantics.db.service.SerialisationSupport; import org.simantics.layer0.Layer0; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.spreadsheet.ModelCellManager.CellCreationData; import org.simantics.spreadsheet.common.cell.Parsers; import org.simantics.spreadsheet.graph.request.RowsColumnsIndex; import org.simantics.spreadsheet.resource.SpreadsheetResource; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.ui.dnd.ResourceTransferUtils; import org.simantics.utils.datastructures.Pair; public class ResourceArrayCellCreator extends CellCreatorBase { public ResourceArrayCellCreator(Resource realization) { super(realization); } private Pair splitModelAndStructure(ReadGraph graph, String path, int startPosition) throws DatabaseException { int position = path.indexOf("/", startPosition); if(position == -1) return null; String prefix = path.substring(0, position); // System.out.println("prefix=" + prefix); Resource r = graph.getResource(prefix); if(graph.isInstanceOf(r, SimulationResource.getInstance(graph).Model)) { return new Pair(path.substring(0, position), path.substring(position+1, path.length())); } else { return splitModelAndStructure(graph, path, position + 1); } } private Pair splitModelAndStructure(ReadGraph graph, String path) throws DatabaseException { return splitModelAndStructure(graph, path, 8); } String match(Pair base, String value) { if(!value.startsWith(base.first)) return null; System.out.println("match " + base + " - " + value); // String[] baseTokens = base.split("/"); // String[] valueTokens = value.split("/"); // // int valueIndex = 0; // // for(int baseIndex = 0; baseIndex < baseTokens.length;) { // base. // if() // } // // Diagram/ // ConfigurationValues/Diagram/Valve1/FSET/Flowrate return null; } String toPath(ReadGraph graph, Pair base, String serialisation) throws DatabaseException { Layer0 b = Layer0.getInstance(graph); SerialisationSupport support = graph.getService(SerialisationSupport.class); System.out.println("baseUri=" + base); try { ResourceArray[] arrays = ResourceTransferUtils.readStringTransferable( support.getResourceSerializer(), serialisation).toResourceArrayArray(); StructuralResource2 sr = StructuralResource2.getInstance(graph); String structure = null; String property = null; for(ResourceArray array : arrays) { for(Resource resource : array) { if(graph.isInstanceOf(resource, b.Literal)) { String uri_ = graph.syncRequest(Queries.uri(resource)); System.out.println("value uri=" + uri_); String match = match(base, uri_); if(match != null) return match; } // Set types = graph.getTypes(resource); // if(types.contains(sr.Component) && !types.contains(sr.Composite)) { // String uri = graph.syncRequest(Queries.uri(resource)); // if(uri != null) { // Pair modelAndStructure = splitModelAndStructure(graph, uri); // if(modelAndStructure != null) { //// System.out.println("model=" + modelAndStructure.first); //// System.out.println("structure=" + modelAndStructure.second); // structure = modelAndStructure.second; // } // } // } // if(types.contains(b.Relation)) { // String name = URIStringUtils.escape(graph.adapt(resource, String.class)); //// System.out.println("property=" + name); // property = name; // } } } // if(structure != null && property != null) { // return structure + "/" + property; // } else { // return null; // } // if(arrays.length != 1) return null; // final ResourceArray parameterArray = arrays[0]; // if(parameterArray.size() < 3) return null; // for(Resource r : parameterArray.resources) // System.out.println("ResourceArray: " + graph.adapt(r, String.class)); // if(parameterArray.resources.length == 6) { // // Resource unit = parameterArray.resources[0]; // Resource module = parameterArray.resources[1]; // Resource property = parameterArray.resources[4]; // // String unitName = graph.adapt(unit, String.class); // String moduleName = graph.adapt(module, String.class); // String propertyName = graph.adapt(property, String.class); // // return unitName + "/" + moduleName + "/" + propertyName; // // } // // if(parameterArray.resources.length == 4) { // // Resource stream = parameterArray.resources[0]; // Resource property = parameterArray.resources[2]; // // String streamName = graph.getRelatedValue(stream, b.HasName, StringJavaBinding.INSTANCE); // String propertyName = graph.adapt(property, String.class); // // return streamName + "/" + propertyName; // // } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override public void create(WriteGraph graph, final RowsColumnsIndex rowIndex, final RowsColumnsIndex columnIndex, final Collection data) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); SpreadsheetResource sr = SpreadsheetResource.getInstance(graph); Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents); Resource model = graph.getPossibleObject(spreadsheet, l0.PartOf); String baseUri = graph.syncRequest(Queries.uri(model)); Pair base = splitModelAndStructure(graph, baseUri); for(CellCreationData datum : data) { Resource newCell = graph.newResource(); graph.claim(newCell, l0.InstanceOf, null, sr.PropertyCell2); String serialised = getProperty(datum, Parsers.RESOURCE_PATH_PROPERTY, Bindings.STRING); String label = toPath(graph, base, serialised); Resource rowResource = rowIndex.getResource(datum.row); Resource columnResource = columnIndex.getResource(datum.column); graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource); graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource); graph.addLiteral(newCell, sr.Expression, sr.ExpressionOf, l0.String, label, Bindings.STRING); graph.claim(spreadsheet, l0.ConsistsOf, l0.PartOf, newCell); // VariableReference reference = graph.syncRequest(new PathToReference(properties, path)); // if(reference == null) continue; // Resource ref = properties.create(graph, reference); // // Resource rowResource = rowIndex.getResource(datum.row); // Resource columnResource = columnIndex.getResource(datum.column); // // graph.claim(newCell, sr.HasPropertyReference, ref); // graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource); // graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource); // graph.addValue(newCell, sr.HasWidth, sr.WidthOf, b.Integer, 1, IntegerJavaBinding.INSTANCE); // graph.addValue(newCell, sr.HasHeight, sr.HeightOf, b.Integer, 1, IntegerJavaBinding.INSTANCE); // graph.claim(spreadsheet, b.ConsistsOf, b.PartOf, newCell); } } }