1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.tests.api.write.story;
14 import java.util.Collection;
15 import java.util.HashSet;
18 import org.junit.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.request.Read;
26 import org.simantics.db.service.LifecycleSupport;
27 import org.simantics.db.testing.base.SimpleBase;
28 import org.simantics.db.testing.common.TestBase;
29 import org.simantics.db.testing.common.Tests;
30 import org.simantics.layer0.Layer0;
31 public class AddStatementTest extends SimpleBase {
32 private static int NR = 1; //1<<14;
33 private static int NS = 1<<16; // 4;
34 private final String stms = "Statements" + getRandomString();
35 private final String types = "Types" + getRandomString();
36 public AddStatementTest() {
39 public void testAddStatement() throws DatabaseException {
40 final Session s = getSession();
43 final Session s2 = Tests.getTestHandler().getSession();
47 LifecycleSupport ls = s2.getService(LifecycleSupport.class);
51 private void addStatements(Session s) throws DatabaseException {
52 s.syncRequest(new WriteRequest() {
54 public void perform(WriteGraph g) throws DatabaseException {
55 Layer0 b = Layer0.getInstance(g);
57 Resource rs = g.newResource();
58 g.claim(rs, b.InstanceOf, null, b.Library);
59 g.claimLiteral(rs, b.HasName, stms);
60 g.claim(rl, b.ConsistsOf, rs);
62 Resource rt = g.newResource();
63 g.claim(rt, b.InstanceOf, null, b.Library);
64 g.claimLiteral(rt, b.HasName, types);
65 g.claim(rl, b.ConsistsOf, rt);
69 Resource[] typesR = new Resource[NS];
70 for (int i=0; i<NS; ++i) {
71 typesR[i] = g.newResource();
72 g.claim(typesR[i], b.InstanceOf, b.Type);
73 g.claim(rt, b.ConsistsOf, typesR[i]);
76 for (int i=0; i<NR; ++i) {
77 Resource r = g.newResource();
78 g.claim(rs, b.ConsistsOf, r);
79 for (int j=0; j<NS; ++j) {
80 g.claim(r, b.InstanceOf, typesR[j]);
86 private void checkStatements(Session s) throws DatabaseException {
87 s.syncRequest(new Read<Resource>() {
89 public Resource perform(ReadGraph g) throws DatabaseException {
90 Layer0 l0 = Layer0.getInstance(g);
92 Resource rs = g.getResource(TestBase.ROOT_LIBRARY_URI + "/" + stms);
93 assertTrue(null != rs);
94 Collection<Resource> stmsR = g.getObjects(rs, l0.ConsistsOf);
95 if (stmsR.size() != NR)
96 fail("Resource count does not match. count=" + stmsR.size());
98 Resource rt = g.getResource(TestBase.ROOT_LIBRARY_URI + "/" + types);
99 assertTrue(null != rs);
100 Collection<Resource> typesR = g.getObjects(rt, l0.ConsistsOf);
101 if (typesR.size() != NS)
102 fail("Types count does not match. count=" + typesR.size());
104 Set<Resource> typesS = new HashSet<Resource>();
105 for (Resource r : typesR) {
108 for (Resource r : stmsR) {
109 Collection<Resource> instances = g.getObjects(r, l0.InstanceOf);
110 if (instances.size() != NS)
111 fail("Instance count does not match. count=" + instances.size());
112 for (Resource ri : instances)
113 if (!typesS.contains(ri))
114 fail("Instance typet does not match. instance=" + ri);