/******************************************************************************* * 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.util.Collection; import org.simantics.databoard.Bindings; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.data.DoubleMatrix; import org.simantics.layer0.Layer0; 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; public class MatrixCellCreator extends CellCreatorBase { public MatrixCellCreator(Resource realization) { super(realization); } @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); for(CellCreationData datum : data) { String text = getProperty(datum, Parsers.LABEL_PROPERTY, Bindings.STRING); String[] size = text.replace("Mat(", "").replace(")", "").split(","); if(size.length != 2) continue; Integer rows = Integer.parseInt(size[0]); Integer columns = Integer.parseInt(size[1]); DoubleMatrix matrix = new DoubleMatrix(rows, columns); Resource newCell = graph.newResource(); graph.claim(newCell, l0.InstanceOf, null, sr.MatrixCell); 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.Value, sr.ValueOf, l0.Value, matrix, Bindings.getBindingUnchecked(DoubleMatrix.class)); graph.claim(spreadsheet, l0.ConsistsOf, l0.PartOf, newCell); } } }