Search in sources :

Example 21 with Attribute

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());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Test(org.junit.Test)

Example 22 with Attribute

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);
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 23 with Attribute

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());
}
Also used : Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Collection(java.util.Collection) GraknTxOperationException(ai.grakn.exception.GraknTxOperationException) Test(org.junit.Test)

Example 24 with Attribute

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());
}
Also used : Attribute(ai.grakn.concept.Attribute) Test(org.junit.Test)

Example 25 with Attribute

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");
}
Also used : Attribute(ai.grakn.concept.Attribute) Test(org.junit.Test)

Aggregations

Attribute (ai.grakn.concept.Attribute)36 Test (org.junit.Test)26 GraknTx (ai.grakn.GraknTx)15 EntityType (ai.grakn.concept.EntityType)15 Entity (ai.grakn.concept.Entity)14 RelationshipType (ai.grakn.concept.RelationshipType)10 Set (java.util.Set)10 Role (ai.grakn.concept.Role)9 AttributeType (ai.grakn.concept.AttributeType)8 Relationship (ai.grakn.concept.Relationship)8 Label (ai.grakn.concept.Label)7 Thing (ai.grakn.concept.Thing)6 Concept (ai.grakn.concept.Concept)5 HashSet (java.util.HashSet)5 ConceptId (ai.grakn.concept.ConceptId)4 Schema (ai.grakn.util.Schema)4 GraknTxOperationException (ai.grakn.exception.GraknTxOperationException)3 Sets (com.google.common.collect.Sets)3 Collection (java.util.Collection)3 Collectors (java.util.stream.Collectors)3