+/*******************************************************************************
+ * 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.write.story;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+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.request.Read;
+import org.simantics.db.service.LifecycleSupport;
+import org.simantics.db.testing.base.SimpleBase;
+import org.simantics.db.testing.common.TestBase;
+import org.simantics.db.testing.common.Tests;
+import org.simantics.layer0.Layer0;
+public class AddStatementTest extends SimpleBase {
+ private static int NR = 1; //1<<14;
+ private static int NS = 1<<16; // 4;
+ private final String stms = "Statements" + getRandomString();
+ private final String types = "Types" + getRandomString();
+ public AddStatementTest() {
+ }
+ @Test
+ public void testAddStatement() throws DatabaseException {
+ final Session s = getSession();
+ addStatements(s);
+ checkStatements(s);
+ final Session s2 = Tests.getTestHandler().getSession();
+ try {
+ checkStatements(s2);
+ } finally {
+ LifecycleSupport ls = s2.getService(LifecycleSupport.class);
+ ls.close();
+ }
+ }
+ private void addStatements(Session s) throws DatabaseException {
+ s.syncRequest(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ Layer0 b = Layer0.getInstance(g);
+
+ Resource rs = g.newResource();
+ g.claim(rs, b.InstanceOf, null, b.Library);
+ g.claimLiteral(rs, b.HasName, stms);
+ g.claim(rl, b.ConsistsOf, rs);
+
+ Resource rt = g.newResource();
+ g.claim(rt, b.InstanceOf, null, b.Library);
+ g.claimLiteral(rt, b.HasName, types);
+ g.claim(rl, b.ConsistsOf, rt);
+
+ g.flushCluster();
+
+ Resource[] typesR = new Resource[NS];
+ for (int i=0; i<NS; ++i) {
+ typesR[i] = g.newResource();
+ g.claim(typesR[i], b.InstanceOf, b.Type);
+ g.claim(rt, b.ConsistsOf, typesR[i]);
+ }
+
+ for (int i=0; i<NR; ++i) {
+ Resource r = g.newResource();
+ g.claim(rs, b.ConsistsOf, r);
+ for (int j=0; j<NS; ++j) {
+ g.claim(r, b.InstanceOf, typesR[j]);
+ }
+ }
+ }
+ });
+ }
+ private void checkStatements(Session s) throws DatabaseException {
+ s.syncRequest(new Read<Resource>() {
+ @Override
+ public Resource perform(ReadGraph g) throws DatabaseException {
+ Layer0 l0 = Layer0.getInstance(g);
+
+ Resource rs = g.getResource(TestBase.ROOT_LIBRARY_URI + "/" + stms);
+ assertTrue(null != rs);
+ Collection<Resource> stmsR = g.getObjects(rs, l0.ConsistsOf);
+ if (stmsR.size() != NR)
+ fail("Resource count does not match. count=" + stmsR.size());
+
+ Resource rt = g.getResource(TestBase.ROOT_LIBRARY_URI + "/" + types);
+ assertTrue(null != rs);
+ Collection<Resource> typesR = g.getObjects(rt, l0.ConsistsOf);
+ if (typesR.size() != NS)
+ fail("Types count does not match. count=" + typesR.size());
+
+ Set<Resource> typesS = new HashSet<Resource>();
+ for (Resource r : typesR) {
+ typesS.add(r);
+ }
+ for (Resource r : stmsR) {
+ Collection<Resource> instances = g.getObjects(r, l0.InstanceOf);
+ if (instances.size() != NS)
+ fail("Instance count does not match. count=" + instances.size());
+ for (Resource ri : instances)
+ if (!typesS.contains(ri))
+ fail("Instance typet does not match. instance=" + ri);
+ }
+ return null;
+ }
+ });
+ }
+}
\ No newline at end of file