Search in sources :

Example 1 with Attribute

use of ai.grakn.concept.Attribute in project grakn by graknlabs.

the class MigratorTestUtils method getResources.

public static Stream<Attribute> getResources(GraknTx graph, Thing thing, Label label) {
    Role roleOwner = graph.getSchemaConcept(Schema.ImplicitType.HAS_OWNER.getLabel(label));
    Role roleOther = graph.getSchemaConcept(Schema.ImplicitType.HAS_VALUE.getLabel(label));
    Stream<Relationship> relations = thing.relationships(roleOwner);
    return relations.flatMap(r -> r.rolePlayers(roleOther)).map(Concept::asAttribute);
}
Also used : Role(ai.grakn.concept.Role) InvalidKBException(ai.grakn.exception.InvalidKBException) Role(ai.grakn.concept.Role) Concept(ai.grakn.concept.Concept) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) HashSet(java.util.HashSet) Label(ai.grakn.concept.Label) AttributeType(ai.grakn.concept.AttributeType) RelationshipType(ai.grakn.concept.RelationshipType) Files(com.google.common.io.Files) GraknTx(ai.grakn.GraknTx) Relationship(ai.grakn.concept.Relationship) GraknTxType(ai.grakn.GraknTxType) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) GraknSession(ai.grakn.GraknSession) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Stream(java.util.stream.Stream) Thing(ai.grakn.concept.Thing) Schema(ai.grakn.util.Schema) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Concept(ai.grakn.concept.Concept) Relationship(ai.grakn.concept.Relationship)

Example 2 with Attribute

use of ai.grakn.concept.Attribute in project grakn by graknlabs.

the class EntityTest method whenAddingMultipleResourcesToEntity_EnsureDifferentRelationsAreBuilt.

@Test
public void whenAddingMultipleResourcesToEntity_EnsureDifferentRelationsAreBuilt() throws InvalidKBException {
    String resourceTypeId = "A Attribute Thing";
    EntityType entityType = tx.putEntityType("A Thing");
    AttributeType<String> attributeType = tx.putAttributeType(resourceTypeId, AttributeType.DataType.STRING);
    entityType.attribute(attributeType);
    Entity entity = entityType.addEntity();
    Attribute attribute1 = attributeType.putAttribute("A resource thing");
    Attribute attribute2 = attributeType.putAttribute("Another resource thing");
    assertEquals(0, entity.relationships().count());
    entity.attribute(attribute1);
    assertEquals(1, entity.relationships().count());
    entity.attribute(attribute2);
    assertEquals(2, entity.relationships().count());
    tx.commit();
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Test(org.junit.Test)

Example 3 with Attribute

use of ai.grakn.concept.Attribute in project grakn by graknlabs.

the class EntityTest method whenAddingResourceToAnEntity_EnsureTheImplicitStructureIsCreated.

@Test
public void whenAddingResourceToAnEntity_EnsureTheImplicitStructureIsCreated() {
    Label resourceLabel = Label.of("A Attribute Thing");
    EntityType entityType = tx.putEntityType("A Thing");
    AttributeType<String> attributeType = tx.putAttributeType(resourceLabel, AttributeType.DataType.STRING);
    entityType.attribute(attributeType);
    Entity entity = entityType.addEntity();
    Attribute attribute = attributeType.putAttribute("A attribute thing");
    entity.attribute(attribute);
    Relationship relationship = entity.relationships().iterator().next();
    checkImplicitStructure(attributeType, relationship, entity, Schema.ImplicitType.HAS, Schema.ImplicitType.HAS_OWNER, Schema.ImplicitType.HAS_VALUE);
}
Also used : EntityType(ai.grakn.concept.EntityType) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Relationship(ai.grakn.concept.Relationship) Label(ai.grakn.concept.Label) Test(org.junit.Test)

Example 4 with Attribute

use of ai.grakn.concept.Attribute in project grakn by graknlabs.

the class RelationshipTest method whenAttributeLinkedToRelationshipIsInferred_EnsureItIsMarkedAsInferred.

@Test
public void whenAttributeLinkedToRelationshipIsInferred_EnsureItIsMarkedAsInferred() {
    AttributeType attributeType = tx.putAttributeType("Another thing of sorts", AttributeType.DataType.STRING);
    RelationshipType relationshipType = tx.putRelationshipType("A thing of sorts").attribute(attributeType);
    Attribute attribute = attributeType.putAttribute("Things");
    Relationship relationship = relationshipType.addRelationship();
    RelationshipImpl.from(relationship).attributeInferred(attribute);
    assertTrue(relationship.relationships().findAny().get().isInferred());
}
Also used : Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Relationship(ai.grakn.concept.Relationship) RelationshipType(ai.grakn.concept.RelationshipType) Test(org.junit.Test)

Example 5 with Attribute

use of ai.grakn.concept.Attribute in project grakn by graknlabs.

the class ResourceAtom method materialise.

@Override
public Stream<Answer> materialise() {
    Answer substitution = getParentQuery().getSubstitution();
    AttributeType type = getSchemaConcept().asAttributeType();
    Concept owner = substitution.get(getVarName());
    Var resourceVariable = getPredicateVariable();
    // if the attribute already exists, only attach a new link to the owner, otherwise create a new attribute
    if (substitution.containsVar(resourceVariable)) {
        Attribute attribute = substitution.get(resourceVariable).asAttribute();
        attachAttribute(owner, attribute);
        return Stream.of(substitution);
    } else {
        Attribute attribute = AttributeTypeImpl.from(type).putAttributeInferred(Iterables.getOnlyElement(getMultiPredicate()).getPredicate().equalsValue().get());
        attachAttribute(owner, attribute);
        return Stream.of(substitution.merge(new QueryAnswer(ImmutableMap.of(resourceVariable, attribute))));
    }
}
Also used : Concept(ai.grakn.concept.Concept) SchemaConcept(ai.grakn.concept.SchemaConcept) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) Var(ai.grakn.graql.Var)

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