1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.typicals.rules;
\r
14 import java.util.Collections;
\r
15 import java.util.Set;
\r
17 import org.simantics.databoard.Bindings;
\r
18 import org.simantics.databoard.binding.Binding;
\r
19 import org.simantics.databoard.type.Datatype;
\r
20 import org.simantics.databoard.util.ObjectUtils;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.Statement;
\r
23 import org.simantics.db.WriteGraph;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.layer0.util.Layer0Utils;
\r
26 import org.simantics.db.layer0.util.RemoverUtil;
\r
27 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
\r
30 * @author Tuukka Lehtonen
\r
32 public class Properties {
\r
35 * Synchronizes a simple primitive-valued property from the specified
\r
36 * resource to another.
\r
38 * @param graph write access
\r
39 * @param from property source
\r
40 * @param to property target
\r
41 * @param property the property to synchronize
\r
42 * @return <code>true</code> if changes were made
\r
43 * @throws DatabaseException
\r
45 public static boolean synchronizePrimitivePropertyValue(WriteGraph graph, Resource from, Resource to, Resource property) throws DatabaseException {
\r
46 Statement templateStm = graph.getPossibleStatement(from, property);
\r
47 Statement instanceStm = graph.getPossibleStatement(to, property);
\r
48 if (templateStm == null && instanceStm == null)
\r
51 if (templateStm == null) {
\r
52 graph.deny(instanceStm);
\r
53 RemoverUtil.remove(graph, instanceStm.getObject());
\r
55 } else if (instanceStm == null) {
\r
56 Resource copiedLabel = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
\r
57 graph.claim(to, property, copiedLabel);
\r
61 boolean templateAsserted = templateStm.isAsserted(from);
\r
62 boolean instanceAsserted = instanceStm.isAsserted(to);
\r
63 if (templateAsserted && instanceAsserted)
\r
66 if (templateAsserted) {
\r
67 // Remove instance label to let assertions work.
\r
68 graph.denyValue(to, property);
\r
69 } else if (instanceAsserted) {
\r
70 // Instance is missing defined label property
\r
71 Resource copiedLabel = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
\r
72 graph.claim(to, property, copiedLabel);
\r
74 // Make sure that both literals are of the same type.
\r
75 Set<Resource> templateTypes = graph.getTypes(templateStm.getObject());
\r
76 Set<Resource> instanceTypes = graph.getTypes(instanceStm.getObject());
\r
77 if (Collections.disjoint(templateTypes, instanceTypes)) {
\r
78 graph.denyValue(to, property);
\r
79 Resource copiedValue = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
\r
80 graph.claim(to, property, copiedValue);
\r
82 Datatype dt = graph.getDataType(templateStm.getObject());
\r
83 Binding binding = Bindings.getBinding(dt);
\r
84 Object templateValue = graph.getPossibleValue(templateStm.getObject(), binding);
\r
85 Object instanceValue = graph.getPossibleValue(instanceStm.getObject(), binding);
\r
86 if (ObjectUtils.objectEquals(templateValue, instanceValue))
\r
88 if (templateValue == null) {
\r
89 graph.denyValue(instanceStm.getObject());
\r
91 graph.claimValue(instanceStm.getObject(), templateValue, binding);
\r
98 public static boolean synchronizeDeepPropertyValue(WriteGraph graph, Resource from, Resource to, Resource property) throws DatabaseException {
\r
100 Statement templateStm = graph.getPossibleStatement(from, property);
\r
101 Statement instanceStm = graph.getPossibleStatement(to, property);
\r
102 if (templateStm == null || instanceStm == null)
\r
105 boolean templateAsserted = templateStm.isAsserted(from);
\r
106 boolean instanceAsserted = instanceStm.isAsserted(to);
\r
107 if (templateAsserted && instanceAsserted)
\r
110 if (templateAsserted) {
\r
111 graph.deny(instanceStm);
\r
112 RemoverUtil.remove(graph, instanceStm.getObject());
\r
116 if (instanceAsserted) {
\r
117 Resource copied = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
\r
118 graph.claim(to, property, copied);
\r
122 return Layer0Utils.merge(graph, templateStm.getObject(), instanceStm.getObject());
\r
132 * @throws DatabaseException
\r
134 public static boolean synchronizeEnumerationPropertyValue(WriteGraph graph, Resource from, Resource to, Resource property) throws DatabaseException {
\r
135 Statement templateStm = graph.getPossibleStatement(from, property);
\r
136 Statement instanceStm = graph.getPossibleStatement(to, property);
\r
137 if (templateStm == null && instanceStm == null)
\r
140 if (templateStm == null) {
\r
141 graph.deny(instanceStm);
\r
143 } else if (instanceStm == null) {
\r
144 graph.claim(to, property, templateStm.getObject());
\r
148 boolean templateAsserted = templateStm.isAsserted(from);
\r
149 boolean instanceAsserted = instanceStm.isAsserted(to);
\r
150 if (templateAsserted && instanceAsserted)
\r
153 if (templateAsserted) {
\r
154 // Remove instance label to let assertions work.
\r
155 graph.denyValue(to, property);
\r
156 } else if (instanceAsserted) {
\r
157 // Instance is missing defined property
\r
158 graph.claim(to, property, templateStm.getObject());
\r
160 // Make sure that both literals are of the same type.
\r
161 if (templateStm.getObject().equals(instanceStm.getObject()))
\r
163 graph.deny(instanceStm);
\r
164 graph.claim(to, property, templateStm.getObject());
\r
175 * @throws DatabaseException
\r
177 public static boolean synchronizeTag(WriteGraph graph, Resource from, Resource to, Resource tag) throws DatabaseException {
\r
178 boolean fromHas = graph.hasStatement(from, tag);
\r
179 boolean toHas = graph.hasStatement(to, tag);
\r
180 if (fromHas == toHas)
\r
183 graph.claim(to, tag, to);
\r
185 graph.deny(to, tag);
\r