]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/tutorial/AdvancedHeadless.java
Support VirtualGraph in GraphToDiagramSynchronizer and DiagramSGProvider
[simantics/platform.git] / tests / org.simantics.db.tests / tutorial / AdvancedHeadless.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.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.Session;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.ValidationException;
23 import org.simantics.db.testing.common.Tests;
24 import org.simantics.layer0.Layer0;
25
26
27 public class AdvancedHeadless {
28     static String newInstanceName ="AdvancedClientTestInstanceName";
29     static Session session = null;
30     static Resource newResource;
31
32     public static void main(String[] args) throws InterruptedException {
33         try {
34             session = Tests.getTestHandler().getSession();
35             session.syncRequest(new WriteRequest() {
36                 @Override
37                 public void perform(WriteGraph g) throws DatabaseException {
38                     Layer0 b = Layer0.getInstance(g);
39                     newResource = g.newResource();
40                     g.claim(newResource, b.InstanceOf, null, b.Type);
41                     g.claimLiteral(newResource, b.HasName, newInstanceName);
42                     g.claim(g.getRootLibrary(), b.ConsistsOf, newResource);
43                     g.claimLiteral(newResource, b.HasDescription, "1");
44                 }
45             });
46             session.syncRequest(new WriteRequest() {
47
48                 @Override
49                 public void perform(WriteGraph g) throws DatabaseException {
50                      Layer0 b = Layer0.getInstance(g);
51                      g.claimLiteral(newResource, b.HasDescription, "11");
52
53                 }
54
55             });
56
57 //            Thread.sleep(5000);
58
59             session.syncRequest(new WriteRequest() {
60
61                 @Override
62                 public void perform(WriteGraph g) throws DatabaseException {
63                      Layer0 b = Layer0.getInstance(g);
64                      g.claimLiteral(newResource, b.HasDescription, "111");
65
66                 }
67
68             });
69
70 //            Thread.sleep(5000);
71
72             session.syncRequest(new WriteRequest() {
73
74                 @Override
75                 public void perform(WriteGraph g) throws DatabaseException {
76                     Layer0 b = Layer0.getInstance(g);
77                      g.claimLiteral(newResource, b.HasDescription, "1111");
78
79                 }
80
81             });
82
83             session.syncRequest(new ReadRequest() {
84                 @Override
85                 public void run(ReadGraph g) throws DatabaseException {
86                     Layer0 b = Layer0.getInstance(g);
87                     Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
88                     Resource newResource = null;
89                     for (Resource r : resources) {
90                         String value = g.getPossibleRelatedValue(r, b.HasName);
91                         if (null != value && value.equals(newInstanceName)) {
92                             newResource = r;
93                             String s = g.getRelatedValue(r, b.HasDescription, Bindings.STRING);
94                             if (!s.equals("1111"))
95                                 throw new ValidationException("Value is not correct.");
96                             break;
97                         }
98                     }
99                     if (newResource == null) {
100                         throw new ValidationException("Could not find created resource");
101                     }
102                     if (!g.isInstanceOf(newResource, b.Type))
103                         throw new ValidationException("Created resource is not an instance of Type");
104                 }
105             });
106
107             session.syncRequest(new ReadRequest() {
108                 @Override
109                 public void run(ReadGraph g) throws DatabaseException {
110                     Layer0 b = Layer0.getInstance(g);
111                     int count = 0;
112                     Collection<Resource> resources = g.getObjects(g.getRootLibrary(), b.ConsistsOf);
113                     for (Resource r : resources) {
114                         String value = g.getPossibleRelatedValue(r, b.HasName);
115                         if (null != value && value.equals(newInstanceName)) {
116                             count++;
117                         }
118                     }
119                     System.out.println("instance count = " + count);
120                 }
121             });
122
123             //LifecycleSupport ls = session.getService(LifecycleSupport.class);
124             //ls.save();
125             //ls.close();
126             System.out.println("Ok. No ls.close()");
127             System.exit(0);
128
129 //        } catch (ClassNotFoundException e) {
130 //            e.printStackTrace();
131         } catch (DatabaseException e) {
132             e.printStackTrace();
133         }
134     }
135 }