use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.
the class StatementMetadataNode method verifyHasCorrectTypePattern.
/**
* Verifies StatementPatterns define a reified pattern with associated
* metadata properties.
*
* @param patterns
* - The patterns to check. (not null)
* @throws IllegalStateException
* No Type or the wrong Type is specified by the patterns.
*/
public static boolean verifyHasCorrectTypePattern(final Collection<StatementPattern> patterns) throws IllegalStateException {
requireNonNull(patterns);
boolean subjFound = false;
boolean objFound = false;
boolean predFound = false;
boolean statementFound = false;
boolean valid = true;
boolean contextSet = false;
Var context = null;
for (final StatementPattern pattern : patterns) {
final RyaURI predicate = new RyaURI(pattern.getPredicateVar().getValue().toString());
if (!contextSet) {
context = pattern.getContextVar();
contextSet = true;
} else {
if (context != null && !context.equals(pattern.getContextVar())) {
return false;
}
}
if (predicate.equals(TYPE_ID_URI)) {
final RyaURI statementID = new RyaURI(pattern.getObjectVar().getValue().stringValue());
if (statementID.equals(STATEMENT_ID_URI)) {
statementFound = true;
} else {
// contains more than one Statement containing TYPE_ID_URI
// as Predicate
// and STATEMENT_ID_URI as Object
valid = false;
}
}
if (predicate.equals(SUBJ_ID_URI)) {
if (!subjFound) {
subjFound = true;
} else {
// contains more than Subject SP
valid = false;
}
}
if (predicate.equals(PRED_ID_URI)) {
if (!predFound) {
predFound = true;
} else {
// contains more than one Predicate SP
valid = false;
}
}
if (predicate.equals(OBJ_ID_URI)) {
if (!objFound) {
objFound = true;
} else {
// contains more than one Object SP
valid = false;
}
}
}
return valid && statementFound && subjFound && predFound && objFound;
}
use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.
the class StatementMetadataNode method setStatementPatternAndProperties.
/**
* Constructs a {@link StatementPattern} from the StatementPatterns
* representing a reified query. This StatementPattern has as a subject, the
* object of the StatementPattern containing the predicate
* {@link RDF#SUBJECT}. This StatementPattern has as predicate, the object
* of the StatementPattern containing the predicate {@link RDF#PREDICATE}.
* This StatementPattern has as an object, the object of the
* StatementPattern containing the predicate {@link RDF#OBJECT}. This method
* also builds a map between all predicates that are not of the above type
* and the object {@link Var}s they are associated with. This map contains
* the user specified metadata properties and is used for comparison with
* the metadata properties extracted from RyaStatements passed back by the
* {@link RyaQueryEngine}.
*
* @param patterns
* - collection of patterns representing a reified query
*/
private void setStatementPatternAndProperties(Collection<StatementPattern> patterns) {
StatementPattern sp = new StatementPattern();
Map<RyaURI, Var> properties = new HashMap<>();
for (final StatementPattern pattern : patterns) {
final RyaURI predicate = new RyaURI(pattern.getPredicateVar().getValue().toString());
if (!uriList.contains(predicate)) {
Var objVar = pattern.getObjectVar();
properties.put(predicate, objVar);
continue;
}
if (predicate.equals(SUBJ_ID_URI)) {
sp.setContextVar(pattern.getContextVar());
sp.setSubjectVar(pattern.getObjectVar());
}
if (predicate.equals(PRED_ID_URI)) {
sp.setPredicateVar(pattern.getObjectVar());
}
if (predicate.equals(OBJ_ID_URI)) {
sp.setObjectVar(pattern.getObjectVar());
}
}
this.statement = sp;
this.properties = properties;
}
use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.
the class SmartUriAdapter method serializeUriEntity.
/**
* Serializes an {@link Entity} into a Smart {@link URI}.
* @param entity the {@link Entity} to serialize into a Smart URI.
* @return the Smart {@link URI}.
* @throws SmartUriException
*/
public static URI serializeUriEntity(final Entity entity) throws SmartUriException {
final Map<URI, Value> objectMap = new LinkedHashMap<>();
// Adds the entity's types to the Smart URI
final List<RyaURI> typeIds = entity.getExplicitTypeIds();
final Map<RyaURI, String> ryaTypeMap = createTypeMap(typeIds);
final URI ryaTypeMapUri = createTypeMapUri(typeIds);
final RyaType valueRyaType = new RyaType(XMLSchema.ANYURI, ryaTypeMapUri.stringValue());
final Value typeValue = RyaToRdfConversions.convertValue(valueRyaType);
objectMap.put(RYA_TYPES_URI, typeValue);
final RyaURI subject = entity.getSubject();
final Map<RyaURI, ImmutableMap<RyaURI, Property>> typeMap = entity.getProperties();
for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> typeEntry : typeMap.entrySet()) {
final RyaURI type = typeEntry.getKey();
String typeShortName = ryaTypeMap.get(type);
typeShortName = typeShortName != null ? typeShortName + "." : "";
final ImmutableMap<RyaURI, Property> typeProperties = typeEntry.getValue();
for (final Entry<RyaURI, Property> properties : typeProperties.entrySet()) {
final RyaURI key = properties.getKey();
final Property property = properties.getValue();
final String valueString = property.getValue().getData();
final RyaType ryaType = property.getValue();
// final RyaType ryaType = new RyaType(new URIImpl(key.getData()), valueString);
final Value value = RyaToRdfConversions.convertValue(ryaType);
String formattedKey = key.getData();
if (StringUtils.isNotBlank(typeShortName)) {
formattedKey = addTypePrefixToUri(formattedKey, typeShortName);
}
final URI uri = new URIImpl(formattedKey);
objectMap.put(uri, value);
}
}
return serializeUri(subject, objectMap);
}
use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.
the class SmartUriAdapter method convertUriToTypeMap.
private static Map<RyaURI, String> convertUriToTypeMap(final URI typeMapUri) throws SmartUriException {
final Map<RyaURI, String> map = new HashMap<>();
java.net.URI uri;
try {
final URIBuilder uriBuilder = new URIBuilder(typeMapUri.stringValue());
uri = uriBuilder.build();
} catch (final URISyntaxException e) {
throw new SmartUriException("Unable to parse Rya type map in Smart URI", e);
}
final List<NameValuePair> params = URLEncodedUtils.parse(uri, Charsets.UTF_8.name());
for (final NameValuePair param : params) {
final String name = param.getName();
final String value = param.getValue();
final RyaURI type = new RyaURI(name);
map.put(type, value);
}
return map;
}
use of org.apache.rya.api.domain.RyaURI in project incubator-rya by apache.
the class SmartUriAdapter method entityToValueMap.
public static Map<URI, Value> entityToValueMap(final Entity entity) {
final Map<URI, Value> map = new LinkedHashMap<>();
for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> entry : entity.getProperties().entrySet()) {
for (final Entry<RyaURI, Property> property : entry.getValue().entrySet()) {
final RyaURI propertyKey = property.getKey();
final URI uri = new URIImpl(propertyKey.getData());
final Property propertyValue = property.getValue();
final Value value = RyaToRdfConversions.convertValue(propertyValue.getValue());
map.put(uri, value);
}
}
return map;
}
Aggregations