Search in sources :

Example 1 with TypedEntity

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

the class MongoDbSmartUriIT method testQuery.

@Test
public void testQuery() throws SmartUriException {
    smartUriConverter.storeEntity(BOB_ENTITY);
    // Look up Person Type Entities that match Bob's SSN property
    final Set<Property> properties = new LinkedHashSet<>();
    properties.add(BOB_ENTITY.lookupTypeProperty(PERSON_TYPE, HAS_SSN).get());
    final Map<URI, Value> map = SmartUriAdapter.propertiesToMap(properties);
    final ConvertingCursor<TypedEntity> cursor = smartUriConverter.queryEntity(PERSON_TYPE, map);
    int count = 0;
    while (cursor.hasNext()) {
        final TypedEntity typedEntity = cursor.next();
        System.out.println(typedEntity);
        count++;
    }
    assertEquals(count, 1);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) Value(org.openrdf.model.Value) Property(org.apache.rya.indexing.entity.model.Property) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) Test(org.junit.Test)

Example 2 with TypedEntity

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

the class EntityQueryNode method findBindings.

private List<BindingSet> findBindings(final BindingSet bindingSet) throws QueryEvaluationException {
    final MapBindingSet resultSet = new MapBindingSet();
    try {
        final ConvertingCursor<TypedEntity> entitiesCursor;
        final String subj;
        // If the subject needs to be filled in, check if the subject variable is in the binding set.
        if (subjectIsConstant) {
            // if it is, fetch that value and then fetch the entity for the subject.
            subj = subjectConstant.get();
            entitiesCursor = entities.search(Optional.of(new RyaURI(subj)), type, properties);
        } else {
            entitiesCursor = entities.search(Optional.empty(), type, properties);
        }
        while (entitiesCursor.hasNext()) {
            final TypedEntity typedEntity = entitiesCursor.next();
            final ImmutableCollection<Property> properties = typedEntity.getProperties();
            // ensure properties match and only add properties that are in the statement patterns to the binding set
            for (final RyaURI key : objectVariables.keySet()) {
                final Optional<RyaType> prop = typedEntity.getPropertyValue(new RyaURI(key.getData()));
                if (prop.isPresent()) {
                    final RyaType type = prop.get();
                    final String bindingName = objectVariables.get(key).getName();
                    resultSet.addBinding(bindingName, ValueFactoryImpl.getInstance().createLiteral(type.getData()));
                }
            }
        }
    } catch (final EntityStorageException e) {
        throw new QueryEvaluationException("Failed to evaluate the binding set", e);
    }
    bindingSet.forEach(new Consumer<Binding>() {

        @Override
        public void accept(final Binding binding) {
            resultSet.addBinding(binding);
        }
    });
    final List<BindingSet> list = new ArrayList<>();
    list.add(resultSet);
    return list;
}
Also used : Binding(org.openrdf.query.Binding) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) ArrayList(java.util.ArrayList) RyaType(org.apache.rya.api.domain.RyaType) RyaURI(org.apache.rya.api.domain.RyaURI) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Property(org.apache.rya.indexing.entity.model.Property) EntityStorageException(org.apache.rya.indexing.entity.storage.EntityStorage.EntityStorageException)

Example 3 with TypedEntity

use of org.apache.rya.indexing.entity.model.TypedEntity 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 4 with TypedEntity

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

the class MongoDbSmartUri method queryEntity.

@Override
public ConvertingCursor<TypedEntity> queryEntity(final Type type, final Map<URI, Value> map) throws SmartUriException {
    checkInit();
    // Query it.
    try {
        final Set<Property> properties = SmartUriAdapter.mapToProperties(map);
        final ConvertingCursor<TypedEntity> cursor = entityStorage.search(Optional.empty(), type, properties);
        return cursor;
    } catch (final EntityStorageException e) {
        throw new SmartUriException("Failed to query entity storage", e);
    }
}
Also used : SmartUriException(org.apache.rya.indexing.smarturi.SmartUriException) TypedEntity(org.apache.rya.indexing.entity.model.TypedEntity) Property(org.apache.rya.indexing.entity.model.Property) EntityStorageException(org.apache.rya.indexing.entity.storage.EntityStorage.EntityStorageException)

Example 5 with TypedEntity

use of org.apache.rya.indexing.entity.model.TypedEntity 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

TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)6 RyaURI (org.apache.rya.api.domain.RyaURI)5 Property (org.apache.rya.indexing.entity.model.Property)5 RyaType (org.apache.rya.api.domain.RyaType)3 Entity (org.apache.rya.indexing.entity.model.Entity)3 Type (org.apache.rya.indexing.entity.model.Type)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)2 EntityStorageException (org.apache.rya.indexing.entity.storage.EntityStorage.EntityStorageException)2 LinkedHashSet (java.util.LinkedHashSet)1 TypeStorageException (org.apache.rya.indexing.entity.storage.TypeStorage.TypeStorageException)1 SmartUriException (org.apache.rya.indexing.smarturi.SmartUriException)1 URI (org.openrdf.model.URI)1 Value (org.openrdf.model.Value)1 Binding (org.openrdf.query.Binding)1 BindingSet (org.openrdf.query.BindingSet)1 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)1 MapBindingSet (org.openrdf.query.impl.MapBindingSet)1