use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.
the class StarQuery method init.
// called in constructor after nodes set
// assumes nodes and nodeColumnCond are same size
private void init() throws RyaTypeResolverException {
commonVar = this.getCommonVar(nodes);
if (!commonVar.isConstant()) {
commonVarName = commonVar.getName();
} else {
commonVarName = commonVar.getName().substring(7);
}
if (hasContext()) {
final RyaURI ctxtURI = (RyaURI) RdfToRyaConversions.convertValue(context.getValue());
contextURI = ctxtURI.getData();
}
for (int i = 0; i < nodes.size(); i++) {
nodeColumnCond[i] = nodeToTextColumn(nodes.get(i), i);
}
}
use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.
the class StarQuery method nodeToTextColumn.
// assumes nodes forms valid star query with only one common variable
// assumes nodes and commonVar has been set
private TextColumn nodeToTextColumn(final StatementPattern node, final int i) throws RyaTypeResolverException {
final RyaContext rc = RyaContext.getInstance();
final Var subjVar = node.getSubjectVar();
final Var predVar = node.getPredicateVar();
final Var objVar = node.getObjectVar();
final RyaURI predURI = (RyaURI) RdfToRyaConversions.convertValue(node.getPredicateVar().getValue());
// assumes StatementPattern contains at least on variable
if (subjVar.isConstant()) {
if (commonVarConstant()) {
varPos.put(objVar.getName(), i);
return new TextColumn(new Text(predURI.getData()), new Text("object"));
} else {
return new TextColumn(new Text(predURI.getData()), new Text("subject" + "\u0000" + subjVar.getValue().stringValue()));
}
} else if (objVar.isConstant()) {
if (commonVarConstant()) {
varPos.put(subjVar.getName(), i);
return new TextColumn(new Text(predURI.getData()), new Text("subject"));
} else {
isCommonVarURI = true;
final RyaType objType = RdfToRyaConversions.convertValue(objVar.getValue());
final byte[][] b1 = rc.serializeType(objType);
final byte[] b2 = Bytes.concat("object".getBytes(StandardCharsets.UTF_8), "\u0000".getBytes(StandardCharsets.UTF_8), b1[0], b1[1]);
return new TextColumn(new Text(predURI.getData()), new Text(b2));
}
} else {
if (subjVar.getName().equals(commonVarName)) {
isCommonVarURI = true;
varPos.put(objVar.getName(), i);
final TextColumn tc = new TextColumn(new Text(predURI.getData()), new Text("object"));
tc.setIsPrefix(true);
return tc;
} else {
varPos.put(subjVar.getName(), i);
final TextColumn tc = new TextColumn(new Text(predURI.getData()), new Text("subject"));
tc.setIsPrefix(true);
return tc;
}
}
}
use of org.apache.rya.api.domain.RyaURI 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.api.domain.RyaURI 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.api.domain.RyaURI in project incubator-rya by apache.
the class EntityQueryNode method verifyAllPredicatesPartOfType.
/**
* Verify all of the patterns have predicates that match one of the Type's property names.
*
* @param type - The Type the patterns match. (not null)
* @param patterns - The patterns to check.
* @throws IllegalStateException If any of the non-type defining Statement Patterns
* contain a predicate that does not match one of the Type's property names.
*/
private static void verifyAllPredicatesPartOfType(final Type type, final Collection<StatementPattern> patterns) throws IllegalStateException {
requireNonNull(type);
requireNonNull(patterns);
for (final StatementPattern pattern : patterns) {
// Skip TYPE patterns.
final RyaURI predicate = new RyaURI(pattern.getPredicateVar().getValue().toString());
if (predicate.equals(TYPE_ID_URI)) {
continue;
}
if (!type.getPropertyNames().contains(predicate)) {
throw new IllegalStateException("The Predicate of a Statement Pattern must be a property name for the Type. " + "Type ID: '" + type.getId().getData() + "' Pattern: " + pattern);
}
}
}
Aggregations