use of ai.grakn.GraknTx in project grakn by graknlabs.
the class GrpcServerIT method whenDefiningASchema_TheSchemaIsDefined.
@Test
public void whenDefiningASchema_TheSchemaIsDefined() {
try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
EntityType animal = tx.putEntityType("animal");
EntityType dog = tx.putEntityType("dog").sup(animal);
EntityType cat = tx.putEntityType("cat");
animal.sub(cat);
cat.setLabel(Label.of("feline"));
dog.setAbstract(true).setAbstract(false);
cat.setAbstract(true);
RelationshipType chases = tx.putRelationshipType("chases");
Role chased = tx.putRole("chased");
Role chaser = tx.putRole("chaser");
chases.relates(chased).relates(chaser);
Role pointlessRole = tx.putRole("pointless-role");
tx.putRelationshipType("pointless").relates(pointlessRole);
chases.relates(pointlessRole).deleteRelates(pointlessRole);
dog.plays(chaser);
cat.plays(chased);
AttributeType<String> name = tx.putAttributeType("name", DataType.STRING);
AttributeType<String> id = tx.putAttributeType("id", DataType.STRING).setRegex("(good|bad)-dog");
AttributeType<Long> age = tx.putAttributeType("age", DataType.LONG);
animal.attribute(name);
animal.key(id);
dog.attribute(age).deleteAttribute(age);
cat.key(age).deleteKey(age);
cat.plays(chaser).deletePlays(chaser);
Entity dunstan = dog.addEntity();
Attribute<String> dunstanId = id.putAttribute("good-dog");
assertNotNull(dunstan.attributeRelationship(dunstanId));
Attribute<String> dunstanName = name.putAttribute("Dunstan");
dunstan.attribute(dunstanName).deleteAttribute(dunstanName);
chases.addRelationship().addRolePlayer(chaser, dunstan);
tx.commit();
}
try (GraknTx tx = localSession.open(GraknTxType.READ)) {
EntityType animal = tx.getEntityType("animal");
EntityType dog = tx.getEntityType("dog");
EntityType cat = tx.getEntityType("feline");
RelationshipType chases = tx.getRelationshipType("chases");
Role chased = tx.getRole("chased");
Role chaser = tx.getRole("chaser");
AttributeType<String> name = tx.getAttributeType("name");
AttributeType<String> id = tx.getAttributeType("id");
Entity dunstan = Iterators.getOnlyElement(dog.instances().iterator());
Relationship aChase = Iterators.getOnlyElement(chases.instances().iterator());
assertEquals(animal, dog.sup());
assertEquals(animal, cat.sup());
assertEquals(ImmutableSet.of(chased, chaser), chases.relates().collect(toSet()));
assertEquals(ImmutableSet.of(chaser), dog.plays().filter(role -> !role.isImplicit()).collect(toSet()));
assertEquals(ImmutableSet.of(chased), cat.plays().filter(role -> !role.isImplicit()).collect(toSet()));
assertEquals(ImmutableSet.of(name, id), animal.attributes().collect(toSet()));
assertEquals(ImmutableSet.of(id), animal.keys().collect(toSet()));
assertEquals(ImmutableSet.of(name, id), dog.attributes().collect(toSet()));
assertEquals(ImmutableSet.of(id), dog.keys().collect(toSet()));
assertEquals(ImmutableSet.of(name, id), cat.attributes().collect(toSet()));
assertEquals(ImmutableSet.of(id), cat.keys().collect(toSet()));
assertEquals("good-dog", Iterables.getOnlyElement(dunstan.keys(id).collect(toSet())).getValue());
ImmutableMap<Role, ImmutableSet<?>> expectedRolePlayers = ImmutableMap.of(chaser, ImmutableSet.of(dunstan), chased, ImmutableSet.of());
assertEquals(expectedRolePlayers, aChase.allRolePlayers());
assertEquals("(good|bad)-dog", id.getRegex());
assertFalse(dog.isAbstract());
assertTrue(cat.isAbstract());
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class GrpcServerIT method whenGettingARule_TheInformationOnTheRuleIsCorrect.
@Test
public void whenGettingARule_TheInformationOnTheRuleIsCorrect() {
try (GraknTx remoteTx = remoteSession.open(GraknTxType.READ);
GraknTx localTx = localSession.open(GraknTxType.READ)) {
GetQuery query = remoteTx.graql().match(var("x").label("expectation-rule")).get();
ai.grakn.concept.Rule remoteConcept = query.stream().findAny().get().get("x").asRule();
ai.grakn.concept.Rule localConcept = localTx.getConcept(remoteConcept.getId()).asRule();
assertEquals(localConcept.getWhen(), remoteConcept.getWhen());
assertEquals(localConcept.getThen(), remoteConcept.getThen());
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class GrpcServerIT method whenExecutingAndCommittingAQuery_TheQueryIsCommitted.
@Test
public void whenExecutingAndCommittingAQuery_TheQueryIsCommitted() throws InterruptedException {
try (GraknTx tx = remoteSession.open(GraknTxType.WRITE)) {
tx.graql().define(label("person").sub("entity")).execute();
tx.commit();
}
try (GraknTx tx = localSession.open(GraknTxType.READ)) {
assertNotNull(tx.getEntityType("person"));
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class JsonMigratorTest method whenMigratorExecutedOverJsonDirectory_AllDataIsPersistedInGraph.
@Test
public void whenMigratorExecutedOverJsonDirectory_AllDataIsPersistedInGraph() {
load(factory, getFile("json", "string-or-object/schema.gql"));
String template = "\n" + "insert $thing isa the-thing\n" + " has a-string if (<the-thing.a-string> != null) do {<the-thing.a-string>}\n" + " else {<the-thing>} ;";
declareAndLoad(template, "string-or-object/data");
try (GraknTx graph = factory.open(GraknTxType.READ)) {
EntityType theThing = graph.getEntityType("the-thing");
assertEquals(2, theThing.instances().count());
Stream<Entity> things = theThing.instances();
boolean thingsCorrect = things.allMatch(thing -> {
Object string = getResource(graph, thing, Label.of("a-string")).getValue();
return string.equals("hello") || string.equals("goodbye");
});
assertTrue(thingsCorrect);
}
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class XMLMigratorTest method assertThingHasName.
private static void assertThingHasName(String name) {
try (GraknTx graph = session.open(GraknTxType.READ)) {
EntityType thingType = graph.getEntityType("thingy");
AttributeType nameType = graph.getAttributeType("name");
assertEquals(1, thingType.instances().count());
thingType.instances().forEach(thing -> {
assertEquals(1, thing.attributes(nameType).count());
assertEquals(name, thing.attributes(nameType).iterator().next().getValue());
});
}
}
Aggregations