]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java
Configurability for Layer0 List modelling
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / ListUtils.java
index 8be698cf121dc124d4f85f5dc9c5474d54014291..15910147ae4d25a50837e50b03e2245b9136e107 100644 (file)
@@ -21,12 +21,13 @@ public class ListUtils {
             WriteGraph g, Layer0 L0, Resource list,
             Resource before, Resource after, 
             Iterable<Resource> elements) throws DatabaseException {
+        Resource elementPredicate = getElementPredicate(g, list);
         for(Resource item : elements) {
             Resource cur = g.newResource();
             g.claim(cur, L0.InstanceOf, null, L0.List_Entry);
             g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list);
             g.claim(before, L0.List_Next, L0.List_Previous, cur);
-            g.claim(cur, L0.List_Element, item);
+            g.claim(cur, elementPredicate, item);
             before = cur;
         }  
         g.claim(before, L0.List_Next, L0.List_Previous, after);
@@ -36,12 +37,13 @@ public class ListUtils {
             WriteGraph g, Layer0 L0, Resource list, 
             Resource before, Resource after, 
             Resource[] elements) throws DatabaseException {
+        Resource elementPredicate = getElementPredicate(g, list);
         for(Resource item : elements) {
             Resource cur = g.newResource();
             g.claim(cur, L0.InstanceOf, null, L0.List_Entry);
             g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list);
             g.claim(before, L0.List_Next, L0.List_Previous, cur);
-            g.claim(cur, L0.List_Element, item);
+            g.claim(cur, elementPredicate, item);
             before = cur;
         }  
         g.claim(before, L0.List_Next, L0.List_Previous, after);
@@ -97,15 +99,20 @@ public class ListUtils {
     }
     
     public static void createExisting(WriteOnlyGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
+       createExisting(g, list, false, elements);
+    }
+
+    public static void createExisting(WriteOnlyGraph g, Resource list, boolean withInverses, Iterable<Resource> elements) throws DatabaseException {
     
         Layer0 L0 = g.getService(Layer0.class);
         Resource before = list;
+        Resource elementPredicate = withInverses ? L0.List_ElementWithInverse : L0.List_Element;
         for(Resource item : elements) {
             Resource cur = g.newResource();
             g.claim(cur, L0.InstanceOf, null, L0.List_Entry);
             g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list);
             g.claim(before, L0.List_Next, L0.List_Previous, cur);
-            g.claim(cur, L0.List_Element, null, item);
+            g.claim(cur, elementPredicate, null, item);
             before = cur;
         }  
         g.claim(before, L0.List_Next, L0.List_Previous, list);
@@ -142,8 +149,9 @@ public class ListUtils {
 
         Resource node = getNode(g, list, element);
         if(node != null) {
+            Resource elementPredicate = getElementPredicate(g, list);
             g.deny(node, L0.List_Element);
-            g.claim(node, L0.List_Element, replacement);
+            g.claim(node, elementPredicate, replacement);
             return true;
         } else {
             return false;
@@ -191,7 +199,7 @@ public class ListUtils {
         while(!cur.equals(list)) {
             Resource el = g.getPossibleObject(cur, L0.List_Element);
             Resource next = g.getSingleObject(cur, L0.List_Next);
-            if(element.equals(el)) {                
+            if(element.equals(el)) {
                 g.deny(cur);
                 g.claim(prev, L0.List_Next, next);
                 return true;
@@ -213,7 +221,7 @@ public class ListUtils {
         while(!cur.equals(list)) {
             Resource el = g.getPossibleObject(cur, L0.List_Element);
             Resource next = g.getSingleObject(cur, L0.List_Next);
-            if(elements.contains(el)) {                
+            if(elements.contains(el)) {
                 g.deny(cur);
                 g.claim(prev, L0.List_Next, next);
                 removed = true;
@@ -264,7 +272,7 @@ public class ListUtils {
         Resource prev = g.getSingleObject(node, L0.List_Previous);
         if(list.equals(prev))
             return false;
-        swap(g, node, prev);
+        swap(g, list, node, prev);
         return true;
     }
     
@@ -276,11 +284,11 @@ public class ListUtils {
         Resource next = g.getSingleObject(node, L0.List_Next);
         if(list.equals(next))
             return false;
-        swap(g, node, next);
+        swap(g, list, node, next);
         return true;
     }
 
-    private static void swap(WriteGraph g, Resource a, Resource b) throws DatabaseException {
+    private static void swap(WriteGraph g, Resource list, Resource a, Resource b) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(g);
         Resource ea = g.getPossibleObject(a, L0.List_Element);
         Resource eb = g.getPossibleObject(b, L0.List_Element);
@@ -288,11 +296,26 @@ public class ListUtils {
         g.deny(a, L0.List_Element);
         g.deny(b, L0.List_Element);
         
+        Resource elementPredicate = getElementPredicate(g, list);
+        
         if(eb != null)
-            g.claim(a, L0.List_Element, eb);
+            g.claim(a, elementPredicate, eb);
         if(ea != null)
-            g.claim(b, L0.List_Element, ea);
+            g.claim(b, elementPredicate, ea);
     }
 
+    public static Resource getElementPredicate(ReadGraph g, Resource list) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(g);
+        Resource predicate = g.getPossibleObject(list, L0.List_ElementPredicate);
+        if(predicate != null) return predicate;
+       return g.isInstanceOf(list, L0.ListWithInverses) ?
+                       L0.List_ElementWithInverse : L0.List_Element; 
+    }
+
+    public static Resource getListElementList(ReadGraph g, Resource element) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(g);
+       return g.getSingleObject(element, L0.IsOwnedBy);
+    }
+    
     
 }