package org.simantics.db.tests.api.readGraph.common; import java.util.UUID; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.tests.common.Writes; import org.simantics.layer0.Layer0; /* Provides simple ontologies for unit tests of various functionalities of * ReadGraph and WriteGraph methods. */ public class Ontologies { // 1 public static Resource typeMultipleSupertype(Layer0 L0, WriteGraph graph) throws DatabaseException { // New type with assertions Resource type = graph.newResource(); graph.claim(type, L0.Inherits, L0.Entity); Resource named1 = Writes.named(graph, UUID.randomUUID().toString()); Resource named2 = Writes.named(graph, UUID.randomUUID().toString()); graph.claim(type, L0.SupertypeOf, named1); graph.claim(type, L0.SupertypeOf, named2); return type; } // 2 public static Resource typeInherited(Layer0 L0, WriteGraph graph) throws DatabaseException { // Create first resource and give it a name Resource type = graph.newResource(); graph.claim(type, L0.Inherits, L0.Entity); Resource string1 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type, L0.HasName, string1); // Create second resource and give it a name Resource type2 = graph.newResource(); Resource string2 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type2, L0.HasName, string2); // second resource inherits from first resource graph.claim(type2, L0.Inherits, type); return type; } // 3 public static Resource typeMultipleFunctional(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource type = graph.newResource(); graph.claim(type, L0.Inherits, L0.Entity); Resource string1 = Writes.string(graph, UUID.randomUUID().toString()); Resource string2 = Writes.string(graph, UUID.randomUUID().toString()); Resource string3 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type, L0.HasName, string1); graph.claim(type, L0.HasName, string2); graph.claim(type, L0.HasName, string3); return type; } // 4 public static Resource typeMultipleInheritance(Layer0 L0, WriteGraph graph) throws DatabaseException { // New type with assertions Resource type2 = graph.newResource(); graph.claim(type2, L0.Inherits, L0.Entity); Resource type3 = graph.newResource(); graph.claim(type3, L0.Inherits, L0.Entity); Resource type = graph.newResource(); Resource string1 = Writes.string(graph, UUID.randomUUID().toString()); Resource string2 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type2, L0.InstanceOf, string1); graph.claim(type3, L0.InstanceOf, string2); graph.claim(type, L0.Inherits, type2); graph.claim(type, L0.Inherits, type3); return type; } // 5 public static Resource typeMultipleInheritanceConsistsOf(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource type2 = graph.newResource(); graph.claim(type2, L0.Inherits, L0.Entity); Resource type3 = graph.newResource(); graph.claim(type3, L0.Inherits, L0.Entity); Resource type = graph.newResource(); graph.claim(type, L0.Inherits, type2); graph.claim(type, L0.Inherits, type3); Resource named1 = Writes.named(graph, UUID.randomUUID().toString()); Resource named2 = Writes.named(graph, UUID.randomUUID().toString()); Resource named3 = Writes.named(graph, UUID.randomUUID().toString()); Resource named4 = Writes.named(graph, UUID.randomUUID().toString()); Resource named5 = Writes.named(graph, UUID.randomUUID().toString()); Resource named6 = Writes.named(graph, UUID.randomUUID().toString()); graph.claim(type, L0.ConsistsOf, named1); graph.claim(type, L0.ConsistsOf, named2); graph.claim(type2, L0.ConsistsOf, named3); graph.claim(type2, L0.ConsistsOf, named4); graph.claim(type3, L0.ConsistsOf, named5); graph.claim(type3, L0.ConsistsOf, named6); return type; } // 6 public static Resource typeConsistsOfInheritance(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource type2 = graph.newResource(); graph.claim(type2, L0.Inherits, L0.Entity); Resource type3 = graph.newResource(); graph.claim(type3, L0.Inherits, L0.Entity); Resource type = graph.newResource(); graph.claim(type, L0.Inherits, type2); graph.claim(type, L0.Inherits, type3); Resource named1 = Writes.named(graph, UUID.randomUUID().toString()); graph.claim(type, L0.ConsistsOf, named1); graph.claim(type2, L0.ConsistsOf, named1); graph.claim(type3, L0.ConsistsOf, named1); return type; } public static Resource typeX(Layer0 L0, WriteGraph graph) throws DatabaseException { // New type with assertions Resource type = graph.newResource(); graph.claim(type, L0.Inherits, L0.Entity); Resource named1 = Writes.named(graph, UUID.randomUUID().toString()); Resource named2 = Writes.named(graph, UUID.randomUUID().toString()); Resource string1 = Writes.string(graph, UUID.randomUUID().toString()); // Assert some nonfunctional objects graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.ConsistsOf, named1)); graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.ConsistsOf, named2)); // Assert a functional object graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.HasName, string1)); return type; } // 7 public static Resource typeCyclicInheritance(Layer0 L0, WriteGraph graph) throws DatabaseException { // Create first resource and give it a name Resource type1 = graph.newResource(); graph.claim(type1, L0.Inherits, L0.Entity); Resource string1 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type1, L0.HasName, string1); // Create second resource and give it a name Resource type2 = graph.newResource(); graph.claim(type2, L0.Inherits, L0.Entity); Resource string2 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type2, L0.HasName, string2); // Create third resource and give it a name Resource type3 = graph.newResource(); graph.claim(type3, L0.Inherits, L0.Entity); string2 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type2, L0.HasName, string2); // second resource inherits from first resource graph.claim(type2, L0.Inherits, type1); // third resource inherits from second resource graph.claim(type3, L0.Inherits, type2); // Completing loop: first resource inherits from third resource graph.claim(type1, L0.Inherits, type3); return type1; } // assertion3 public static Resource typeMultipleFunctionalAssertions(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource type = graph.newResource(); graph.claim(type, L0.Inherits, L0.Entity); Resource string1 = Writes.string(graph, UUID.randomUUID().toString()); Resource string2 = Writes.string(graph, UUID.randomUUID().toString()); Resource string3 = Writes.string(graph, UUID.randomUUID().toString()); graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.HasName, string1)); graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.HasName, string2)); graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.HasName, string3)); return type; } // assertion6 public static Resource typeFunctionalAndNonFunctionalAssertions(Layer0 L0, WriteGraph graph) throws DatabaseException { // New type with assertions Resource type = graph.newResource(); graph.claim(type, L0.Inherits, L0.Entity); Resource named1 = Writes.named(graph, UUID.randomUUID().toString()); Resource named2 = Writes.named(graph, UUID.randomUUID().toString()); Resource string1 = Writes.string(graph, UUID.randomUUID().toString()); // Assert some nonfunctional objects graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.ConsistsOf, named1)); graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.ConsistsOf, named2)); // Assert a functional object graph.claim(type, L0.Asserts, Writes.assertion(graph, L0.HasName, string1)); return type; } //assertion7 public static Resource typeCyclicAssertion(Layer0 L0, WriteGraph graph) throws DatabaseException { // New type with assertions Resource type1 = graph.newResource(); graph.claim(type1, L0.Inherits, L0.Entity); Resource type2 = graph.newResource(); graph.claim(type2, L0.Inherits, L0.Entity); graph.claim(type1, L0.HasName, Writes.string(graph, UUID.randomUUID().toString())); // named1 = Writes.named(graph, UUID.randomUUID().toString()); // named2 = Writes.named(graph, UUID.randomUUID().toString()); // Assert some nonfunctional objects graph.claim(type1, L0.Asserts, Writes.assertion(graph, L0.ConsistsOf, type2)); graph.claim(type2, L0.Asserts, Writes.assertion(graph, L0.ConsistsOf, type1)); return type1; } public static Resource blankInstanceofEntity(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource resource = graph.newResource(); graph.claim(resource, L0.InstanceOf, null, L0.Entity); return resource; } public static Resource relatedTypelessResource(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource resource = graph.newResource(); Resource name = graph.newResource(); graph.claim(name, L0.InstanceOf, null, L0.String); graph.claimValue(name, "A Typeless Resource"); graph.claim(resource, L0.HasName, name); return resource; } public static Resource relatedConsiststOfString(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource instance = graph.newResource(); Resource resource = graph.newResource(); graph.claim(instance, L0.InstanceOf, null, L0.String); graph.claimValue(instance, "Test String"); graph.claim(resource, L0.ConsistsOf, instance); return resource; } public static Resource relatedTypelessConsistsOf(Layer0 L0, WriteGraph graph) throws DatabaseException { Resource resource1 = graph.newResource(); Resource resource2 = graph.newResource(); Resource name = graph.newResource(); graph.claim(name, L0.InstanceOf, null, L0.String); graph.claimValue(name, "A Typeless Resource"); graph.claim(resource1, L0.HasName, name); graph.claim(resource2, L0.ConsistsOf, resource1); return resource2; } /* Type testing public static Resource value1(Layer0 L0, WriteGraph graph) throws DatabaseException { assertTrue(g.getValue(l0.True)); try { g.getValue(l0.Entity); } catch (DoesNotContainValueException e) { return; } fail("GetValue did not throw DoesNotContainValueException."); } } public static Resource value2(Layer0 L0, WriteGraph graph) throws DatabaseException { s.syncRequest(new ReadRequest() { @Override public void run(ReadGraph g) throws DatabaseException { Layer0 l0 = Layer0.getInstance(g); // g.getValue(null, null); assertTrue(g.getValue(l0.True, Bindings.BOOLEAN)); try { g.getValue(l0.Entity, Bindings.BOOLEAN); } catch (DoesNotContainValueException e) { return; } fail("GetValue did not throw DoesNotContainValueException."); } }); } */ }