Utility function for claiming literals 64/2464/1
authorAntti Villberg <antti.villberg@semantum.fi>
Fri, 16 Nov 2018 13:10:03 +0000 (15:10 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Sat, 17 Nov 2018 06:19:23 +0000 (08:19 +0200)
gitlab #204

Change-Id: Ie1738b7400d26aa05bd889e2853362145cc87adc

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java

index ee97fdb5827a723d8f1e86520d0343a81dbaee7f..d89d7165226e7522fdf0a015a7209418e5185847 100644 (file)
@@ -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);