use of org.apache.rya.indexing.entity.model.Type in project incubator-rya by apache.
the class MongoEntityIndexIT method setupTypes.
private void setupTypes(MongoEntityIndexer indexer) throws Exception {
final TypeStorage typeStore = indexer.getTypeStorage();
// Add some Types to the storage.
final Type cat = new Type(new RyaURI("urn:cat"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:numLegs")).add(new RyaURI("urn:eye")).add(new RyaURI("urn:species")).build());
final Type dog = new Type(new RyaURI("urn:dog"), ImmutableSet.<RyaURI>builder().add(new RyaURI("urn:numLegs")).add(new RyaURI("urn:eye")).add(new RyaURI("urn:species")).build());
final Type icecream = 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());
final Type person = 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());
typeStore.create(cat);
typeStore.create(dog);
typeStore.create(icecream);
typeStore.create(person);
}
use of org.apache.rya.indexing.entity.model.Type in project incubator-rya by apache.
the class MongoTypeStorageIT method get_nonexisting.
@Test
public void get_nonexisting() throws TypeStorageException {
// Get a Type that hasn't been created.
final TypeStorage storage = new MongoTypeStorage(super.getMongoClient(), RYA_INSTANCE_NAME);
final Optional<Type> storedType = storage.get(new RyaURI("urn:icecream"));
// Verify nothing was returned.
assertFalse(storedType.isPresent());
}
use of org.apache.rya.indexing.entity.model.Type in project incubator-rya by apache.
the class EntityIndexSetProvider method discoverEntities.
private void discoverEntities(final StatementPattern pattern, final List<StatementPattern> unmatched) {
final Var subj = pattern.getSubjectVar();
final String subjStr = subj.getName();
final RyaURI predURI = getPredURI(pattern);
// check to see if current node is type
if (new URIImpl(predURI.getData()).equals(RDF.TYPE)) {
final Var obj = pattern.getObjectVar();
final RyaURI objURI = new RyaURI(obj.getValue().stringValue());
try {
final Optional<Type> optType = typeStorage.get(objURI);
// if is type, fetch type add to subject -> type map
if (optType.isPresent()) {
final Type type = optType.get();
typeMap.put(type, pattern);
subjectTypeMap.put(subjStr, type);
// check unmatched properties, add matches
for (final StatementPattern propertyPattern : unmatched) {
// store sps into the type -> property map
final RyaURI property = getPredURI(propertyPattern);
final Var typeSubVar = getTypeSubject(type);
final Var patternSubVar = propertyPattern.getSubjectVar();
if (type.getPropertyNames().contains(property) && typeSubVar.equals(patternSubVar)) {
typeMap.put(type, propertyPattern);
}
}
}
} catch (final TypeStorageException e) {
e.printStackTrace();
}
} else {
// if not type, check to see if subject is in type map
if (subjectTypeMap.containsKey(subjStr)) {
// if is, check to see if pred is a property of type
final Type type = subjectTypeMap.get(subjStr);
if (type.getPropertyNames().contains(predURI)) {
// if is, add sp to type -> sp map
if (!typeMap.containsKey(type)) {
// each variable can only contain 1 type for now @see:Rya-235?
typeMap.put(type, pattern);
}
} else {
// if not, add to unmatched type
unmatched.add(pattern);
}
} else {
// if not, add to unmatched
unmatched.add(pattern);
}
}
}
use of org.apache.rya.indexing.entity.model.Type 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;
}
use of org.apache.rya.indexing.entity.model.Type 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);
}
Aggregations