Utility function for claiming literals
[simantics/platform.git] / 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);