]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/creator/MatrixCellCreator.java.keep
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / creator / MatrixCellCreator.java.keep
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.spreadsheet.graph.creator;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.layer0.data.DoubleMatrix;\r
21 import org.simantics.layer0.Layer0;\r
22 import org.simantics.spreadsheet.ModelCellManager.CellCreationData;\r
23 import org.simantics.spreadsheet.common.cell.Parsers;\r
24 import org.simantics.spreadsheet.graph.request.RowsColumnsIndex;\r
25 import org.simantics.spreadsheet.resource.SpreadsheetResource;\r
26 \r
27 public class MatrixCellCreator extends CellCreatorBase {\r
28 \r
29     public MatrixCellCreator(Resource realization) {\r
30         super(realization);\r
31     }\r
32 \r
33 \r
34     @Override\r
35     public void create(WriteGraph graph, final RowsColumnsIndex rowIndex, final RowsColumnsIndex columnIndex, final Collection<CellCreationData> data) throws DatabaseException {\r
36 \r
37         Layer0 l0 = Layer0.getInstance(graph);\r
38         SpreadsheetResource sr = SpreadsheetResource.getInstance(graph);\r
39 \r
40         Resource spreadsheet = graph.getPossibleObject(realization, l0.Represents);\r
41 \r
42         for(CellCreationData datum : data) {\r
43 \r
44             String text = getProperty(datum, Parsers.LABEL_PROPERTY, Bindings.STRING);\r
45             String[] size = text.replace("Mat(", "").replace(")", "").split(",");\r
46             if(size.length != 2) continue;\r
47             Integer rows = Integer.parseInt(size[0]);\r
48             Integer columns = Integer.parseInt(size[1]);\r
49 \r
50             DoubleMatrix matrix = new DoubleMatrix(rows, columns);\r
51 \r
52             Resource newCell = graph.newResource();\r
53             graph.claim(newCell, l0.InstanceOf, null, sr.MatrixCell);\r
54             Resource rowResource = rowIndex.getResource(datum.row);\r
55             Resource columnResource = columnIndex.getResource(datum.column);\r
56             graph.claim(newCell, sr.HasRow, sr.RowOf, rowResource);\r
57             graph.claim(newCell, sr.HasColumn, sr.ColumnOf, columnResource);\r
58             graph.addLiteral(newCell, sr.Value, sr.ValueOf, l0.Value, matrix, Bindings.getBindingUnchecked(DoubleMatrix.class));\r
59             graph.claim(spreadsheet, l0.ConsistsOf, l0.PartOf, newCell);\r
60         }\r
61 \r
62     }\r
63 \r
64 }\r