use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.
the class EntityDocumentConverter method fromDocument.
@Override
public Entity fromDocument(final Document document) throws DocumentConverterException {
requireNonNull(document);
// Preconditions.
if (!document.containsKey(SUBJECT)) {
throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + SUBJECT + "' field is missing.");
}
if (!document.containsKey(EXPLICIT_TYPE_IDS)) {
throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + EXPLICIT_TYPE_IDS + "' field is missing.");
}
if (!document.containsKey(PROPERTIES)) {
throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + PROPERTIES + "' field is missing.");
}
if (!document.containsKey(VERSION)) {
throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + VERSION + "' field is missing.");
}
if (!document.containsKey(SMART_URI)) {
throw new DocumentConverterException("Could not convert document '" + document + "' because its '" + SMART_URI + "' field is missing.");
}
// Perform the conversion.
final Entity.Builder builder = Entity.builder().setSubject(new RyaURI(document.getString(SUBJECT)));
((List<String>) document.get(EXPLICIT_TYPE_IDS)).stream().forEach(explicitTypeId -> builder.setExplicitType(new RyaURI(explicitTypeId)));
final Document propertiesDoc = (Document) document.get(PROPERTIES);
for (final String typeId : propertiesDoc.keySet()) {
final Document typePropertiesDoc = (Document) propertiesDoc.get(typeId);
for (final String propertyName : typePropertiesDoc.keySet()) {
final String decodedPropertyName = MongoDbSafeKey.decodeKey(propertyName);
final Document value = (Document) typePropertiesDoc.get(propertyName);
final RyaType propertyValue = ryaTypeConverter.fromDocument(value);
builder.setProperty(new RyaURI(typeId), new Property(new RyaURI(decodedPropertyName), propertyValue));
}
}
builder.setVersion(document.getInteger(VERSION));
builder.setSmartUri(new URIImpl(document.getString(SMART_URI)));
return builder.build();
}
use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.
the class MongoEntityIndex2IT method beforeClass.
@Before
public void beforeClass() throws Exception {
optimizer = new EntityIndexOptimizer();
optimizer.setConf(conf);
final TypeStorage typeStorage = optimizer.getTypeStorage();
typeStorage.create(PERSON_TYPE);
final Entity entity = Entity.builder().setSubject(new RyaURI("urn:SSN:111-11-1111")).setExplicitType(RYA_PERSON_TYPE).setProperty(RYA_PERSON_TYPE, new Property(new RyaURI("urn:age"), new RyaType("25"))).setProperty(RYA_PERSON_TYPE, new Property(new RyaURI("urn:eye"), new RyaType("blue"))).setProperty(RYA_PERSON_TYPE, new Property(new RyaURI("urn:name"), new RyaType("bob"))).build();
entityStorage = optimizer.getEntityStorage();
entityStorage.create(entity);
}
use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.
the class EntityQueryNodeIT method evaluate_variableSubject.
@Test
public void evaluate_variableSubject() throws Exception {
final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), "testDB");
final ValueFactory vf = ValueFactoryImpl.getInstance();
RyaURI subject = new RyaURI("urn:SSN:111-11-1111");
final Entity bob = 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();
subject = new RyaURI("urn:SSN:222-22-2222");
final Entity fred = Entity.builder().setSubject(subject).setExplicitType(PERSON_TYPE.getId()).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:age"), RdfToRyaConversions.convertLiteral(vf.createLiteral(25)))).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:eye"), RdfToRyaConversions.convertLiteral(vf.createLiteral("brown")))).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:name"), RdfToRyaConversions.convertLiteral(vf.createLiteral("Fred")))).build();
storage.create(bob);
storage.create(fred);
// A set of patterns that match a sepecific Entity subject.
final List<StatementPattern> patterns = getSPs("SELECT * WHERE { " + "?ssn <" + RDF.TYPE + "> <urn:person> ." + "?ssn <urn:age> ?age . " + "?ssn <urn:eye> ?eye . " + "?ssn <urn:name> ?name . " + "}");
final EntityQueryNode node = new EntityQueryNode(PERSON_TYPE, patterns, storage);
final CloseableIteration<BindingSet, QueryEvaluationException> rez = node.evaluate(new MapBindingSet());
final List<BindingSet> expectedBindings = new ArrayList<>();
final MapBindingSet expectedBob = new MapBindingSet();
expectedBob.addBinding("age", vf.createLiteral("20"));
expectedBob.addBinding("eye", vf.createLiteral("blue"));
expectedBob.addBinding("name", vf.createLiteral("Bob"));
final MapBindingSet expectedFred = new MapBindingSet();
expectedFred.addBinding("age", vf.createLiteral("25"));
expectedFred.addBinding("eye", vf.createLiteral("brown"));
expectedFred.addBinding("name", vf.createLiteral("Fred"));
expectedBindings.add(expectedBob);
expectedBindings.add(expectedFred);
while (rez.hasNext()) {
final BindingSet bs = rez.next();
assertTrue(expectedBindings.contains(bs));
}
}
use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.
the class EntityQueryNodeIT method evaluate_constantSubject.
@Test
public void evaluate_constantSubject() 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> ?eye . " + "<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("eye", vf.createLiteral("blue"));
expected.addBinding("name", vf.createLiteral("Bob"));
while (rez.hasNext()) {
assertEquals(expected, rez.next());
break;
}
}
use of org.apache.rya.indexing.entity.model.Property in project incubator-rya by apache.
the class MongoEntityStorageIT method search_byFields.
@Test
public void search_byFields() throws Exception {
final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
// A Type that defines a Person.
final Type personType = new Type(new RyaURI("urn:person"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:name")).add(new RyaURI("urn:age")).add(new RyaURI("urn:eye")).build());
// Some Person typed objects.
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();
final Entity charlie = Entity.builder().setSubject(new RyaURI("urn:SSN/333-33-3333")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "Charlie"))).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 david = Entity.builder().setSubject(new RyaURI("urn:SSN/444-44-4444")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "David"))).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, "brown"))).build();
final Entity eve = Entity.builder().setSubject(new RyaURI("urn:SSN/555-55-5555")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "Eve"))).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:age"), new RyaType(XMLSchema.INT, "30"))).build();
final Entity frank = Entity.builder().setSubject(new RyaURI("urn:SSN/666-66-6666")).setExplicitType(new RyaURI("urn:person")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "Frank"))).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:eye"), new RyaType(XMLSchema.STRING, "blue"))).setProperty(new RyaURI("urn:someOtherType"), new Property(new RyaURI("urn:age"), new RyaType(XMLSchema.INT, "30"))).build();
final Entity george = Entity.builder().setSubject(new RyaURI("urn:SSN/777-77-7777")).setProperty(new RyaURI("urn:person"), new Property(new RyaURI("urn:name"), new RyaType(XMLSchema.STRING, "George"))).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();
// Create the objects in the storage.
storage.create(alice);
storage.create(bob);
storage.create(charlie);
storage.create(david);
storage.create(eve);
storage.create(frank);
storage.create(george);
// Search for all people who are 30 and have blue eyes.
final Set<TypedEntity> objects = new HashSet<>();
final Set<Property> searchValues = Sets.newHashSet(new Property(new RyaURI("urn:eye"), new RyaType(XMLSchema.STRING, "blue")), new Property(new RyaURI("urn:age"), new RyaType(XMLSchema.INT, "30")));
try (final ConvertingCursor<TypedEntity> it = storage.search(Optional.empty(), personType, searchValues)) {
while (it.hasNext()) {
objects.add(it.next());
}
}
// Verify the expected results were returned.
assertEquals(2, objects.size());
assertTrue(objects.contains(alice.makeTypedEntity(new RyaURI("urn:person")).get()));
assertTrue(objects.contains(charlie.makeTypedEntity(new RyaURI("urn:person")).get()));
}
Aggregations