package org.simantics.db.tests.common; import java.util.Collection; import java.util.ListIterator; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ValidationException; import org.simantics.db.impl.ClusterSupport; import org.simantics.db.impl.ResourceImpl; import org.simantics.db.testing.common.Client; import org.simantics.db.testing.common.TestBase; import org.simantics.layer0.Layer0; public class ClientOperations { public static boolean DEBUG = false; public static String createData(Client client) throws DatabaseException { final String name = client.getInstanceName(); client.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Resource newResource = g.newResource(); g.claim(newResource, b.InstanceOf, null, b.Type); g.claimLiteral(newResource, b.HasName, name); g.claim(g.getRootLibrary(), b.ConsistsOf, newResource); } }); return name; } public static void validateData(Client client, final String name) throws DatabaseException { client.getSession().syncRequest(new ReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI); Layer0 l0 = Layer0.getInstance(g); Collection resources = g.getObjects(rl, l0.ConsistsOf); Resource newResource = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, l0.HasName); if (null != value && value.equals(name)) { newResource = r; break; } } if (newResource == null) { throw new ValidationException("Could not find resource " + name); } if (!g.isInstanceOf(newResource, l0.Type)) throw new ValidationException("Created resource is not an instance of Type"); } }); } public static void removeData(Client client, final String name) throws DatabaseException { // ++instanceNumber; client.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Collection resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf); Resource newResource = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, b.HasName); if (null != value && value.equals(name)) { newResource = r; g.deny(g.getRootLibrary(), b.ConsistsOf, newResource); break; } } if (newResource == null) { throw new ValidationException("Could not find resource " + name); } } }); } public static void validateDataRemoved(Client client, final String name) throws DatabaseException { client.getSession().syncRequest(new ReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { Resource rl = g.getResource(TestBase.ROOT_LIBRARY_URI); Layer0 l0 = Layer0.getInstance(g); Collection resources = g.getObjects(rl, l0.ConsistsOf); Resource newResource = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, l0.HasName); if (null != value && value.equals(name)) { newResource = r; break; } } if (newResource != null) { throw new ValidationException("Could find resource " + name); } } }); } public static String createOrderedSet(Client client, final int size) throws DatabaseException { final String name = client.getInstanceName(); client.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Resource l = OrderedSetUtils.create(g, b.Type); g.claimLiteral(l, b.HasName, name); g.claim(g.getRootLibrary(), b.ConsistsOf, l); for (int i=0; i resources = g.getObjects(rl, l0.ConsistsOf); Resource newResource = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, l0.HasName); if (null != value && value.equals(name)) { newResource = r; break; } } if (newResource == null) { throw new ValidationException("Could not find resource " + name); } if (!g.isInstanceOf(newResource, l0.Type)) throw new ValidationException("Created resource is not an instance of Type"); ListIterator a = OrderedSetUtils.iterator(g, newResource); int count = 0; while (a.hasNext()) { Resource r = a.next(); if (DEBUG) System.out.println("DEBUG: Resource " + r); ++count; } if (size != count) throw new ValidationException("Number of elements doesn't match!"); } }); } public static void removeElement(Client client, final String name, final int size) throws DatabaseException { client.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Collection resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf); Resource newResource = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, b.HasName); if (null != value && value.equals(name)) { newResource = r; break; } } if (newResource == null) { throw new ValidationException("Could not find resource " + name); } if (!g.isInstanceOf(newResource, b.Type)) throw new ValidationException("Created resource is not an instance of Type"); ListIterator a = OrderedSetUtils.iterator(g, newResource); Resource el = null; while (a.hasNext()) { el = a.next(); break; } if (el == null) { if (size != 0) throw new ValidationException("Number of elements doesn't match!"); return; } OrderedSetUtils.remove(g, newResource, el); a = OrderedSetUtils.iterator(g, newResource); int count = 0; while (a.hasNext()) { Resource r = a.next(); if (DEBUG) System.out.println("DEBUG: Resource " + r); ++count; } if (size != count) throw new ValidationException("Number of elements doesn't match!"); } }); } public static void adddElement(Client client, final String name, final int size) throws DatabaseException { client.getSession().syncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) throws DatabaseException { Layer0 b = Layer0.getInstance(g); Collection resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf); Resource newResource = null; for (Resource r : resources) { String value = g.getPossibleRelatedValue(r, b.HasName); if (null != value && value.equals(name)) { newResource = r; break; } } if (newResource == null) { throw new ValidationException("Could not find resource " + name); } if (!g.isInstanceOf(newResource, b.Type)) throw new ValidationException("Created resource is not an instance of Type"); for (int i=0; i