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