]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
5b3c122c0c7397a9df40d14feba907d7d12be91c
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.editor.participant;\r
13 \r
14 import org.simantics.databoard.Bindings;\r
15 import org.simantics.db.ReadGraph;\r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.Statement;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.utils.NameUtils;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.exception.ServiceException;\r
22 import org.simantics.diagram.synchronization.ISynchronizationContext;\r
23 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.modeling.ComponentUtils;\r
26 import org.simantics.modeling.mapping.ComponentCopyAdvisor;\r
27 import org.simantics.sysdyn.SysdynResource;\r
28 import org.simantics.utils.datastructures.BinaryFunction;\r
29 \r
30 public class SysdynComponentCopyAdvisor extends ComponentCopyAdvisor{\r
31 \r
32     @Override\r
33     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
34         \r
35         BinaryFunction<Boolean, ReadGraph, Statement> tester = new BinaryFunction<Boolean, ReadGraph, Statement>() {\r
36 \r
37                         @Override\r
38                         public Boolean call(ReadGraph graph, Statement statement) {\r
39                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
40                                 try {\r
41                                         if(graph.isInstanceOf(statement.getSubject(), sr.IndependentVariable)) {\r
42                                                 if(statement.getPredicate().equals(sr.HasExpressions))\r
43                                                         return true;\r
44                                         }\r
45                                 } catch (ServiceException e) {\r
46                                         e.printStackTrace();\r
47                                 }\r
48                                 return false;\r
49                         }\r
50                 \r
51         };\r
52         \r
53         Resource copy = CopyAdvisorUtil.copy(graph, source, tester);\r
54         \r
55         renameComponent(context, graph, source, copy, sourceContainer, targetContainer);\r
56         return copy;\r
57     }\r
58     \r
59 \r
60     public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,\r
61             Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
62         Layer0 l0 = Layer0.getInstance(graph);\r
63         String copyName = NameUtils.getSafeName(graph, copy);\r
64         Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);\r
65         String name =  NameUtils.findFreshName(graph, copyName, configurationRoot, l0.ConsistsOf, "%s%d");\r
66         graph.claimLiteral(copy, l0.HasName, name, Bindings.STRING);\r
67         return name;\r
68     }\r
69 }\r