1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.typicals.rules;
16 import gnu.trove.set.hash.THashSet;
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;
34 * * IO table row index
36 * @author Tuukka Lehtonen
38 public enum FlagRule implements ITypicalSynchronizationRule {
42 public static FlagRule getInstance() {
47 public boolean synchronize(WriteGraph graph, Resource template, Resource instance, TypicalInfo info) throws DatabaseException {
48 DiagramResource DIA = DiagramResource.getInstance(graph);
50 boolean changed = false;
52 boolean result = Properties.synchronizeEnumerationPropertyValue(graph, template, instance, DIA.HasFlagType);
53 if(result) info.messageLog.add("\t\t\tflag type");
55 result = Properties.synchronizeTag(graph, template, instance, DIA.ExternalFlag);
56 if(result) info.messageLog.add("\t\t\texternal status");
58 result = Properties.synchronizePrimitivePropertyValue(graph, template, instance, DIA.Flag_HasIOTableBinding);
59 if(result) info.messageLog.add("\t\t\tIO table binding");
61 result = Properties.synchronizePrimitivePropertyValue(graph, template, instance, DIA.Flag_HasIOTableRowIndex);
62 if(result) info.messageLog.add("\t\t\tIO table row");
64 changed |= synchronizeJoins(graph, template, instance, info);
69 public boolean synchronizeJoins(WriteGraph graph, Resource template, Resource instance, TypicalInfo info) throws DatabaseException {
70 boolean changed = false;
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())
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"));
86 Resource i = info.bean.templateToInstance.get(tCounterpart);
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"));
96 if (iCounterparts.remove(i))
97 // instance has the similar counterpart as the template
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));
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));