From 3e6b970912a5b3a61fc594c5464f1e2582ed0b3c Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Mon, 11 Sep 2017 11:49:03 +0300 Subject: [PATCH] Added ListUtils.create alternatives that work with WriteOnlyGraph This allows those functions to work with DelayedWrites as well. refs #7477 Change-Id: I88a9de053783f05db85ecad8d1c6290e4ebc8258 --- .../simantics/db/common/utils/ListUtils.java | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) 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 4d6518524..3631de4b3 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; @@ -84,7 +85,19 @@ 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); + 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. */ @@ -92,33 +105,41 @@ 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); + 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; + 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) { 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, elementPredicate, 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. */ @@ -308,14 +329,13 @@ public class ListUtils { 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; + 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); + return g.getSingleObject(element, L0.IsOwnedBy); } - - + } -- 2.43.2