use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class JsonMigratorTest method whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph.
@Test
public void whenMigratorExecutedOverDataWithAllDataTypes_AllDataIsPersistedInGraph() throws FileNotFoundException {
load(factory, getFile("json", "all-types/schema.gql"));
String template = "" + "insert $x isa thingy\n" + " has a-boolean <a-boolean>\n" + " has a-number <a-number>\n" + " for (int in <array-of-ints> ) do {\n" + " has a-int <int>\n" + " }\n" + " has a-string <a-string>\n" + " if (<a-null> != null) do {has a-null <a-null>};";
declareAndLoad(template, "all-types/data.json");
try (GraknTx graph = factory.open(GraknTxType.READ)) {
EntityType rootType = graph.getEntityType("thingy");
Set<Entity> things = rootType.instances().collect(toSet());
assertEquals(1, things.size());
Entity thing = things.iterator().next();
Collection<Object> integers = getResources(graph, thing, Label.of("a-int")).map(Attribute::getValue).collect(toSet());
assertEquals(Sets.newHashSet(1L, 2L, 3L), integers);
Attribute aBoolean = getResource(graph, thing, Label.of("a-boolean"));
assertEquals(true, aBoolean.getValue());
Attribute aNumber = getResource(graph, thing, Label.of("a-number"));
assertEquals(42.1, aNumber.getValue());
Attribute aString = getResource(graph, thing, Label.of("a-string"));
assertEquals("hi", aString.getValue());
assertEquals(0, graph.getAttributeType("a-null").instances().count());
}
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class JsonMigratorTest method whenMigratorExecutedOverSimpleJson_DataIsPersistedInGraph.
@Test
public void whenMigratorExecutedOverSimpleJson_DataIsPersistedInGraph() {
load(factory, getFile("json", "simple-schema/schema.gql"));
String template = " \n" + "insert " + "$person isa person;\n" + " \n" + "$address isa address\n" + " has city <address.city>;\n" + "\n" + "$street isa street-address\n" + " has street <address.streetAddress.street>\n" + " has number <address.streetAddress.number>;\n" + "\n" + "(address-with-street: $address, street-of-address: $street) isa address-has-street;\n" + "\n" + "(person-with-address: $person, address-of-person: $address) isa has-address;\n" + "\n" + "for ( <phoneNumber> ) do {\n" + " $phone isa phone-number\n" + " has location <location>\n" + " has code <code>;\n" + " \n" + " (person-with-phone: $person, phone-of-person: $phone) isa has-phone;\n" + " \n" + "} ";
declareAndLoad(template, "simple-schema/data.json");
try (GraknTx graph = factory.open(GraknTxType.READ)) {
EntityType personType = graph.getEntityType("person");
assertEquals(1, personType.instances().count());
Entity person = personType.instances().iterator().next();
Entity address = getProperty(graph, person, "has-address").asEntity();
Entity streetAddress = getProperty(graph, address, "address-has-street").asEntity();
Attribute number = getResource(graph, streetAddress, Label.of("number"));
assertEquals(21L, number.getValue());
Attribute street = getResource(graph, streetAddress, Label.of("street"));
assertEquals("2nd Street", street.getValue());
Attribute city = getResource(graph, address, Label.of("city")).asAttribute();
assertEquals("New York", city.getValue());
Collection<Thing> phoneNumbers = getProperties(graph, person, "has-phone");
assertEquals(2, phoneNumbers.size());
boolean phoneNumbersCorrect = phoneNumbers.stream().allMatch(phoneNumber -> {
Object location = getResource(graph, phoneNumber, Label.of("location")).getValue();
Object code = getResource(graph, phoneNumber, Label.of("code")).getValue();
return ((location.equals("home") && code.equals(44L)) || (location.equals("work") && code.equals(45L)));
});
assertTrue(phoneNumbersCorrect);
}
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class AttributeTest method whenCreatingResourceWithAnInvalidDataType_DoNotCreateTheResource.
// this is deliberately an incorrect type for the test
@SuppressWarnings("unchecked")
@Test
public void whenCreatingResourceWithAnInvalidDataType_DoNotCreateTheResource() {
AttributeType longAttributeType = tx.putAttributeType("long", AttributeType.DataType.LONG);
try {
longAttributeType.putAttribute("Invalid Thing");
fail("Expected to throw");
} catch (GraknTxOperationException e) {
// expected failure
}
Collection<Attribute> instances = (Collection<Attribute>) longAttributeType.instances().collect(toSet());
assertThat(instances, empty());
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class AttributeTest method whenCreatingAnInferredAttribute_EnsureMarkedAsInferred.
@Test
public void whenCreatingAnInferredAttribute_EnsureMarkedAsInferred() {
AttributeTypeImpl at = AttributeTypeImpl.from(tx.putAttributeType("at", AttributeType.DataType.STRING));
Attribute attribute = at.putAttribute("blergh");
Attribute attributeInferred = at.putAttributeInferred("bloorg");
assertFalse(attribute.isInferred());
assertTrue(attributeInferred.isInferred());
}
use of ai.grakn.concept.Attribute in project grakn by graknlabs.
the class AttributeTest method whenCreatingAnInferredAttributeWhichIsAlreadyNonInferred_Throw.
@Test
public void whenCreatingAnInferredAttributeWhichIsAlreadyNonInferred_Throw() {
AttributeTypeImpl at = AttributeTypeImpl.from(tx.putAttributeType("at", AttributeType.DataType.STRING));
Attribute attribute = at.putAttribute("blergh");
expectedException.expect(GraknTxOperationException.class);
expectedException.expectMessage(GraknTxOperationException.nonInferredThingExists(attribute).getMessage());
at.putAttributeInferred("blergh");
}
Aggregations