--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.api.story.misc;
+
+import gnu.trove.set.hash.THashSet;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+import org.junit.Test;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.testing.annotation.Fails;
+import org.simantics.db.testing.base.ExistingDatabaseTest;
+import org.simantics.db.tests.common.Configuration;
+import org.simantics.layer0.Layer0;
+import org.simantics.project.ontology.ProjectResource;
+
+public class SyntheticModellingTest extends ExistingDatabaseTest {
+
+ static int LOOP_COUNT = Configuration.get().modellingLoopCount;
+ static int OP_COUNT = Configuration.get().modellingOpCount;
+ Session session;
+ Resource root;
+ Layer0 b;
+
+ Resource Component;
+ Resource Composite;
+ Resource IsDefinedBy;
+ Resource Connection;
+ Resource IsConnected;
+ Resource IsConnectedTo;
+ Resource IsConnectedFrom;
+
+ int componentTypeCount = 0;
+ int componentCount = 0;
+ int connectionCount = 0;
+
+ static class Component {
+ Resource resource;
+ ComponentType type;
+ }
+
+ static class ComponentType {
+ Resource resource;
+ Resource composite;
+ ArrayList<Component> components = new ArrayList<Component>();
+ ArrayList<Resource> connections = new ArrayList<Resource>();
+ }
+
+ ArrayList<ComponentType> componentTypes = new ArrayList<ComponentType>();
+
+ Random random = new Random(0x12345679);
+
+// @Override
+// protected void setUp() throws Exception {
+// SessionFactory factory = new SessionFactory(Configuration.get().host, Configuration.get().port);
+// session = factory.create();
+// }
+//
+// @Override
+// protected void tearDown() throws Exception {
+// LifecycleSupport support = session.getService(LifecycleSupport.class);
+// support.close();
+// }
+
+ class SetUp extends WriteRequest {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+
+ b = Layer0.getInstance(g);
+
+ ProjectResource PROJ = ProjectResource.getInstance(g);
+
+ root = g.newResource();
+ g.claim(root, b.InstanceOf, PROJ.Project);
+
+ Component = g.getResource("http://www.simantics.org/Structural-1.0/Component");
+ Composite = g.getResource("http://www.simantics.org/Structural-1.0/Composite");
+ IsDefinedBy = g.getResource("http://www.simantics.org/Structural-1.0/IsDefinedBy");
+ Connection = g.getResource("http://www.simantics.org/Structural-1.0/Connection");
+ IsConnected = g.getResource("http://www.simantics.org/Structural-1.0/IsConnected");
+ IsConnectedTo = g.getResource("http://www.simantics.org/Structural-1.0/IsConnectedTo");
+ IsConnectedFrom = g.getResource("http://www.simantics.org/Structural-1.0/IsConnectedFrom");
+
+ }
+
+ }
+
+ class NewComponentType extends WriteRequest {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ ComponentType componentType = new ComponentType();
+
+ componentType.resource = g.newResource();
+ g.claim(componentType.resource, b.Inherits, Component);
+ g.claim(root, b.ConsistsOf, componentType.resource);
+ g.claimLiteral(componentType.resource, b.HasName, "ComponentType" + componentType.resource.getResourceId());
+
+ componentType.composite = g.newResource();
+ g.claim(componentType.composite, b.Inherits, Composite);
+ g.claim(componentType.resource, IsDefinedBy, componentType.composite);
+
+ componentTypes.add(componentType);
+
+ ++componentTypeCount;
+ }
+
+ }
+
+ class NewComponent extends WriteRequest {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ ComponentType componentType = componentTypes.get(random.nextInt(componentTypes.size()));
+ ComponentType containerType = componentTypes.get(random.nextInt(componentTypes.size()));
+
+ Component component = new Component();
+
+ component.resource = g.newResource();
+ g.claim(component.resource, b.InstanceOf, componentType.resource);
+ g.claim(containerType.composite, b.ConsistsOf, component.resource);
+
+ g.claimLiteral(component.resource, b.HasName, "Component" + componentType.resource.getResourceId());
+
+ containerType.components.add(component);
+
+ ++componentCount;
+ }
+
+ }
+
+ class RemoveComponent extends WriteRequest {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ ComponentType containerType = componentTypes.get(random.nextInt(componentTypes.size()));
+ if(containerType.components.isEmpty())
+ return;
+ int cid = random.nextInt(containerType.components.size());
+ Component component = containerType.components.get(cid);
+ THashSet<Resource> removed = new THashSet<Resource>();
+ for(Resource c : g.getObjects(component.resource, IsConnected))
+ if(removed.add(c)) {
+ g.deny(c);
+ int id = containerType.connections.indexOf(c);
+ int last = containerType.connections.size()-1;
+ if(id < last)
+ containerType.connections.set(id, containerType.connections.remove(last));
+ else
+ containerType.connections.remove(last);
+ --connectionCount;
+ }
+ g.deny(component.resource);
+ {
+ int last = containerType.components.size() - 1;
+ if(cid < last)
+ containerType.components.set(cid, containerType.components.remove(last));
+ else
+ containerType.components.remove(last);
+ }
+
+ --componentCount;
+ }
+
+ }
+
+ class NewConnection extends WriteRequest {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ ComponentType containerType = componentTypes.get(random.nextInt(componentTypes.size()));
+ if(containerType.components.isEmpty())
+ return;
+
+ Component c1 = containerType.components.get(random.nextInt(containerType.components.size()));
+ Component c2 = containerType.components.get(random.nextInt(containerType.components.size()));
+
+ Resource connection = g.newResource();
+ g.claim(connection, b.InstanceOf, Connection);
+ g.claim(c1.resource, IsConnectedTo, connection);
+ g.claim(c2.resource, IsConnectedFrom, connection);
+
+ containerType.connections.add(connection);
+
+ ++connectionCount;
+ }
+
+ }
+
+ class RemoveConnection extends WriteRequest {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ ComponentType containerType = componentTypes.get(random.nextInt(componentTypes.size()));
+ if(containerType.connections.isEmpty())
+ return;
+
+ int id = random.nextInt(containerType.connections.size());
+ g.deny(containerType.connections.get(id));
+ int last = containerType.connections.size()-1;
+ if(id < last)
+ containerType.connections.set(id, containerType.connections.remove(last));
+ else
+ containerType.connections.remove(last);
+
+ --connectionCount;
+ }
+
+ }
+
+ @Fails
+ @Test public void test() {
+ try {
+ session = getSession();
+ session.syncRequest(new SetUp());
+ for(int i=0;i<100;++i)
+ session.syncRequest(new NewComponentType());
+ for(int i=0;i<LOOP_COUNT;++i) {
+ long startTime = System.nanoTime();
+ for(int j=0;j<OP_COUNT;++j)
+ switch(random.nextInt(8)) {
+ case 0:
+ session.syncRequest(new NewComponent());
+ break;
+ case 1:
+ session.syncRequest(new RemoveComponent());
+ break;
+ case 2:
+ if(componentCount > 1000)
+ session.syncRequest(new RemoveComponent());
+ else
+ session.syncRequest(new NewComponent());
+ break;
+ case 3:
+ session.syncRequest(new NewConnection());
+ break;
+ case 4:
+ session.syncRequest(new RemoveConnection());
+ break;
+ case 5:
+ case 6:
+ case 7:
+ if(connectionCount > 1000)
+ session.syncRequest(new RemoveConnection());
+ else
+ session.syncRequest(new NewConnection());
+ break;
+ }
+ long endTime = System.nanoTime();
+ System.out.println((endTime - startTime) * 1e-9 + "s / 1000 operations");
+ System.out.println(
+ componentTypeCount + " user components, " +
+ componentCount + " components, " +
+ connectionCount + " connections"
+ );
+ }
+ } catch (DatabaseException e) {
+ if (DEBUG)
+ e.printStackTrace();
+ fail("Test failed with: " + e.getMessage());
+ }
+ }
+
+}