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);
}
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)));
}
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);
}
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;
}
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);
}
}
Aggregations