package org.simantics.db.common.utils;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import java.util.Set;
insertBetween(g, L0, list, list, list, elements);
return list;
}
-
+
+ public static Resource create(WriteOnlyGraph g, Resource type, Resource elementPredicate, Resource elementPredicateInverse, Iterable<Resource> 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.
*/
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<Resource> elements) throws DatabaseException {
- createExisting(g, list, false, elements);
+ 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;
+ 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<Resource> 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.
*/
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);
}
-
-
+
}