]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
8e6158630732a84a8ce30e1ef49d2ce30c6be4e3
[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 java.util.Map;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.Statement;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.utils.NameUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.db.exception.ServiceException;\r
24 import org.simantics.diagram.synchronization.ISynchronizationContext;\r
25 import org.simantics.diagram.synchronization.StatementEvaluation;\r
26 import org.simantics.diagram.synchronization.SynchronizationHints;\r
27 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;\r
28 import org.simantics.layer0.Layer0;\r
29 import org.simantics.modeling.ComponentUtils;\r
30 import org.simantics.modeling.mapping.ComponentCopyAdvisor;\r
31 import org.simantics.structural.stubs.StructuralResource2;\r
32 import org.simantics.sysdyn.SysdynResource;\r
33 import org.simantics.utils.datastructures.BinaryFunction;\r
34 \r
35 public class SysdynComponentCopyAdvisor extends ComponentCopyAdvisor {\r
36 \r
37     private Layer0 L0;\r
38     private StructuralResource2 STR;\r
39     private SysdynResource SYSDYN;\r
40 \r
41     @Override\r
42     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer, Map<Object, Object> map) throws DatabaseException {\r
43 //        Object copy = super.copy(context, graph, source, sourceContainer, targetContainer, map);\r
44         \r
45       BinaryFunction<StatementEvaluation, ReadGraph, Statement> tester = new BinaryFunction<StatementEvaluation, ReadGraph, Statement>() {\r
46           @Override\r
47           public StatementEvaluation call(ReadGraph graph, Statement statement) {\r
48               try {\r
49                   if(statement.getPredicate().equals(L0.List_Next) ||\r
50                           statement.getPredicate().equals(L0.List_Previous)) {\r
51                       return StatementEvaluation.INCLUDE_AND_FOLLOW;\r
52                   } else if(statement.getPredicate().equals(L0.List_Element)){\r
53                       if(graph.isInstanceOf(statement.getObject(), SYSDYN.Enumeration))\r
54                           return StatementEvaluation.INCLUDE;\r
55                       else\r
56                           return StatementEvaluation.INCLUDE_AND_FOLLOW;\r
57                   } else if(statement.getPredicate().equals(SYSDYN.Shadow_original))\r
58                       return StatementEvaluation.INCLUDE;\r
59               } catch (ServiceException e) {\r
60                   e.printStackTrace();\r
61               }\r
62               return StatementEvaluation.USE_DEFAULT;\r
63           }\r
64           \r
65       };\r
66         \r
67       SYSDYN = SysdynResource.getInstance(graph);\r
68       L0 = Layer0.getInstance(graph);\r
69         STR = StructuralResource2.getInstance(graph);\r
70         Resource copy = null;\r
71         if (graph.isInstanceOf(source, STR.Connection)) {\r
72             // Configuration connections are not named, can't use TG copy for\r
73             // them at the moment.\r
74             copy = CopyAdvisorUtil.copy2(graph, source, null, map);\r
75         } else {\r
76 //            Resource model = graph.syncRequest(new PossibleModel(targetContainer));\r
77 //            copy = CopyAdvisorUtil.copy4(graph, source, model);\r
78             copy = CopyAdvisorUtil.copy2(graph, source, tester, map);\r
79         }\r
80 \r
81         Layer0 L0 = Layer0.getInstance(graph);\r
82         if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))\r
83             graph.claim(targetContainer, L0.ConsistsOf, copy);\r
84 \r
85         if (context.get(SynchronizationHints.NO_RENAME) == null)\r
86 //            renameComponent(context, graph, source, copy, sourceContainer, targetContainer);\r
87             rename(context, graph, source, (Resource)copy, sourceContainer, targetContainer);\r
88         \r
89         return copy;\r
90     }\r
91     \r
92 \r
93     public String rename(ISynchronizationContext context, WriteGraph graph, Resource source,\r
94             Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
95         Layer0 l0 = Layer0.getInstance(graph);\r
96         String copyName = NameUtils.getSafeName(graph, copy);\r
97         Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);\r
98         String name =  NameUtils.findFreshName(graph, copyName, configurationRoot, l0.ConsistsOf, "%s%d");\r
99         graph.claimLiteral(copy, l0.HasName, name, Bindings.STRING);\r
100         return name;\r
101     }\r
102 }\r