]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/rules/FlagRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / rules / FlagRule.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.typicals.rules;
13
14 import java.util.Set;
15
16 import gnu.trove.set.hash.THashSet;
17
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.diagram.flag.FlagUtil;
23 import org.simantics.diagram.stubs.DiagramResource;
24 import org.simantics.modeling.typicals.ITypicalSynchronizationRule;
25 import org.simantics.modeling.typicals.TypicalInfo;
26 import org.simantics.utils.ui.ErrorLogger;
27
28 /**
29  * Synchronizes:
30  * * joins
31  * * flag type
32  * * external tag
33  * * IO table binding
34  * * IO table row index 
35  * 
36  * @author Tuukka Lehtonen
37  */
38 public enum FlagRule implements ITypicalSynchronizationRule {
39
40     INSTANCE;
41
42     public static FlagRule getInstance() {
43         return INSTANCE;
44     }
45
46     @Override
47     public boolean synchronize(WriteGraph graph, Resource template, Resource instance, TypicalInfo info) throws DatabaseException {
48         DiagramResource DIA = DiagramResource.getInstance(graph);
49
50         boolean changed = false;
51
52         boolean result = Properties.synchronizeEnumerationPropertyValue(graph, template, instance, DIA.HasFlagType);
53         if(result) info.messageLog.add("\t\t\tflag type");
54         changed |= result;
55         result = Properties.synchronizeTag(graph, template, instance, DIA.ExternalFlag);
56         if(result) info.messageLog.add("\t\t\texternal status");
57         changed |= result;
58         result = Properties.synchronizePrimitivePropertyValue(graph, template, instance, DIA.Flag_HasIOTableBinding);
59         if(result) info.messageLog.add("\t\t\tIO table binding");
60         changed |= result;
61         result = Properties.synchronizePrimitivePropertyValue(graph, template, instance, DIA.Flag_HasIOTableRowIndex);
62         if(result) info.messageLog.add("\t\t\tIO table row");
63         changed |= result;
64         changed |= synchronizeJoins(graph, template, instance, info);
65
66         return changed;
67     }
68
69     public boolean synchronizeJoins(WriteGraph graph, Resource template, Resource instance, TypicalInfo info) throws DatabaseException {
70         boolean changed = false;
71
72         Set<Resource> tCounterparts = FlagUtil.getCounterparts(graph, template, new THashSet<Resource>(2));
73         Set<Resource> iCounterparts = FlagUtil.getCounterparts(graph, instance, new THashSet<Resource>(2));
74         if (tCounterparts.isEmpty() && iCounterparts.isEmpty())
75             return false;
76
77         for (Resource tCounterpart : tCounterparts) {
78             if (!info.bean.templateElements.contains(tCounterpart)) {
79                 // Ignore external flag reference in synchronization.
80                 // The user should not be able to create these at all.
81                 ErrorLogger.defaultLogWarning("Encountered flag " + NameUtils.getSafeName(graph, template, true)
82                         + " in a typical template diagram with an invalid flag reference to correspondence "
83                         + NameUtils.getSafeName(graph, tCounterpart, true), new Exception("trace"));
84                 continue;
85             }
86             Resource i = info.bean.templateToInstance.get(tCounterpart);
87             if (i == null) {
88                 ErrorLogger.defaultLogError(
89                         "Encountered flag " + NameUtils.getSafeName(graph, template, true)
90                                 + " in a typical template diagram while the instance diagram flag "
91                                 + NameUtils.getSafeName(graph, instance, true)
92                                 + " does not contain an element mapped to template flag correspondence "
93                                 + NameUtils.getSafeName(graph, tCounterpart, true), new Exception("trace"));
94                 continue;
95             }
96             if (iCounterparts.remove(i))
97                 // instance has the similar counterpart as the template
98                 continue;
99
100             // Instance does not have the same counterpart as the template.
101             // Create a new join.
102             FlagUtil.join(graph, instance, i);
103                         info.messageLog.add("\t\t\tsynchronized join " + NameUtils.getSafeName(graph, i));
104             changed = true;
105         }
106
107         // iCounterparts contains the set of joins that should be severed for
108         // the instance flag.
109         for (Resource iCounterpart : iCounterparts) {
110             FlagUtil.disconnectFlag(graph, instance, iCounterpart);
111                         info.messageLog.add("\t\t\tdisconnected flag " + NameUtils.getSafeName(graph, iCounterpart));
112             changed = true;
113         }
114
115         return changed;
116     }
117
118 }