1 package org.simantics.db.tests.common;
3 import java.util.Collection;
4 import java.util.ListIterator;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.ReadRequest;
10 import org.simantics.db.common.request.WriteRequest;
11 import org.simantics.db.common.utils.OrderedSetUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.exception.ValidationException;
14 import org.simantics.db.impl.ClusterSupport;
15 import org.simantics.db.impl.ResourceImpl;
16 import org.simantics.db.testing.common.Client;
17 import org.simantics.db.testing.common.TestBase;
18 import org.simantics.layer0.Layer0;
20 public class ClientOperations {
22 public static boolean DEBUG = false;
24 public static String createData(Client client)
25 throws DatabaseException {
26 final String name = client.getInstanceName();
27 client.getSession().syncRequest(new WriteRequest() {
29 public void perform(WriteGraph g) throws DatabaseException {
30 Layer0 b = Layer0.getInstance(g);
31 Resource newResource = g.newResource();
32 g.claim(newResource, b.InstanceOf, null, b.Type);
33 g.claimLiteral(newResource, b.HasName, name);
34 g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
39 public static void validateData(Client client, final String name)
40 throws DatabaseException {
41 client.getSession().syncRequest(new ReadRequest() {
43 public void run(ReadGraph g) throws DatabaseException {
44 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
45 Layer0 l0 = Layer0.getInstance(g);
46 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
47 Resource newResource = null;
48 for (Resource r : resources) {
49 String value = g.getPossibleRelatedValue(r, l0.HasName);
50 if (null != value && value.equals(name)) {
55 if (newResource == null) {
56 throw new ValidationException("Could not find resource " + name);
58 if (!g.isInstanceOf(newResource, l0.Type))
59 throw new ValidationException("Created resource is not an instance of Type");
63 public static void removeData(Client client, final String name)
64 throws DatabaseException {
66 client.getSession().syncRequest(new WriteRequest() {
68 public void perform(WriteGraph g) throws DatabaseException {
69 Layer0 b = Layer0.getInstance(g);
70 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
71 Resource newResource = null;
72 for (Resource r : resources) {
73 String value = g.getPossibleRelatedValue(r, b.HasName);
74 if (null != value && value.equals(name)) {
76 g.deny(g.getRootLibrary(), b.ConsistsOf, newResource);
80 if (newResource == null) {
81 throw new ValidationException("Could not find resource " + name);
86 public static void validateDataRemoved(Client client, final String name)
87 throws DatabaseException {
88 client.getSession().syncRequest(new ReadRequest() {
90 public void run(ReadGraph g) throws DatabaseException {
91 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
92 Layer0 l0 = Layer0.getInstance(g);
93 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
94 Resource newResource = null;
95 for (Resource r : resources) {
96 String value = g.getPossibleRelatedValue(r, l0.HasName);
97 if (null != value && value.equals(name)) {
102 if (newResource != null) {
103 throw new ValidationException("Could find resource " + name);
108 public static String createOrderedSet(Client client, final int size)
109 throws DatabaseException {
110 final String name = client.getInstanceName();
111 client.getSession().syncRequest(new WriteRequest() {
113 public void perform(WriteGraph g) throws DatabaseException {
114 Layer0 b = Layer0.getInstance(g);
115 Resource l = OrderedSetUtils.create(g, b.Type);
116 g.claimLiteral(l, b.HasName, name);
117 g.claim(g.getRootLibrary(), b.ConsistsOf, l);
118 for (int i=0; i<size; ++i) {
119 Resource el = g.newResource();
120 g.claim(el, b.InstanceOf, null, b.Type);
121 OrderedSetUtils.add(g, l, el);
123 g.claim(g.getRootLibrary(), b.ConsistsOf, l);
128 public static void validateOrderedSet(final Client client, final String name, final int size)
129 throws DatabaseException {
130 client.getSession().syncRequest(new ReadRequest() {
132 public void run(ReadGraph g) throws DatabaseException {
133 Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI);
134 Layer0 l0 = Layer0.getInstance(g);
135 Collection<Resource> resources = g.getObjects(rl, l0.ConsistsOf);
136 Resource newResource = null;
137 for (Resource r : resources) {
138 String value = g.getPossibleRelatedValue(r, l0.HasName);
139 if (null != value && value.equals(name)) {
144 if (newResource == null) {
145 throw new ValidationException("Could not find resource " + name);
147 if (!g.isInstanceOf(newResource, l0.Type))
148 throw new ValidationException("Created resource is not an instance of Type");
149 ListIterator<Resource> a = OrderedSetUtils.iterator(g, newResource);
151 while (a.hasNext()) {
152 Resource r = a.next();
154 System.out.println("DEBUG: Resource " + r);
158 throw new ValidationException("Number of elements doesn't match!");
162 public static void removeElement(Client client, final String name, final int size)
163 throws DatabaseException {
164 client.getSession().syncRequest(new WriteRequest() {
166 public void perform(WriteGraph g) throws DatabaseException {
167 Layer0 b = Layer0.getInstance(g);
168 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
169 Resource newResource = null;
170 for (Resource r : resources) {
171 String value = g.getPossibleRelatedValue(r, b.HasName);
172 if (null != value && value.equals(name)) {
177 if (newResource == null) {
178 throw new ValidationException("Could not find resource " + name);
180 if (!g.isInstanceOf(newResource, b.Type))
181 throw new ValidationException("Created resource is not an instance of Type");
182 ListIterator<Resource> a = OrderedSetUtils.iterator(g, newResource);
184 while (a.hasNext()) {
190 throw new ValidationException("Number of elements doesn't match!");
193 OrderedSetUtils.remove(g, newResource, el);
194 a = OrderedSetUtils.iterator(g, newResource);
196 while (a.hasNext()) {
197 Resource r = a.next();
199 System.out.println("DEBUG: Resource " + r);
203 throw new ValidationException("Number of elements doesn't match!");
207 public static void adddElement(Client client, final String name, final int size)
208 throws DatabaseException {
209 client.getSession().syncRequest(new WriteRequest() {
211 public void perform(WriteGraph g) throws DatabaseException {
212 Layer0 b = Layer0.getInstance(g);
213 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
214 Resource newResource = null;
215 for (Resource r : resources) {
216 String value = g.getPossibleRelatedValue(r, b.HasName);
217 if (null != value && value.equals(name)) {
222 if (newResource == null) {
223 throw new ValidationException("Could not find resource " + name);
225 if (!g.isInstanceOf(newResource, b.Type))
226 throw new ValidationException("Created resource is not an instance of Type");
227 for (int i=0; i<size; ++i) {
228 Resource el = g.newResource();
229 g.claim(el, b.InstanceOf, null, b.Type);
230 OrderedSetUtils.add(g, newResource, el);