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.regression.bugs;
14 import java.util.HashMap;
17 import java.util.TreeSet;
18 import java.util.Vector;
20 import org.junit.Test;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.WriteOnlyGraph;
26 import org.simantics.db.common.request.ReadRequest;
27 import org.simantics.db.common.request.WriteOnlyRequest;
28 import org.simantics.db.common.request.WriteRequest;
29 import org.simantics.db.exception.ClusterSetExistException;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.db.impl.ClusterTraitsBase;
32 import org.simantics.db.service.ClusteringSupport;
33 import org.simantics.db.testing.base.ExistingDatabaseTest;
34 import org.simantics.layer0.Layer0;
37 * Tests that cluster set can be used to explicitly control clustering.
38 * Must be called more than once from runner to test that cluster set is persistent.
40 public class Issue3420Test2 extends ExistingDatabaseTest {
41 // final boolean DEBUG = true;
42 final boolean VERBOSE = false;
43 final int CLUSTER_FILL_SIZE = ClusterTraitsBase.getMaxNumberOfResources();
44 final Vector<Resource> clusterSets = new Vector<Resource>();
45 final Vector<Resource> resources = new Vector<Resource>();
47 public void test() throws DatabaseException {
48 Session session = getSession();
49 session.syncRequest(new InitClusterSet());
50 session.syncRequest(new Init());
51 session.syncRequest(new Modi());
52 session.syncRequest(new Check());
53 session.syncRequest(new ModiWO());
54 session.syncRequest(new Check());
56 class InitClusterSet extends WriteRequest {
58 public void perform(WriteGraph g) throws DatabaseException {
59 Layer0 l0 = Layer0.getInstance(g);
60 String base = "http://Projects/Development%20Project";
61 String tail = "Issue3420Test2";
62 Resource pl = g.getResource(base);
64 System.out.println("projects=" + pl);
65 Resource il = g.getPossibleResource(base + "/" + tail);
69 assertTrue("Old cluster set data lost.", null == il);
71 g.claim(il, l0.InstanceOf, null, l0.Library);
72 g.claim(il, l0.PartOf, pl);
73 g.claimLiteral(il, l0.HasName, tail);
74 } catch (ClusterSetExistException e) {
76 assertTrue("Missing library resource.", null != il);
78 clusterSets.setSize(2);
79 clusterSets.set(0, pl);
80 clusterSets.set(1, il);
83 class Init extends ReadRequest {
85 public void run(ReadGraph g) throws DatabaseException {
87 Layer0 l0 = Layer0.getInstance(g);
88 for (Resource r : g.getObjects(clusterSets.get(1), l0.ConsistsOf)) {
91 System.out.println("Resource " + r);
95 System.out.println("Resource count=" + resources.size());
98 class Modi extends WriteRequest {
100 public void perform(WriteGraph g) throws DatabaseException {
101 for (int i=0; i<CLUSTER_FILL_SIZE * 2 + 33; ++i) {
102 Resource r = g.newResource(clusterSets.get(0));
103 g.claim(r, L0.InstanceOf, null, L0.Entity);
104 g.claim(r, L0.PartOf, clusterSets.get(1));
109 class ModiWO extends WriteOnlyRequest {
111 public void perform(WriteOnlyGraph g) throws DatabaseException {
112 for (int i=0; i<CLUSTER_FILL_SIZE * 2 + 33; ++i) {
113 Resource r = g.newResource(clusterSets.get(0));
114 g.claim(r, L0.InstanceOf, null, L0.Entity);
115 g.claim(r, L0.PartOf, L0.ConsistsOf, clusterSets.get(1));
120 class Check extends ReadRequest {
122 public void run(ReadGraph g) throws DatabaseException {
123 ClusteringSupport cs = g.getService(ClusteringSupport.class);
124 Map<Long, Integer> clusters = new HashMap<Long, Integer>();
125 for (int i=0; i<resources.size(); ++i){
126 long cluster = cs.getCluster(resources.get(i));
127 Integer count = clusters.get(cluster);
130 clusters.put(cluster, ++count);
132 Set<Long> sortedKeys = new TreeSet<Long>();
133 for (Map.Entry<Long, Integer> i : clusters.entrySet())
134 sortedKeys.add(i.getKey());
136 for (long key : sortedKeys) {
137 int count = clusters.get(key);
139 System.out.println("cluster=" + key + " count=" + count);
142 if (resources.size() != sum) {
143 String msg = "Resource count mismatch. expected=" + resources.size() + " realized=" + sum;
145 System.out.println(msg);
148 int n = resources.size() / CLUSTER_FILL_SIZE + 1;
149 if (n < clusters.size()) {
150 String msg = "Cluster count mismatch. expected=" + n + " realized=" + clusters.size();
152 System.out.println(msg);