]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/tutorial/Local1.java
(refs #7358) Mars to Oxygen updates
[simantics/platform.git] / tests / org.simantics.db.tests / tutorial / Local1.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.testing.common.Tests;
23 import org.simantics.layer0.Layer0;
24
25
26 public class Local1 {
27         static String newInstanceName ="Local1InstanceName";
28         static Session session = null;
29         public static void main(String[] args) {
30                 try {
31                         session = Tests.getTestHandler().getSession();
32                         session.syncRequest(new WriteRequest() {
33                     @Override
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);
40                                 }
41                         });
42
43                 session.syncRequest(new ReadRequest() {
44                     @Override
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)) {
52                                                         newResource = r;
53                                                         break;
54                                                 }
55                                         }
56                                         if (newResource == null) {
57                                                 throw new ValidationException("Could not find created resource");
58                                         }
59                                         if (!g.isInstanceOf(newResource, b.Type))
60                                                 throw new ValidationException("Created resource is not an instance of Type");
61                                 }
62                 });
63
64                 session.syncRequest(new ReadRequest() {
65                     @Override
66                     public void run(ReadGraph g) throws DatabaseException {
67                         Layer0 b = Layer0.getInstance(g);
68                         int count = 0;
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)) {
73                                                     count++;
74                                                 }
75                                         }
76                                         System.out.println("instance count = " + count);
77                                 }
78                 });
79
80 //              LifecycleSupport ls = session.getService(LifecycleSupport.class);
81 //            PublishSynchroniseSupport pss = session.getService(PublishSynchroniseSupport.class);
82 //            pss.publish();
83 //            ls.close();
84             System.out.println("Ok.");
85 //              } catch (ClassNotFoundException e) {
86 //                      e.printStackTrace();
87                 } catch (DatabaseException e) {
88                         e.printStackTrace();
89                 }
90         }
91 }