]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.db.tests/src/org/simantics/db/tests/api/story/misc/QuerySemanticsTest.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / api / story / misc / QuerySemanticsTest.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 package org.simantics.db.tests.api.story.misc;
13
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.Statement;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.request.ReadRequest;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.common.utils.NameUtils;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.service.LifecycleSupport;
27 import org.simantics.db.testing.annotation.Fails;
28 import org.simantics.db.testing.common.Tests;
29 import org.simantics.layer0.Layer0;
30
31 public class QuerySemanticsTest {
32
33     Session session;
34     Layer0 b;
35
36     Resource instance1;
37     Resource relation1;
38     Resource type1;
39
40     Resource createRelation(WriteGraph g, String name) throws DatabaseException {
41         Resource result = g.newResource();
42         g.claimLiteral(result, b.HasName, name);
43         Resource inv = g.newResource();
44         g.claim(result, b.SubrelationOf, b.IsRelatedTo);
45         g.claim(inv, b.SubrelationOf, b.IsRelatedTo);
46         g.claim(result, b.InverseOf, inv);
47         return result;
48     }
49
50     Resource createFunctionalRelation(WriteGraph g, String name) throws DatabaseException {
51         Resource result = createRelation(g, name);
52         g.claim(result, b.InstanceOf, b.FunctionalRelation);
53         return result;
54     }
55
56     Resource createType(WriteGraph g, String name) throws DatabaseException {
57         Resource result = g.newResource();
58         g.claimLiteral(result, b.HasName, name);
59         g.claim(result, b.Inherits, b.Entity);
60         return result;
61     }
62
63     Resource createInstance(WriteGraph g, String name, Resource type) throws DatabaseException {
64         Resource result = g.newResource();
65         g.claimLiteral(result, b.HasName, name);
66         g.claim(result, b.InstanceOf, type);
67         return result;
68     }
69
70     void assertion(WriteGraph g, Resource type, Resource pred, Resource obj) throws DatabaseException {
71         Resource assertion = g.newResource();
72         g.claim(assertion, b.InstanceOf, b.Assertion);
73         g.claim(type, b.Asserts, assertion);
74         g.claim(assertion, b.HasPredicate, pred);
75         g.claim(assertion, b.HasObject, obj);
76     }
77
78     @Before
79     public void setUp() throws Exception {
80 //        SessionFactory factory = new SessionFactory(Configuration.get().host, Configuration.get().port);
81 //        session = factory.create();
82         session = Tests.getTestHandler().getSession();
83         session.syncRequest(new WriteRequest() {
84
85             @Override
86             public void perform(WriteGraph g) throws DatabaseException {
87                 b = Layer0.getInstance(g);
88
89                 relation1 = createFunctionalRelation(g, "Relation1");
90                 type1 = createType(g, "Type1");
91                 instance1 = createInstance(g, "Instance1", type1);
92                 assertion(g, type1, relation1, b.Double);
93                 g.claim(instance1, relation1, b.Float);
94             }
95
96         });
97     }
98
99     @After
100     public void tearDown() throws Exception {
101         LifecycleSupport support = session.getService(LifecycleSupport.class);
102         support.close();
103     }
104
105     @Fails
106     @Test
107     public void test() {
108         try {
109                         session.syncRequest(new ReadRequest() {
110
111                             @Override
112                             public void run(ReadGraph g) throws DatabaseException {
113                                 System.out.println("--- getStatements(relation1) ---------------------");
114                                 for(Statement stat : g.getStatements(instance1, relation1)) {
115                                     System.out.println(
116                                         NameUtils.getSafeName(g, stat.getSubject()) + " " +
117                                         NameUtils.getSafeName(g, stat.getPredicate()) + " " +
118                                         NameUtils.getSafeName(g, stat.getObject())
119                                         );
120                                 }
121                                 System.out.println("--- getStatements(IsRelatedTo) ---------------------");
122                                 for(Statement stat : g.getStatements(instance1, b.IsRelatedTo)) {
123                                     System.out.println(
124                                         NameUtils.getSafeName(g, stat.getSubject()) + " " +
125                                         NameUtils.getSafeName(g, stat.getPredicate()) + " " +
126                                         NameUtils.getSafeName(g, stat.getObject())
127                                         );
128                                 }
129                                 System.out.println("--- getObjects(relation1) ---------------------");
130                                 for(Resource r : g.getObjects(instance1, relation1)) {
131                                     System.out.println(
132                                         NameUtils.getSafeName(g, r)
133                                         );
134                                 }
135                                 System.out.println("--- getObjects(IsRelatedTo) ---------------------");
136                                 for(Resource r : g.getObjects(instance1, b.IsRelatedTo)) {
137                                     System.out.println(
138                                         NameUtils.getSafeName(g, r)
139                                         );
140                                 }
141                             }
142
143                         });
144                 } catch (DatabaseException e) {
145                         e.printStackTrace();
146                 }
147     }
148
149 }