public boolean isString() { return datatype.equals(XSD.stringType); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } else if (obj instanceof STMT) { STMT other = (STMT) obj; if (!subject.equals(other.subject)) { return false; } if (!predicate.equals(other.predicate)) { return false; } if (!object.equals(other.object)) { return false; } if (context == null) { if (other.context != null) { return false; } } else if (!context.equals(other.context)) { return false; } if (asserted != other.asserted) { return false; } return true; } else { return false; } }
protected void appendPredicate(UID uid) throws IOException { if (uid.equals(RDF.type)) { appendable.append("a"); } else { append(uid); } }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } else if (obj == null) { return false; } else if (!(obj instanceof LIT)) { return false; } LIT other = (LIT) obj; if (!value.equals(other.value)) { return false; } if (lang == null) { if (other.lang != null) { return false; } } else if (!lang.equals(other.lang)) { return false; } if (!datatype.equals(other.datatype)) { return false; } return true; }
private Set<NODE> findValues(UID predicate, Multimap<UID, STMT> properties, @Nullable UID context, boolean inv) { Set<NODE> nodes = new HashSet<NODE>(); if (properties.containsKey(predicate)) { for (STMT stmt : properties.get(predicate)) { if (context == null || context.equals(stmt.getContext())) { nodes.add(inv ? stmt.getSubject() : stmt.getObject()); } } } return nodes; }
public static boolean matches(STMT stmt, ID subject, UID predicate, NODE object, UID context, boolean includeInferred) { return // Subject match (subject == null || subject.equals(stmt.getSubject())) && // Predicate match (predicate == null || predicate.equals(stmt.getPredicate())) && // Object match (object == null || object.equals(stmt.getObject())) && // Context match (context == null || context.equals(stmt.getContext())) && // Asserted or includeInferred statement (includeInferred || stmt.isAsserted()); }
protected void append(LIT lit) throws IOException { String val = lit.getValue(); if (val.indexOf('\n') > 0 || val.indexOf('\r') > 0 || val.indexOf('\t') > 0) { appendable.append("\"\"\""); appendable.append(TurtleUtil.encodeLongString(val)); appendable.append("\"\"\""); } else { appendable.append("\""); appendable.append(TurtleUtil.encodeString(val)); appendable.append("\""); } if (lit.getLang() != null) { appendable.append("@").append(LocaleUtil.toLang(lit.getLang())); } else if (!lit.getDatatype().equals(RDF.text)) { appendable.append("^^"); append(lit.getDatatype()); } }
private Object convertValue(NODE node, Class<?> targetClass) { UID targetType = configuration.getConverterRegistry().getDatatype(targetClass); if (targetClass.isAssignableFrom(node.getClass())) { return node; } else if (targetType != null && node.isLiteral()) { // TODO : make sure this works also with untyped literals etc if (((LIT) node).getDatatype().equals(targetType)) { return configuration.getConverterRegistry().fromString(node.getValue(), targetClass); } else { throw new IllegalArgumentException("Literal " + node + " is not of type " + targetType); } } else if (targetType == null && node.isURI()) { return get(targetClass, node.asURI()); } else { throw new IllegalArgumentException("Node " + node + " could not be converted to " + targetClass.getName()); } }
private List<STMT> findStatements(@Nullable ID subject, @Nullable UID predicate, @Nullable NODE object, @Nullable UID context, boolean includeInferred) { if (logger.isDebugEnabled()) { logger.debug("findStatements " + subject + " " + predicate + " " + object + " " + context); } // rdf type inference if (RDF.type.equals(predicate) && subject == null && object != null && connection.getInferenceOptions().subClassOf()) { Collection<UID> types = ontology.getSubtypes(object.asURI()); if (types.size() > 1) { RDFQuery query = new RDFQueryImpl(connection); CloseableIterator<STMT> stmts = query.where( Blocks.SPOC, QNODE.o.in(types)) .set(QNODE.p, predicate) .construct(Blocks.SPOC); return IteratorAdapter.asList(stmts); } } return IteratorAdapter.asList(connection.findStatements(subject, predicate, object, context, includeInferred)); }
@Override public void handle(STMT stmt) { try { if (last == null || !last.getSubject().equals(stmt.getSubject())) { if (last != null) { appendable.append(" .\n"); } append(stmt.getSubject()); appendable.append(" "); appendPredicate(stmt.getPredicate()); appendable.append(" "); } else if (!last.getPredicate().equals(stmt.getPredicate())) { appendable.append(" ; "); appendPredicate(stmt.getPredicate()); appendable.append(" "); } else { appendable.append(" , "); } append(stmt.getObject()); last = stmt; } catch (IOException e) { throw new RepositoryException(e); } }
private void deleteResource(ID subject, @Nullable UID context) { // Delete own properties for (STMT statement : findStatements(subject, null, null, context, false)) { recordRemoveStatement(statement); NODE object = statement.getObject(); if (object.isResource() && !statement.getPredicate().equals(RDF.type)) { removeList((ID) object, context); removeContainer((ID) object, context); } } // Delete references for (STMT statement : findStatements(null, null, subject, context, false)) { recordRemoveStatement(statement); } // Remove from primary cache Collection<Object> instances = instanceCache.removeAll(subject); if (instances != null) { for (Object obj : instances) { resourceCache.remove(obj); } } }
private void removeListInternal(ID node, UID context) { for (STMT statement : findStatements(node, null, null, context, false)) { recordRemoveStatement(statement); NODE object = statement.getObject(); // Remove rdf:rest if (RDF.rest.equals(statement.getPredicate()) && object.isResource()) { removeListInternal((ID) object, context); } } }
@Override public int compare(NODE o1, NODE o2) { if (o1 == null) { return o2 == null ? 0 : -1; } else if (o2 == null) { return 1; } // node type if (o1.getNodeType() != o2.getNodeType()) { return o1.getNodeType().compareTo(o2.getNodeType()); } // value if (!o1.getValue().equals(o2.getValue())) { return o1.getValue().compareTo(o2.getValue()); } if (o1.isLiteral() && o2.isLiteral()) { LIT l1 = o1.asLiteral(); LIT l2 = o2.asLiteral(); // datatype if (!l1.getDatatype().equals(l2.getDatatype())) { return compare(l1.getDatatype(), l2.getDatatype()); } // locale Locale loc1 = l1.getLang() == null ? new Locale("") : l1.getLang(); Locale loc2 = l2.getLang() == null ? new Locale("") : l2.getLang(); return loc1.toString().compareTo(loc2.toString()); } return 0; }
@Override public Literal getLiteral(LIT lit) { Literal literal = literalCache.get(lit); if (literal == null){ if (lit.isText()) { Locale lang = lit.getLang(); if (lang.equals(Locale.ROOT)) { literal = vf.createLiteral(lit.getValue()); } else { literal = vf.createLiteral(lit.getValue(), LocaleUtil.toLang(lang)); } } else if (lit.isString()) { literal = vf.createLiteral(lit.getValue(), getURI(XSD.stringType)); } else if (lit.getDatatype().equals(RDF.text)){ literal = vf.createLiteral(lit.getValue()); } else { literal = vf.createLiteral(lit.getValue(), getURI(lit.getDatatype())); } literalCache.put(lit, literal); } return literal; }
@Override public Literal getLiteral(LIT lit) { Literal literal = literalCache.get(lit); if (literal == null) { if (lit.isText()) { Locale lang = lit.getLang(); if (lang.equals(Locale.ROOT)) { literal = vf.createLiteral(lit.getValue()); } else { literal = vf.createLiteral(lit.getValue(), LocaleUtil.toLang(lang)); } } else if (lit.isString()) { literal = vf.createLiteral(lit.getValue(), getURI(XSD.stringType)); } else if (lit.getDatatype().equals(RDF.text)) { literal = vf.createLiteral(lit.getValue()); } else { literal = vf.createLiteral(lit.getValue(), getURI(lit.getDatatype())); } literalCache.put(lit, literal); } return literal; }
@Override public Literal getLiteral(LIT lit) { Literal literal = literalCache.get(lit); if (literal == null) { if (lit.isText()) { Locale lang = lit.getLang(); if (lang.equals(Locale.ROOT)) { literal = vf.createLiteral(lit.getValue()); } else { literal = vf.createLiteral(lit.getValue(), LocaleUtil.toLang(lang)); } } else if (lit.isString()) { literal = vf.createLiteral(lit.getValue(), getURI(XSD.stringType)); } else if (lit.getDatatype().equals(RDF.text)) { literal = vf.createLiteral(lit.getValue()); } else { literal = vf.createLiteral(lit.getValue(), getURI(lit.getDatatype())); } literalCache.put(lit, literal); } return literal; }