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 package org.simantics.db.tests.api.story.misc;
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;
31 public class QuerySemanticsTest {
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);
50 Resource createFunctionalRelation(WriteGraph g, String name) throws DatabaseException {
51 Resource result = createRelation(g, name);
52 g.claim(result, b.InstanceOf, b.FunctionalRelation);
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);
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);
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);
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() {
86 public void perform(WriteGraph g) throws DatabaseException {
87 b = Layer0.getInstance(g);
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);
100 public void tearDown() throws Exception {
101 LifecycleSupport support = session.getService(LifecycleSupport.class);
109 session.syncRequest(new ReadRequest() {
112 public void run(ReadGraph g) throws DatabaseException {
113 System.out.println("--- getStatements(relation1) ---------------------");
114 for(Statement stat : g.getStatements(instance1, relation1)) {
116 NameUtils.getSafeName(g, stat.getSubject()) + " " +
117 NameUtils.getSafeName(g, stat.getPredicate()) + " " +
118 NameUtils.getSafeName(g, stat.getObject())
121 System.out.println("--- getStatements(IsRelatedTo) ---------------------");
122 for(Statement stat : g.getStatements(instance1, b.IsRelatedTo)) {
124 NameUtils.getSafeName(g, stat.getSubject()) + " " +
125 NameUtils.getSafeName(g, stat.getPredicate()) + " " +
126 NameUtils.getSafeName(g, stat.getObject())
129 System.out.println("--- getObjects(relation1) ---------------------");
130 for(Resource r : g.getObjects(instance1, relation1)) {
132 NameUtils.getSafeName(g, r)
135 System.out.println("--- getObjects(IsRelatedTo) ---------------------");
136 for(Resource r : g.getObjects(instance1, b.IsRelatedTo)) {
138 NameUtils.getSafeName(g, r)
144 } catch (DatabaseException e) {