@Override public boolean apply(Bindings bindings) { NODE lhs = (NODE) expr.getArg(0).accept(QueryRDFVisitor.this, bindings); NODE rhs = (NODE) expr.getArg(1).accept(QueryRDFVisitor.this, bindings); if (lhs == null || rhs == null) { return false; } else if (op == Ops.STARTS_WITH) { return lhs.getValue().startsWith(rhs.getValue()); } else if (op == Ops.STARTS_WITH_IC) { return lhs.getValue().toLowerCase().startsWith(rhs.getValue().toLowerCase()); } else if (op == Ops.ENDS_WITH) { return lhs.getValue().endsWith(rhs.getValue()); } else if (op == Ops.ENDS_WITH_IC) { return lhs.getValue().toLowerCase().endsWith(rhs.getValue().toLowerCase()); } else if (op == Ops.STRING_CONTAINS) { return lhs.getValue().contains(rhs.getValue()); } else if (op == Ops.STRING_CONTAINS_IC) { return lhs.getValue().toLowerCase().contains(rhs.getValue().toLowerCase()); } else { throw new IllegalArgumentException(op.toString()); } } };
@Override public boolean apply(Bindings bindings) { NODE lhs = (NODE) expr.getArg(0).accept(QueryRDFVisitor.this, bindings); NODE rhs = (NODE) expr.getArg(1).accept(QueryRDFVisitor.this, bindings); if (lhs != null && rhs != null) { Pattern pattern; Map<String, Pattern> cache = op == Ops.MATCHES ? patterns : caseInsensitivePatterns; pattern = cache.get(rhs.getValue()); if (pattern == null) { pattern = Pattern.compile(rhs.getValue(), op == Ops.MATCHES ? 0 : Pattern.CASE_INSENSITIVE); cache.put(rhs.getValue(), pattern); } return pattern.matcher(lhs.getValue()).matches(); } else { return false; } } };
@Override public boolean apply(Bindings bindings) { NODE lhs = (NODE) expr.getArg(0).accept(QueryRDFVisitor.this, bindings); NODE rhs = (NODE) expr.getArg(1).accept(QueryRDFVisitor.this, bindings); if (lhs != null && rhs != null) { Pattern pattern; Map<String, Pattern> cache = patterns; pattern = cache.get(rhs.getValue()); if (pattern == null) { String regex = rhs.getValue().replace("%", ".*").replaceAll("_", "."); pattern = Pattern.compile(regex); cache.put(rhs.getValue(), pattern); } return pattern.matcher(lhs.getValue()).matches(); } else { return false; } } };
@SuppressWarnings("unchecked") private Object convertEnum(NODE value, Class<?> targetClass) { if (value instanceof UID) { return Enum.valueOf((Class<? extends Enum>) targetClass, ((UID) value).ln()); } else if (value instanceof LIT) { return Enum.valueOf((Class<? extends Enum>) targetClass, value.getValue()); } else { throw new BindException("Cannot bind BNode into enum"); } }
@Override public boolean apply(Bindings bindings) { NODE lhs = (NODE) expr.getArg(0).accept(QueryRDFVisitor.this, bindings); return lhs != null ? lhs.getValue().isEmpty() : false; } };
@Override public boolean apply(Bindings bindings) { NODE lhs = (NODE) expr.getArg(0).accept(QueryRDFVisitor.this, bindings); NODE rhs = (NODE) expr.getArg(1).accept(QueryRDFVisitor.this, bindings); if (lhs != null && rhs != null) { return lhs.getValue().equalsIgnoreCase(rhs.getValue()); } else { return lhs == rhs; } } };
protected void appendAsString(Expression<?> expr) { Object constant; if (expr instanceof Constant<?>) { constant = ((Constant<?>) expr).getConstant(); } else if (expr instanceof ParamExpression<?> && metadata != null) { if (metadata.getParams().containsKey(expr)) { constant = metadata.getParams().get(expr); } else { constant = ((ParamExpression<?>) expr).getName(); } } else { constant = expr.toString(); } if (constant instanceof NODE) { append(((NODE) constant).getValue()); } else { append(constant.toString()); } }
@Override public LID getLID(ID id) { STMT stmt = find(id, CORE.localId, null); String lid; if (stmt != null) { lid = stmt.getObject().getValue(); } else { lid = String.valueOf(connection.getNextLocalId()); add(id, CORE.localId, new LIT(lid)); } return new LID(lid); }
@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 long count() { TupleQuery query = createTupleQuery(true); if (!connection.getQueryOptions().isCountViaAggregation()) { long counter = 0; CloseableIterator<Map<String, NODE>> tuples = query.getTuples(); try { while (tuples.hasNext()) { counter++; tuples.next(); } } finally { tuples.close(); } return counter; } else { List<Map<String, NODE>> results = IteratorAdapter.asList(query.getTuples()); NODE result = results.get(0).values().iterator().next(); if (result.isLiteral()) { return Long.valueOf(result.getValue()); } else { throw new IllegalArgumentException(result.toString()); } } }
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()); } }
@SuppressWarnings("unchecked") private <RT> RT getAsProjectionValue(NODE node, Class<RT> type) { if (node.isResource()) { if (type.equals(String.class)) { // TODO : always return LID ? return (RT) session.getLID(node.asResource()).getId(); } else { return session.get(type, node.asResource()); } } else { return converterRegistry.fromString(node.getValue(), type); } }
NODE lhs = (NODE) expr.getArg(0).accept(this, bindings); NODE rhs = (NODE) expr.getArg(1).accept(this, bindings); return new LIT(lhs.getValue() + rhs.getValue()); return new LIT(lhs.getValue().toLowerCase()); return new LIT(lhs.getValue().toUpperCase()); return new LIT(lhs.getValue().trim()); return new LIT(lhs.getValue().substring(Integer.parseInt(rhs.getValue()))); NODE arg1 = (NODE) expr.getArg(1).accept(this, bindings); NODE arg2 = (NODE) expr.getArg(2).accept(this, bindings); return new LIT(arg0.getValue().substring( Integer.parseInt(arg1.getValue()), Integer.parseInt(arg2.getValue()))); return new LIT(String.valueOf(lhs.getValue().charAt(Integer.parseInt(rhs.getValue())))); return (lhs.isResource()) ? new LIT(lhs.getValue()) : lhs;
convertedValue = configuration.getConverterRegistry().fromString(value.getValue(), targetClass);
appendEscaped(stmt.getObject().getValue()); writer.append("</" + prefix + ":" + stmt.getPredicate().ln() + ">\n"); } else if (stmt.getObject().isBNode()) { writer.append(" rdf:nodeID=\"" + stmt.getObject().getValue() + "\"/>\n"); } else { writer.append(" rdf:resource=\"" + shorten(stmt.getObject().asURI()) + "\"/>\n");
@Test public void VirtualProperties() { MiniRepository repository = new MiniRepository(); Session session = SessionUtil.openSession(repository, Party.class, Person.class); // Persistence session.save(new Person("John", "Doe")); List<STMT> statements = IteratorAdapter.asList(repository .findStatements(null, new UID(TEST.NS, "displayName"), null, null, false)); assertEquals(1, statements.size()); STMT stmt = statements.get(0); assertEquals("John Doe", stmt.getObject().getValue()); // Retrieval session = SessionUtil.openSession(repository, Party.class, Person.class); Person person = session.findInstances(Person.class).get(0); assertEquals("John Doe", person.getDisplayName()); }