+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.tests.api.story.misc;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.Statement;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.LifecycleSupport;
+import org.simantics.db.testing.annotation.Fails;
+import org.simantics.db.testing.common.Tests;
+import org.simantics.layer0.Layer0;
+
+public class QuerySemanticsTest {
+
+ Session session;
+ Layer0 b;
+
+ Resource instance1;
+ Resource relation1;
+ Resource type1;
+
+ Resource createRelation(WriteGraph g, String name) throws DatabaseException {
+ Resource result = g.newResource();
+ g.claimLiteral(result, b.HasName, name);
+ Resource inv = g.newResource();
+ g.claim(result, b.SubrelationOf, b.IsRelatedTo);
+ g.claim(inv, b.SubrelationOf, b.IsRelatedTo);
+ g.claim(result, b.InverseOf, inv);
+ return result;
+ }
+
+ Resource createFunctionalRelation(WriteGraph g, String name) throws DatabaseException {
+ Resource result = createRelation(g, name);
+ g.claim(result, b.InstanceOf, b.FunctionalRelation);
+ return result;
+ }
+
+ Resource createType(WriteGraph g, String name) throws DatabaseException {
+ Resource result = g.newResource();
+ g.claimLiteral(result, b.HasName, name);
+ g.claim(result, b.Inherits, b.Entity);
+ return result;
+ }
+
+ Resource createInstance(WriteGraph g, String name, Resource type) throws DatabaseException {
+ Resource result = g.newResource();
+ g.claimLiteral(result, b.HasName, name);
+ g.claim(result, b.InstanceOf, type);
+ return result;
+ }
+
+ void assertion(WriteGraph g, Resource type, Resource pred, Resource obj) throws DatabaseException {
+ Resource assertion = g.newResource();
+ g.claim(assertion, b.InstanceOf, b.Assertion);
+ g.claim(type, b.Asserts, assertion);
+ g.claim(assertion, b.HasPredicate, pred);
+ g.claim(assertion, b.HasObject, obj);
+ }
+
+ @Before
+ public void setUp() throws Exception {
+// SessionFactory factory = new SessionFactory(Configuration.get().host, Configuration.get().port);
+// session = factory.create();
+ session = Tests.getTestHandler().getSession();
+ session.syncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph g) throws DatabaseException {
+ b = Layer0.getInstance(g);
+
+ relation1 = createFunctionalRelation(g, "Relation1");
+ type1 = createType(g, "Type1");
+ instance1 = createInstance(g, "Instance1", type1);
+ assertion(g, type1, relation1, b.Double);
+ g.claim(instance1, relation1, b.Float);
+ }
+
+ });
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ LifecycleSupport support = session.getService(LifecycleSupport.class);
+ support.close();
+ }
+
+ @Fails
+ @Test
+ public void test() {
+ try {
+ session.syncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph g) throws DatabaseException {
+ System.out.println("--- getStatements(relation1) ---------------------");
+ for(Statement stat : g.getStatements(instance1, relation1)) {
+ System.out.println(
+ NameUtils.getSafeName(g, stat.getSubject()) + " " +
+ NameUtils.getSafeName(g, stat.getPredicate()) + " " +
+ NameUtils.getSafeName(g, stat.getObject())
+ );
+ }
+ System.out.println("--- getStatements(IsRelatedTo) ---------------------");
+ for(Statement stat : g.getStatements(instance1, b.IsRelatedTo)) {
+ System.out.println(
+ NameUtils.getSafeName(g, stat.getSubject()) + " " +
+ NameUtils.getSafeName(g, stat.getPredicate()) + " " +
+ NameUtils.getSafeName(g, stat.getObject())
+ );
+ }
+ System.out.println("--- getObjects(relation1) ---------------------");
+ for(Resource r : g.getObjects(instance1, relation1)) {
+ System.out.println(
+ NameUtils.getSafeName(g, r)
+ );
+ }
+ System.out.println("--- getObjects(IsRelatedTo) ---------------------");
+ for(Resource r : g.getObjects(instance1, b.IsRelatedTo)) {
+ System.out.println(
+ NameUtils.getSafeName(g, r)
+ );
+ }
+ }
+
+ });
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ }
+ }
+
+}