]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/tutorial/HeadlessClientToRunningCore.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / tests / org.simantics.db.tests / tutorial / HeadlessClientToRunningCore.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 import java.util.Collection;
13
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.service.LifecycleSupport;
23 import org.simantics.db.testing.common.Tests;
24 import org.simantics.layer0.Layer0;
25
26
27 public class HeadlessClientToRunningCore {
28         static String newInstanceName ="HeadlessClientTestInstanceName";
29         static Session session = null;
30         public static void main(String[] args) {
31                 try {
32                         session = Tests.getTestHandler().getSession();
33                         session.syncRequest(new WriteRequest() {
34                     @Override
35                     public void perform(WriteGraph g) throws DatabaseException {
36                         Layer0 b = Layer0.getInstance(g);
37                                         Resource newResource = g.newResource();
38                                         g.claim(newResource, b.InstanceOf, null, b.Type);
39                                         g.claimLiteral(newResource, b.HasName, newInstanceName);
40                                         g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
41                                 }
42                         });
43
44                 session.syncRequest(new ReadRequest() {
45                     @Override
46                     public void run(ReadGraph g) throws DatabaseException {
47                         Layer0 b = Layer0.getInstance(g);
48                                         Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
49                                         Resource newResource = null;
50                                         for (Resource r : resources) {
51                                             String value = g.getPossibleRelatedValue(r, b.HasName);
52                                                 if (null != value && value.equals(newInstanceName)) {
53                                                         newResource = r;
54                                                         break;
55                                                 }
56                                         }
57                                         if (newResource == null) {
58                                                 throw new ValidationException("Could not find created resource");
59                                         }
60                                         if (!g.isInstanceOf(newResource, b.Type))
61                                                 throw new ValidationException("Created resource is not an instance of Type");
62                                 }
63                 });
64
65                 session.syncRequest(new ReadRequest() {
66                     @Override
67                     public void run(ReadGraph g) throws DatabaseException {
68                         Layer0 b = Layer0.getInstance(g);
69                         int count = 0;
70                                         Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
71                                         for (Resource r : resources) {
72                                             String value = g.getPossibleRelatedValue(r, b.HasName);
73                                                 if (null != value && value.equals(newInstanceName)) {
74                                                     count++;
75                                                 }
76                                         }
77                                         System.out.println("instance count = " + count);
78                                 }
79                 });
80
81                 LifecycleSupport ls = session.getService(LifecycleSupport.class);
82             ls.close();
83             System.out.println("Ok.");
84
85 //              } catch (ClassNotFoundException e) {
86 //                      e.printStackTrace();
87                 } catch (DatabaseException e) {
88                         e.printStackTrace();
89                 }
90         }
91 }