From: Antti Villberg Date: Fri, 16 Nov 2018 13:10:03 +0000 (+0200) Subject: Utility function for claiming literals X-Git-Tag: v1.43.0~136^2~264^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F64%2F2464%2F1;p=simantics%2Fplatform.git Utility function for claiming literals gitlab #204 Change-Id: Ie1738b7400d26aa05bd889e2853362145cc87adc --- diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java index ee97fdb58..d89d71652 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java @@ -927,6 +927,27 @@ public class Layer0Utils { return stm != null && stm.isAsserted(subject); } + /* + * Works around problems in WriteGraph methods with similar signature. + * Especially handles better some cases with existing literals. + * + */ + public static void claimLiteral(WriteGraph graph, Resource r, Resource p, Resource i, Resource t, Object value, Binding binding) throws DatabaseException { + Statement stm = graph.getPossibleStatement(r, p); + if(stm != null && !stm.isAsserted(r)) { + if(graph.isInstanceOf(stm.getObject(), t)) { + // Existing statement is compatible, reuse the literal + graph.claimValue(stm.getObject(), value, binding); + return; + } else { + // Existing statement is incompatible - remove it + graph.deny(stm); + } + } + // Create new statement + graph.claimLiteral(r, p, i, t, value, binding); + } + public static void setExpression(WriteGraph graph, Variable context, String text, Resource expressionValueType) throws DatabaseException { Resource value = context.getRepresents(graph);