X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftypicals%2Frules%2FProperties.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftypicals%2Frules%2FProperties.java;h=e0d467d01497050475728eb3522d69f1fb7e9cda;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/rules/Properties.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/rules/Properties.java
new file mode 100644
index 000000000..e0d467d01
--- /dev/null
+++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/rules/Properties.java
@@ -0,0 +1,189 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.typicals.rules;
+
+import java.util.Collections;
+import java.util.Set;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.util.ObjectUtils;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class Properties {
+
+ /**
+ * Synchronizes a simple primitive-valued property from the specified
+ * resource to another.
+ *
+ * @param graph write access
+ * @param from property source
+ * @param to property target
+ * @param property the property to synchronize
+ * @return true
if changes were made
+ * @throws DatabaseException
+ */
+ public static boolean synchronizePrimitivePropertyValue(WriteGraph graph, Resource from, Resource to, Resource property) throws DatabaseException {
+ Statement templateStm = graph.getPossibleStatement(from, property);
+ Statement instanceStm = graph.getPossibleStatement(to, property);
+ if (templateStm == null && instanceStm == null)
+ return false;
+
+ if (templateStm == null) {
+ graph.deny(instanceStm);
+ RemoverUtil.remove(graph, instanceStm.getObject());
+ return true;
+ } else if (instanceStm == null) {
+ Resource copiedLabel = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
+ graph.claim(to, property, copiedLabel);
+ return true;
+ }
+
+ boolean templateAsserted = templateStm.isAsserted(from);
+ boolean instanceAsserted = instanceStm.isAsserted(to);
+ if (templateAsserted && instanceAsserted)
+ return false;
+
+ if (templateAsserted) {
+ // Remove instance label to let assertions work.
+ graph.denyValue(to, property);
+ } else if (instanceAsserted) {
+ // Instance is missing defined label property
+ Resource copiedLabel = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
+ graph.claim(to, property, copiedLabel);
+ } else {
+ // Make sure that both literals are of the same type.
+ Set templateTypes = graph.getTypes(templateStm.getObject());
+ Set instanceTypes = graph.getTypes(instanceStm.getObject());
+ if (Collections.disjoint(templateTypes, instanceTypes)) {
+ graph.denyValue(to, property);
+ Resource copiedValue = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
+ graph.claim(to, property, copiedValue);
+ } else {
+ Datatype dt = graph.getDataType(templateStm.getObject());
+ Binding binding = Bindings.getBinding(dt);
+ Object templateValue = graph.getPossibleValue(templateStm.getObject(), binding);
+ Object instanceValue = graph.getPossibleValue(instanceStm.getObject(), binding);
+ if (ObjectUtils.objectEquals(templateValue, instanceValue))
+ return false;
+ if (templateValue == null) {
+ graph.denyValue(instanceStm.getObject());
+ } else {
+ graph.claimValue(instanceStm.getObject(), templateValue, binding);
+ }
+ }
+ }
+ return true;
+ }
+
+ public static boolean synchronizeDeepPropertyValue(WriteGraph graph, Resource from, Resource to, Resource property) throws DatabaseException {
+
+ Statement templateStm = graph.getPossibleStatement(from, property);
+ Statement instanceStm = graph.getPossibleStatement(to, property);
+ if (templateStm == null || instanceStm == null)
+ return false;
+
+ boolean templateAsserted = templateStm.isAsserted(from);
+ boolean instanceAsserted = instanceStm.isAsserted(to);
+ if (templateAsserted && instanceAsserted)
+ return false;
+
+ if (templateAsserted) {
+ graph.deny(instanceStm);
+ RemoverUtil.remove(graph, instanceStm.getObject());
+ return true;
+ }
+
+ if (instanceAsserted) {
+ Resource copied = CopyAdvisorUtil.copy2(graph, templateStm.getObject(), null);
+ graph.claim(to, property, copied);
+ return true;
+ }
+
+ return Layer0Utils.merge(graph, templateStm.getObject(), instanceStm.getObject());
+
+ }
+
+ /**
+ * @param graph
+ * @param from
+ * @param to
+ * @param property
+ * @return
+ * @throws DatabaseException
+ */
+ public static boolean synchronizeEnumerationPropertyValue(WriteGraph graph, Resource from, Resource to, Resource property) throws DatabaseException {
+ Statement templateStm = graph.getPossibleStatement(from, property);
+ Statement instanceStm = graph.getPossibleStatement(to, property);
+ if (templateStm == null && instanceStm == null)
+ return false;
+
+ if (templateStm == null) {
+ graph.deny(instanceStm);
+ return true;
+ } else if (instanceStm == null) {
+ graph.claim(to, property, templateStm.getObject());
+ return true;
+ }
+
+ boolean templateAsserted = templateStm.isAsserted(from);
+ boolean instanceAsserted = instanceStm.isAsserted(to);
+ if (templateAsserted && instanceAsserted)
+ return false;
+
+ if (templateAsserted) {
+ // Remove instance label to let assertions work.
+ graph.denyValue(to, property);
+ } else if (instanceAsserted) {
+ // Instance is missing defined property
+ graph.claim(to, property, templateStm.getObject());
+ } else {
+ // Make sure that both literals are of the same type.
+ if (templateStm.getObject().equals(instanceStm.getObject()))
+ return false;
+ graph.deny(instanceStm);
+ graph.claim(to, property, templateStm.getObject());
+ }
+ return true;
+ }
+
+ /**
+ * @param graph
+ * @param from
+ * @param to
+ * @param property
+ * @return
+ * @throws DatabaseException
+ */
+ public static boolean synchronizeTag(WriteGraph graph, Resource from, Resource to, Resource tag) throws DatabaseException {
+ boolean fromHas = graph.hasStatement(from, tag);
+ boolean toHas = graph.hasStatement(to, tag);
+ if (fromHas == toHas)
+ return false;
+ if (fromHas)
+ graph.claim(to, tag, to);
+ else
+ graph.deny(to, tag);
+ return true;
+ }
+
+}