X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FListUtils.java;h=656dfeb27550b8acfe8e1f1c8801cbebefe6e879;hp=8be698cf121dc124d4f85f5dc9c5474d54014291;hb=b28a9d30e47693246404bac50c7e031c1146c273;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 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..656dfeb27 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 @@ -1,6 +1,7 @@ package org.simantics.db.common.utils; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Set; @@ -21,12 +22,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,32 +38,33 @@ 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); } /** - * Creates a list containing the givens {@code elements}. + * Creates a list containing the given {@code elements}. */ public static Resource create(WriteGraph g, Iterable elements) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(g); - - Resource list = g.newResource(); - g.claim(list, L0.InstanceOf, L0.List); - - insertBetween(g, L0, list, list, list, elements); - return list; + Layer0 L0 = Layer0.getInstance(g); + return ListUtils.create(g,L0.List, L0.List_Element, null, elements); } - + + public static Resource createWithInverses(WriteGraph g, Iterable elements) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(g); + return ListUtils.create(g,L0.ListWithInverses, L0.List_ElementWithInverse, L0.List_ElementWithInverse_Inverse, elements); + } + /** - * Creates a list containing the givens {@code elements}. + * Creates a list of the given list type containing the given {@code elements}. */ public static Resource create(WriteGraph g, Resource type, Iterable elements) throws DatabaseException { Layer0 L0 = Layer0.getInstance(g); @@ -82,7 +85,21 @@ public class ListUtils { insertBetween(g, L0, list, list, list, elements); return list; } - + + public static Resource create(WriteOnlyGraph g, Resource type, Resource elementPredicate, Resource elementPredicateInverse, Iterable elements) throws DatabaseException { + Layer0 L0 = g.getService(Layer0.class); + Resource list = g.newResource(); + g.claim(list, L0.InstanceOf, null, type); + if (!elementPredicate.equals(L0.List_Element)) + g.claim(list, L0.List_ElementPredicate, L0.List_ElementPredicate_Inverse, elementPredicate); + createExisting(g, list, elementPredicate, elementPredicateInverse, elements); + return list; + } + + public static Resource create(WriteOnlyGraph g, Resource type, Resource elementPredicate, Resource elementPredicateInverse, Resource... elements) throws DatabaseException { + return create(g, type, elementPredicate, elementPredicateInverse, Arrays.asList(elements)); + } + /** * Inserts given {@code elements} into the front of the list. */ @@ -90,14 +107,24 @@ public class ListUtils { if (!elements.iterator().hasNext()) return; Layer0 L0 = Layer0.getInstance(g); - - Resource first = g.getSingleObject(list, L0.List_Next); - g.deny(list, L0.List_Next, L0.List_Previous, first); + + Resource first = g.getSingleObject(list, L0.List_Next); + g.deny(list, L0.List_Next, L0.List_Previous, first); insertBetween(g, L0, list, list, first, elements); } - + 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 elementPredicate = withInverses ? L0.List_ElementWithInverse : L0.List_Element; + Resource elementPredicateInverse = withInverses ? L0.List_ElementWithInverse_Inverse : null; + createExisting(g, list, elementPredicate, elementPredicateInverse, elements); + } + + public static void createExisting(WriteOnlyGraph g, Resource list, Resource elementPredicate, Resource elementPredicateInverse, Iterable elements) throws DatabaseException { Layer0 L0 = g.getService(Layer0.class); Resource before = list; for(Resource item : elements) { @@ -105,13 +132,16 @@ public class ListUtils { 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, elementPredicateInverse, item); before = cur; } g.claim(before, L0.List_Next, L0.List_Previous, list); - } - + + public static void createExisting(WriteOnlyGraph g, Resource list, Resource elementPredicate, Resource elementPredicateInverse, Resource... elements) throws DatabaseException { + createExisting(g, list, elementPredicate, elementPredicateInverse, Arrays.asList(elements)); + } + /** * Inserts given {@code elements} into the back of the list. */ @@ -142,8 +172,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 +222,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 +244,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 +295,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 +307,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 +319,25 @@ 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); } - }