Search in sources :

Example 6 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class MongoEntityStorage method searchHasAllExplicitTypes.

/**
 * Searches the Entity storage for all Entities that contain all the
 * specified explicit type IDs.
 * @param explicitTypeIds the {@link ImmutableList} of {@link RyaURI}s that
 * are being searched for.
 * @return the {@link List} of {@link Entity}s that have all the specified
 * explicit type IDs. If nothing was found an empty {@link List} is
 * returned.
 * @throws EntityStorageException
 */
private List<Entity> searchHasAllExplicitTypes(final ImmutableList<RyaURI> explicitTypeIds) throws EntityStorageException {
    final List<Entity> hasAllExplicitTypesEntities = new ArrayList<>();
    if (!explicitTypeIds.isEmpty()) {
        // Grab the first type from the explicit type IDs.
        final RyaURI firstType = explicitTypeIds.get(0);
        // Check if that type exists anywhere in storage.
        final List<RyaURI> subjects = new ArrayList<>();
        Optional<Type> type;
        try {
            if (mongoTypeStorage == null) {
                mongoTypeStorage = new MongoTypeStorage(mongo, ryaInstanceName);
            }
            type = mongoTypeStorage.get(firstType);
        } catch (final TypeStorageException e) {
            throw new EntityStorageException("Unable to get entity type: " + firstType, e);
        }
        if (type.isPresent()) {
            // Grab the subjects for all the types we found matching "firstType"
            final ConvertingCursor<TypedEntity> cursor = search(Optional.empty(), type.get(), Collections.emptySet());
            while (cursor.hasNext()) {
                final TypedEntity typedEntity = cursor.next();
                final RyaURI subject = typedEntity.getSubject();
                subjects.add(subject);
            }
        }
        // Now grab all the Entities that have the subjects we found.
        for (final RyaURI subject : subjects) {
            final Optional<Entity> entityFromSubject = get(subject);
            if (entityFromSubject.isPresent()) {
                final Entity candidateEntity = entityFromSubject.get();
                // types they have.
                if (candidateEntity.getExplicitTypeIds().containsAll(explicitTypeIds)) {
                    hasAllExplicitTypesEntities.add(candidateEntity);
                }
            }
        }
    }
    return hasAllExplicitTypesEntities;
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) ArrayList(java.util.ArrayList) TypeStorageException(org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException) RyaURI(org.apache.rya.api.domain.RyaURI) Type(org.apache.rya.indexing.entity.model.Type) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity)

Example 7 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class DuplicateDataDetector method compareSmartUris.

/**
 * Compares two Smart URI's to determine if they have nearly identical data.
 * @param uri1 the first Smart {@link URI}. (not {@code null})
 * @param uri2 the second Smart {@link URI}. (not {@code null})
 * @return {@code true} if the two Smart URI's have nearly identical data.
 * {@code false} otherwise.
 * @throws SmartUriException
 */
public boolean compareSmartUris(final URI uri1, final URI uri2) throws SmartUriException {
    requireNonNull(uri1);
    requireNonNull(uri2);
    final Entity entity1 = SmartUriAdapter.deserializeUriEntity(uri1);
    final Entity entity2 = SmartUriAdapter.deserializeUriEntity(uri2);
    return compareEntities(entity1, entity2);
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity)

Example 8 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class SmartUriAdapter method convertMapToEntity.

private static Entity convertMapToEntity(final RyaURI subject, final Map<RyaURI, Map<URI, Value>> map) {
    final Entity.Builder entityBuilder = Entity.builder();
    entityBuilder.setSubject(subject);
    for (final Entry<RyaURI, Map<URI, Value>> typeEntry : map.entrySet()) {
        final RyaURI type = typeEntry.getKey();
        final Map<URI, Value> subMap = typeEntry.getValue();
        entityBuilder.setExplicitType(type);
        for (final Entry<URI, Value> entry : subMap.entrySet()) {
            final URI uri = entry.getKey();
            final Value value = entry.getValue();
            final RyaURI ryaUri = new RyaURI(uri.stringValue());
            final RyaURI ryaName = new RyaURI(uri.stringValue());
            final RyaType ryaType = new RyaType(value.stringValue());
            final Property property = new Property(ryaName, ryaType);
            entityBuilder.setProperty(ryaUri, property);
        }
    }
    final Entity entity = entityBuilder.build();
    return entity;
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) RyaURI(org.apache.rya.api.domain.RyaURI) Value(org.openrdf.model.Value) RyaType(org.apache.rya.api.domain.RyaType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashBiMap(com.google.common.collect.HashBiMap) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) Property(org.apache.rya.indexing.entity.model.Property)

Example 9 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class EntityQueryNodeIT method evaluate_constantObject.

@Test
public void evaluate_constantObject() throws Exception {
    final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), "testDB");
    final ValueFactory vf = ValueFactoryImpl.getInstance();
    final RyaURI subject = new RyaURI("urn:SSN:111-11-1111");
    final Entity entity = Entity.builder().setSubject(subject).setExplicitType(PERSON_TYPE.getId()).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:age"), RdfToRyaConversions.convertLiteral(vf.createLiteral(20)))).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:eye"), RdfToRyaConversions.convertLiteral(vf.createLiteral("blue")))).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:name"), RdfToRyaConversions.convertLiteral(vf.createLiteral("Bob")))).build();
    storage.create(entity);
    // A set of patterns that match a sepecific Entity subject.
    final List<StatementPattern> patterns = getSPs("SELECT * WHERE { " + "<urn:SSN:111-11-1111> <" + RDF.TYPE + "> <urn:person> ." + "<urn:SSN:111-11-1111> <urn:age> ?age . " + "<urn:SSN:111-11-1111> <urn:eye> \"blue\" . " + "<urn:SSN:111-11-1111> <urn:name> ?name . " + "}");
    final EntityQueryNode node = new EntityQueryNode(PERSON_TYPE, patterns, storage);
    final CloseableIteration<BindingSet, QueryEvaluationException> rez = node.evaluate(new MapBindingSet());
    final MapBindingSet expected = new MapBindingSet();
    expected.addBinding("age", vf.createLiteral("20"));
    expected.addBinding("-const-blue", vf.createLiteral("blue"));
    expected.addBinding("name", vf.createLiteral("Bob"));
    while (rez.hasNext()) {
        assertEquals(expected, rez.next());
        break;
    }
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) MongoEntityStorage(org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage) EntityStorage(org.apache.rya.indexing.entity.storage.EntityStorage) MongoEntityStorage(org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage) ValueFactory(org.openrdf.model.ValueFactory) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 10 with Entity

use of org.apache.rya.indexing.entity.model.Entity in project incubator-rya by apache.

the class MongoEntityStorageIT method search_byDataType.

@Test
public void search_byDataType() throws Exception {
    final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
    // The Type we will search by.
    final Type icecreamType = new Type(new RyaURI("urn:icecream"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:brand")).add(new RyaURI("urn:flavor")).add(new RyaURI("urn:cost")).build());
    // Some Person typed entities.
    final Entity alice = Entity.builder().setSubject(new RyaURI("urn:SSN/111-11-1111")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "Alice"))).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:age"), new RyaType(XMLSchema.INT, "30"))).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:eye"), new RyaType(XMLSchema.STRING, "blue"))).build();
    final Entity bob = Entity.builder().setSubject(new RyaURI("urn:SSN/222-22-2222")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "Bob"))).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:age"), new RyaType(XMLSchema.INT, "57"))).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:eye"), new RyaType(XMLSchema.STRING, "blue"))).build();
    // Some Icecream typed objects.
    final Entity chocolateIcecream = Entity.builder().setSubject(new RyaURI("urn:GTIN-14/00012345600012")).setExplicitType(new RyaURI("urn:icecream")).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:brand"), new RyaType(XMLSchema.STRING, "Awesome Icecream"))).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:flavor"), new RyaType(XMLSchema.STRING, "Chocolate"))).build();
    final Entity vanillaIcecream = Entity.builder().setSubject(new RyaURI("urn:GTIN-14/22356325213432")).setExplicitType(new RyaURI("urn:icecream")).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:brand"), new RyaType(XMLSchema.STRING, "Awesome Icecream"))).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:flavor"), new RyaType(XMLSchema.STRING, "Vanilla"))).build();
    final Entity strawberryIcecream = Entity.builder().setSubject(new RyaURI("urn:GTIN-14/77544325436721")).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:brand"), new RyaType(XMLSchema.STRING, "Awesome Icecream"))).setProperty(new RyaURI("urn:icecream"), new Property(new RyaURI("urn:flavor"), new RyaType(XMLSchema.STRING, "Strawberry"))).build();
    // Create the objects in the storage.
    storage.create(alice);
    storage.create(bob);
    storage.create(chocolateIcecream);
    storage.create(vanillaIcecream);
    storage.create(strawberryIcecream);
    // Search for all icecreams.
    final Set<TypedEntity> objects = new HashSet<>();
    try (final ConvertingCursor<TypedEntity> it = storage.search(Optional.empty(), icecreamType, new HashSet<>())) {
        while (it.hasNext()) {
            objects.add(it.next());
        }
    }
    // Verify the expected results were returned.
    final Set<TypedEntity> expected = Sets.newHashSet(chocolateIcecream.makeTypedEntity(new RyaURI("urn:icecream")).get(), vanillaIcecream.makeTypedEntity(new RyaURI("urn:icecream")).get());
    assertEquals(expected, objects);
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) Entity(org.apache.rya.indexing.entity.model.Entity) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) RyaType(org.apache.rya.api.domain.RyaType) Type(org.apache.rya.indexing.entity.model.Type) EntityStorage(org.apache.rya.indexing.entity.storage.EntityStorage) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) RyaType(org.apache.rya.api.domain.RyaType) Property(org.apache.rya.indexing.entity.model.Property) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Entity (org.apache.rya.indexing.entity.model.Entity)40 RyaURI (org.apache.rya.api.domain.RyaURI)31 Test (org.junit.Test)29 Property (org.apache.rya.indexing.entity.model.Property)24 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)23 RyaType (org.apache.rya.api.domain.RyaType)22 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)13 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)11 MongoEntityStorage (org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage)11 ReflectionToStringBuilder (org.apache.commons.lang.builder.ReflectionToStringBuilder)8 Builder (org.apache.rya.indexing.entity.model.Entity.Builder)8 MongoTypeStorage (org.apache.rya.indexing.entity.storage.mongo.MongoTypeStorage)8 RyaStatement (org.apache.rya.api.domain.RyaStatement)7 Type (org.apache.rya.indexing.entity.model.Type)6 URI (org.openrdf.model.URI)4 URIImpl (org.openrdf.model.impl.URIImpl)4 RyaTypeUtils.booleanRyaType (org.apache.rya.api.domain.RyaTypeUtils.booleanRyaType)3 RyaTypeUtils.byteRyaType (org.apache.rya.api.domain.RyaTypeUtils.byteRyaType)3 RyaTypeUtils.dateRyaType (org.apache.rya.api.domain.RyaTypeUtils.dateRyaType)3 RyaTypeUtils.doubleRyaType (org.apache.rya.api.domain.RyaTypeUtils.doubleRyaType)3