Search in sources :

Example 1 with Property

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

the class MongoDbSmartUriIT method testStorage.

@Test
public void testStorage() throws SmartUriException, MalformedQueryException, RuntimeException, QueryEvaluationException {
    smartUriConverter.storeEntity(BOB_ENTITY);
    final String sparql = "SELECT * WHERE { " + "<" + BOB.getData() + "> <" + RDF.TYPE + "> <" + PERSON_TYPE.getId().getData() + "> . " + "<" + BOB.getData() + "> <" + HAS_SSN.getData() + "> ?ssn . " + "<" + BOB.getData() + "> <" + HAS_AGE.getData() + "> ?age . " + "<" + BOB.getData() + "> <" + HAS_WEIGHT.getData() + "> ?weight . " + "<" + BOB.getData() + "> <" + HAS_ADDRESS.getData() + "> ?address . " + "}";
    final StatementPatternCollector spCollector = new StatementPatternCollector();
    new SPARQLParser().parseQuery(sparql, null).getTupleExpr().visit(spCollector);
    final List<StatementPattern> patterns = spCollector.getStatementPatterns();
    final EntityQueryNode entityQueryNode = new EntityQueryNode(PERSON_TYPE, patterns, smartUriConverter.getEntityStorage());
    final QueryBindingSet queryBindingSet = new QueryBindingSet();
    final Property ssnProperty = BOB_ENTITY.lookupTypeProperty(PERSON_TYPE, HAS_SSN).get();
    queryBindingSet.addBinding(HAS_SSN.getData(), RyaToRdfConversions.convertValue(ssnProperty.getValue()));
    final CloseableIteration<BindingSet, QueryEvaluationException> iter = entityQueryNode.evaluate(queryBindingSet);
    int count = 0;
    // These should match what was used in the SPARQL query.
    final List<String> queryParamNames = Lists.newArrayList("ssn", "age", "weight", "address");
    while (iter.hasNext()) {
        final BindingSet bs = iter.next();
        assertTrue(bs.getBindingNames().containsAll(queryParamNames));
        count++;
    }
    assertEquals(count, 1);
}
Also used : StatementPatternCollector(org.openrdf.query.algebra.helpers.StatementPatternCollector) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) EntityQueryNode(org.apache.rya.indexing.entity.query.EntityQueryNode) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 2 with Property

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

the class MongoDbSmartUriIT method testUpdate.

@Test
public void testUpdate() throws SmartUriException {
    smartUriConverter.storeEntity(BOB_ENTITY);
    // New properties to add
    final RyaURI hasNickName = createRyaUri("hasNickName");
    final RyaURI hasWindowOffice = createRyaUri("hasWindowOffice");
    final Entity.Builder builder = Entity.builder(BOB_ENTITY);
    builder.setProperty(PERSON_TYPE_URI, new Property(HAS_AGE, shortRyaType((short) 41)));
    builder.setProperty(PERSON_TYPE_URI, new Property(hasNickName, stringRyaType("Bobby")));
    builder.setProperty(EMPLOYEE_TYPE_URI, new Property(HAS_POSITION_TITLE, stringRyaType("Assistant Regional Manager")));
    builder.setProperty(EMPLOYEE_TYPE_URI, new Property(hasWindowOffice, booleanRyaType(true)));
    builder.setVersion(BOB_ENTITY.getVersion() + 1);
    builder.rebuildSmartUri();
    final Entity newBobEntity = builder.build();
    smartUriConverter.updateEntity(BOB_ENTITY, newBobEntity);
    final Entity resultEntity = smartUriConverter.queryEntity(BOB_ENTITY.getSubject());
    assertEquals(newBobEntity.getVersion(), resultEntity.getVersion());
    assertEquals(newBobEntity.lookupTypeProperty(PERSON_TYPE, HAS_AGE), resultEntity.lookupTypeProperty(PERSON_TYPE, HAS_AGE));
    assertEquals(newBobEntity.lookupTypeProperty(PERSON_TYPE, hasNickName), resultEntity.lookupTypeProperty(PERSON_TYPE, hasNickName));
    assertEquals(newBobEntity.lookupTypeProperty(EMPLOYEE_TYPE, HAS_POSITION_TITLE), resultEntity.lookupTypeProperty(EMPLOYEE_TYPE, HAS_POSITION_TITLE));
    assertEquals(newBobEntity.lookupTypeProperty(EMPLOYEE_TYPE, hasWindowOffice), resultEntity.lookupTypeProperty(EMPLOYEE_TYPE, hasWindowOffice));
    assertEquals(newBobEntity.getSmartUri(), resultEntity.getSmartUri());
    final String resultUriString = resultEntity.getSmartUri().stringValue();
    assertTrue(resultUriString.contains(getRyaUriLocalName(hasWindowOffice)));
    assertTrue(resultUriString.contains(getRyaUriLocalName(hasNickName)));
}
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) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 3 with Property

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

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

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

Aggregations

Property (org.apache.rya.indexing.entity.model.Property)35 RyaURI (org.apache.rya.api.domain.RyaURI)30 RyaType (org.apache.rya.api.domain.RyaType)25 Entity (org.apache.rya.indexing.entity.model.Entity)24 Test (org.junit.Test)21 EntityStorage (org.apache.rya.indexing.entity.storage.EntityStorage)18 TypedEntity (org.apache.rya.indexing.entity.model.TypedEntity)10 MongoEntityStorage (org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage)10 TypeStorage (org.apache.rya.indexing.entity.storage.TypeStorage)9 URI (org.openrdf.model.URI)8 URIImpl (org.openrdf.model.impl.URIImpl)8 MongoTypeStorage (org.apache.rya.indexing.entity.storage.mongo.MongoTypeStorage)7 Value (org.openrdf.model.Value)7 RyaStatement (org.apache.rya.api.domain.RyaStatement)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Type (org.apache.rya.indexing.entity.model.Type)5 BindingSet (org.openrdf.query.BindingSet)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4