]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/rules/Properties.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / rules / Properties.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.Collections;
15 import java.util.Set;
16
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;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class Properties {
33
34     /**
35      * Synchronizes a simple primitive-valued property from the specified
36      * resource to another.
37      * 
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
44      */
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)
49             return false;
50
51         if (templateStm == null) {
52             graph.deny(instanceStm);
53             RemoverUtil.remove(graph, instanceStm.getObject());
54             return true;
55         } else if (instanceStm == null) {
56             Resource copiedLabel = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
57             graph.claim(to, property, copiedLabel);
58             return true;
59         }
60
61         boolean templateAsserted = templateStm.isAsserted(from);
62         boolean instanceAsserted = instanceStm.isAsserted(to);
63         if (templateAsserted && instanceAsserted)
64             return false;
65
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);
73         } else {
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);
81             } else {
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))
87                     return false;
88                 if (templateValue == null) {
89                     graph.denyValue(instanceStm.getObject());
90                 } else {
91                     graph.claimValue(instanceStm.getObject(), templateValue, binding);
92                 }
93             }
94         }
95         return true;
96     }
97
98     public static boolean synchronizeDeepPropertyValue(WriteGraph graph, Resource from, Resource to, Resource property) throws DatabaseException {
99         
100         Statement templateStm = graph.getPossibleStatement(from, property);
101         Statement instanceStm = graph.getPossibleStatement(to, property);
102         if (templateStm == null || instanceStm == null)
103             return false;
104
105         boolean templateAsserted = templateStm.isAsserted(from);
106         boolean instanceAsserted = instanceStm.isAsserted(to);
107         if (templateAsserted && instanceAsserted)
108             return false;
109         
110         if (templateAsserted) {
111             graph.deny(instanceStm);
112             RemoverUtil.remove(graph, instanceStm.getObject());
113             return true;
114         }
115         
116         if (instanceAsserted) {
117             Resource copied = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
118             graph.claim(to, property, copied);
119             return true;
120         }
121
122         return Layer0Utils.merge(graph, templateStm.getObject(), instanceStm.getObject());
123         
124     }
125     
126     /**
127      * @param graph
128      * @param from
129      * @param to
130      * @param property
131      * @return
132      * @throws DatabaseException
133      */
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)
138             return false;
139
140         if (templateStm == null) {
141             graph.deny(instanceStm);
142             return true;
143         } else if (instanceStm == null) {
144             graph.claim(to, property, templateStm.getObject());
145             return true;
146         }
147
148         boolean templateAsserted = templateStm.isAsserted(from);
149         boolean instanceAsserted = instanceStm.isAsserted(to);
150         if (templateAsserted && instanceAsserted)
151             return false;
152
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());
159         } else {
160             // Make sure that both literals are of the same type.
161             if (templateStm.getObject().equals(instanceStm.getObject()))
162                 return false;
163             graph.deny(instanceStm);
164             graph.claim(to, property, templateStm.getObject());
165         }
166         return true;
167     }
168
169     /**
170      * @param graph
171      * @param from
172      * @param to
173      * @param property
174      * @return
175      * @throws DatabaseException
176      */
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)
181             return false;
182         if (fromHas)
183             graph.claim(to, tag, to);
184         else
185             graph.deny(to, tag);
186         return true;
187     }
188
189 }