From: Antti Villberg Date: Mon, 31 Jul 2017 05:25:41 +0000 (+0300) Subject: Configurability for Layer0 List modelling X-Git-Tag: v1.31.0~264^2~22 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=c818c98e04cc069eb25c7842e5e099907380f0bc Configurability for Layer0 List modelling refs #7390 Change-Id: Iaa0409642364c5e75de4b1bdac1efb281150a06a --- diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java index 8be698cf1..15910147a 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java @@ -21,12 +21,13 @@ public class ListUtils { WriteGraph g, Layer0 L0, Resource list, Resource before, Resource after, Iterable 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 elements) throws DatabaseException { + createExisting(g, list, false, elements); + } + + public static void createExisting(WriteOnlyGraph g, Resource list, boolean withInverses, Iterable 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); + } + } diff --git a/bundles/org.simantics.layer0/graph/Layer0List.pgraph b/bundles/org.simantics.layer0/graph/Layer0List.pgraph index 64d13dcf7..659a8aa22 100644 --- a/bundles/org.simantics.layer0/graph/Layer0List.pgraph +++ b/bundles/org.simantics.layer0/graph/Layer0List.pgraph @@ -7,6 +7,10 @@ L0.List -- L0.List.ElementPredicate --> L0.Relation Resource + + @JavaName create + createList :: Resource -> [Resource] -> Resource @javaName insertBack insertBack :: Resource -> [Resource] -> ()