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 import java.util.Collection;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.Session;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.common.request.WriteRequest;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.ValidationException;
22 import org.simantics.db.testing.common.Tests;
23 import org.simantics.layer0.Layer0;
27 static String newInstanceName ="Local1InstanceName";
28 static Session session = null;
29 public static void main(String[] args) {
31 session = Tests.getTestHandler().getSession();
32 session.syncRequest(new WriteRequest() {
34 public void perform(WriteGraph g) throws DatabaseException {
35 Layer0 b = Layer0.getInstance(g);
36 Resource newResource = g.newResource();
37 g.claim(newResource, b.InstanceOf, null, b.Type);
38 g.claimLiteral(newResource, b.HasName, newInstanceName);
39 g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
43 session.syncRequest(new ReadRequest() {
45 public void run(ReadGraph g) throws DatabaseException {
46 Layer0 b = Layer0.getInstance(g);
47 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
48 Resource newResource = null;
49 for (Resource r : resources) {
50 String value = g.getPossibleRelatedValue(r, b.HasName);
51 if (null != value && value.equals(newInstanceName)) {
56 if (newResource == null) {
57 throw new ValidationException("Could not find created resource");
59 if (!g.isInstanceOf(newResource, b.Type))
60 throw new ValidationException("Created resource is not an instance of Type");
64 session.syncRequest(new ReadRequest() {
66 public void run(ReadGraph g) throws DatabaseException {
67 Layer0 b = Layer0.getInstance(g);
69 Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
70 for (Resource r : resources) {
71 String value = g.getPossibleRelatedValue(r, b.HasName);
72 if (null != value && value.equals(newInstanceName)) {
76 System.out.println("instance count = " + count);
80 // LifecycleSupport ls = session.getService(LifecycleSupport.class);
81 // PublishSynchroniseSupport pss = session.getService(PublishSynchroniseSupport.class);
84 System.out.println("Ok.");
85 // } catch (ClassNotFoundException e) {
86 // e.printStackTrace();
87 } catch (DatabaseException e) {