/** * Apply the selector to the context node passed as argument and return the collection * of selected nodes in appropriate order. * * @param context the node where to start the selection * @param path the path leading to but not including the context node in the current evaluation of LDPath; may be null, * in which case path tracking is disabled * @param resultPaths a map where each of the result nodes maps to a path leading to the result node in the LDPath evaluation; * if null, path tracking is disabled and the path argument is ignored * @return the collection of selected nodes */ @Override public Collection<Node> select(RDFBackend<Node> rdfBackend, Node context, List<Node> path, Map<Node, List<Node>> resultPaths) { return Collections.singleton(rdfBackend.createLiteral(constant)); }
/** * Apply the selector to the context node passed as argument and return the collection * of selected nodes in appropriate order. * * @param context the node where to start the selection * @param path the path leading to but not including the context node in the current evaluation of LDPath; may be null, * in which case path tracking is disabled * @param resultPaths a map where each of the result nodes maps to a path leading to the result node in the LDPath evaluation; * if null, path tracking is disabled and the path argument is ignored * @return the collection of selected nodes */ @Override public Collection<Node> select(RDFBackend<Node> rdfBackend, Node context, List<Node> path, Map<Node, List<Node>> resultPaths) { return Collections.singleton(rdfBackend.createLiteral(constant)); }
@Override @SafeVarargs public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { LinkedList<Node> result = new LinkedList<>(); for (Collection<Node> coll : args) { final Node intLit = backend.createLiteral(String.valueOf(coll.size()), null, dataType); result.add(intLit); } return result; }
@Override @SafeVarargs public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { LinkedList<Node> result = new LinkedList<>(); for (Collection<Node> coll : args) { final Node intLit = backend.createLiteral(String.valueOf(coll.size()), null, dataType); result.add(intLit); } return result; }
protected Node calc(RDFBackend<Node> backend, Node node) { /* SUM */ try { Double val = doubleTransformer.transform(backend, node, null); return backend.createLiteral(String.valueOf(Math.round(val)), null, intType); } catch (IllegalArgumentException e) { return null; } }
protected Node calc(RDFBackend<Node> backend, Collection<Node> arg) { /* SUM */ Double d = 0d; for (Node n : arg) { try { Double val = doubleTransformer.transform(backend, n, null); d += val; } catch (IllegalArgumentException e) { // we just ignore non-numeric nodes } } return backend.createLiteral(String.valueOf(d), null, doubleType); }
protected Node calc(RDFBackend<Node> backend, Node node) { /* SUM */ try { Double val = doubleTransformer.transform(backend, node, null); return backend.createLiteral(String.valueOf(Math.round(val)), null, intType); } catch (IllegalArgumentException e) { return null; } }
protected Node calc(RDFBackend<Node> backend, Collection<Node> arg) { /* SUM */ Double d = 0d; for (Node n : arg) { try { Double val = doubleTransformer.transform(backend, n, null); d += val; } catch (IllegalArgumentException e) { // we just ignore non-numeric nodes } } return backend.createLiteral(String.valueOf(d), null, doubleType); }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { Preconditions.checkArgument(args.length == 1, "Check usage: " + getSignature()); LinkedList<Node> result = new LinkedList<>(); for (Node node : args[0]) { final String stringValue = backend.stringValue(node); final int c = stringValue.length(); result.add(backend.createLiteral(String.valueOf(c), null, dataType)); } return result; }
/** * Apply the function to the list of nodes passed as arguments and return the result as type T. * Throws IllegalArgumentException if the function cannot be applied to the nodes passed as argument * or the number of arguments is not correct. * * @param args a nested list of KiWiNodes * @return */ @Override @SafeVarargs public final Collection<Node> apply(RDFBackend<Node> rdfBackend, Node context, Collection<Node>... args) throws IllegalArgumentException { Iterator<Node> it = org.apache.marmotta.ldpath.util.Collections.iterator(args); StringBuilder result = new StringBuilder(); while (it.hasNext()) { result.append(transformer.transform(rdfBackend,it.next(), null)); } return Collections.singleton(rdfBackend.createLiteral(result.toString())); }
/** * Apply the function to the list of nodes passed as arguments and return the result as type T. * Throws IllegalArgumentException if the function cannot be applied to the nodes passed as argument * or the number of arguments is not correct. * * @param args a nested list of KiWiNodes * @return */ @Override @SafeVarargs public final Collection<Node> apply(RDFBackend<Node> rdfBackend, Node context, Collection<Node>... args) throws IllegalArgumentException { Iterator<Node> it = org.apache.marmotta.ldpath.util.Collections.iterator(args); StringBuilder result = new StringBuilder(); while (it.hasNext()) { result.append(transformer.transform(rdfBackend,it.next(), null)); } return Collections.singleton(rdfBackend.createLiteral(result.toString())); }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { try { if (args.length != 2) { throw new IllegalArgumentException("LdPath function " + getLocalName() + " requires 2 arguments"); } if (args[1].size() != 1) { throw new IllegalArgumentException("len argument must be a single literal for function " + getLocalName()); } final Collection<Node> nodes = args[0]; final int length = Math.max(backend.intValue(args[1].iterator().next()), 0); final Collection<Node> result = new LinkedList<>(); for (Node node : nodes) { final String str = backend.stringValue(node); result.add(backend.createLiteral(str.substring(Math.max(0, str.length() - length)))); } return result; } catch (NumberFormatException | ArithmeticException nfe) { throw new IllegalArgumentException(nfe); } }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { Preconditions.checkArgument(args.length == 1, "Check usage: " + getSignature()); LinkedList<Node> result = new LinkedList<>(); for (Node node : args[0]) { final String stringValue = backend.stringValue(node); boolean isWordChar = false; int c = 0; for (int i = 0; i < stringValue.length(); i++) { final boolean isWC = Character.isLetterOrDigit(stringValue.codePointAt(i)); if (!isWordChar && isWC) { c++; } isWordChar = isWC; } result.add(backend.createLiteral(String.valueOf(c), null, dataType)); } return result; }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { try { if (args.length != 2) { throw new IllegalArgumentException("LdPath function " + getLocalName() + " requires 2 arguments"); } if (args[1].size() != 1) { throw new IllegalArgumentException("len argument must be a single literal for function " + getLocalName()); } final Collection<Node> nodes = args[0]; final int length = Math.max(backend.intValue(args[1].iterator().next()), 0); final Collection<Node> result = new LinkedList<>(); for (Node node : nodes) { final String str = backend.stringValue(node); result.add(backend.createLiteral(str.substring(0, Math.min(length, str.length())))); } return result; } catch (NumberFormatException | ArithmeticException nfe) { throw new IllegalArgumentException(nfe); } }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { if ((args.length < 2 || args.length > 4)) { throw new IllegalArgumentException("wrong usage: " + getSignature()); } if (args[1].size() != 1 || (args.length > 2 && args[2].size() != 1) || (args.length > 3 && args[3].size() != 1)) { throw new IllegalArgumentException("wrong usage: " + getSignature()); } Collection<Node> nodes = args[0]; String join = transformer.transform(backend, args[1].iterator().next(), null); String prefix = args.length > 2 ? transformer.transform(backend, args[2].iterator().next(), null):""; String suffix = args.length > 3 ? transformer.transform(backend, args[3].iterator().next(), null):""; final StringBuilder sb = new StringBuilder(prefix); boolean first = true; for (final Node node : nodes) { final String string = backend.stringValue(node); if (!first) sb.append(join); sb.append(string); first = false; } sb.append(suffix); return Collections.singleton(backend.createLiteral(sb.toString())); }
/** * Apply the function to the list of nodes passed as arguments and return the result as type T. * Throws IllegalArgumentException if the function cannot be applied to the nodes passed as argument * or the number of arguments is not correct. * * @param args a nested list of KiWiNodes * @return */ @SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> rdfBackend, Node context, Collection<Node>... args) throws IllegalArgumentException { if(args.length < 1){ log.debug("filter text from context {}",context); return java.util.Collections.singleton( rdfBackend.createLiteral(doFilter(transformer.transform(rdfBackend, context, null)))); } else { log.debug("filter text from parameters"); Iterator<Node> it = Collections.iterator(args); List<Node> result = new ArrayList<>(); while (it.hasNext()) { result.add(rdfBackend.createLiteral(doFilter(transformer.transform(rdfBackend, it.next(), null)))); } return result; } }
/** * Apply the function to the list of nodes passed as arguments and return the result as type T. * Throws IllegalArgumentException if the function cannot be applied to the nodes passed as argument * or the number of arguments is not correct. * * @param args a nested list of KiWiNodes * @return */ @SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> rdfBackend, Node context, Collection<Node>... args) throws IllegalArgumentException { if(args.length < 1){ log.debug("filter text from context {}",context); return java.util.Collections.singleton( rdfBackend.createLiteral(doFilter(transformer.transform(rdfBackend, context, null)))); } else { log.debug("filter text from parameters"); Iterator<Node> it = Collections.iterator(args); List<Node> result = new ArrayList<>(); while (it.hasNext()) { result.add(rdfBackend.createLiteral(doFilter(transformer.transform(rdfBackend, it.next(), null)))); } return result; } }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> rdfBackend, Node context, @SuppressWarnings("unchecked") Collection<Node>... args) throws IllegalArgumentException { Set<String> jsonpaths = new HashSet<>(); for (Node jsonpath : args[0]) { try { jsonpaths.add(transformer.transform(rdfBackend,jsonpath, null)); } catch (IllegalArgumentException iae) { throw new IllegalArgumentException("First argument must not contain anything else than String-Literals!"); } } Iterator<Node> it; if(args.length < 2){ log.debug("Use context {} to execute jsonpaths {}",context,jsonpaths); it = Collections.singleton(context).iterator(); } else { log.debug("execute jsonpaths {} on parsed parameters",jsonpaths); it = org.apache.marmotta.ldpath.util.Collections.iterator(1,args); } List<Node> result = new ArrayList<>(); while (it.hasNext()) { Node n = it.next(); for (String r : doFilter(transformer.transform(rdfBackend,n, null), jsonpaths)) { result.add(rdfBackend.createLiteral(r)); } } return result; }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { try { if (args.length < 2 || args.length > 3) { throw new IllegalArgumentException("LdPath function " + getLocalName() + " requires 2 or 3 arguments"); } if (args[1].size() != 1) { throw new IllegalArgumentException("start argument must be a single literal for function " + getLocalName()); } if (args.length > 2 && args[2].size() != 1) { throw new IllegalArgumentException("end argument must be a single literal for function " + getLocalName()); } final Collection<Node> nodes = args[0]; final int start = Math.max(backend.intValue(args[1].iterator().next()), 0); final int end; if (args.length > 2) { end = Math.max(backend.intValue(args[2].iterator().next()), 0); } else { end = Integer.MAX_VALUE; } if (end < start) { throw new IllegalArgumentException(getLocalName() + " does not allow end beeing smaller than start (end:" + end + " < start:" + start + ")"); } final Collection<Node> result = new LinkedList<>(); for (Node node : nodes) { final String str = backend.stringValue(node); result.add(backend.createLiteral(str.substring(Math.min(start, str.length()), Math.min(end, str.length())))); } return result; } catch (NumberFormatException | ArithmeticException nfe) { throw new IllegalArgumentException(nfe); } }
@SafeVarargs @Override public final Collection<Node> apply(RDFBackend<Node> backend, Node context, Collection<Node>... args) throws IllegalArgumentException { if (args.length != 3 || args[1].size() != 1 || args[2].size() != 1) { throw new IllegalArgumentException("wrong usage: " + getSignature()); } Collection<Node> nodes = args[0]; String regex = transformer.transform(backend, args[1].iterator().next(), null); String replace = transformer.transform(backend, args[2].iterator().next(), null); try { final Pattern pattern = Pattern.compile(regex); Set<Node> result = new HashSet<>(); for (Node node : nodes) { final String string = backend.stringValue(node); final String replaced = pattern.matcher(string).replaceAll(replace); if (backend.isURI(node)) { result.add(backend.createURI(replaced)); } else if (backend.isLiteral(node)) { final Locale lang = backend.getLiteralLanguage(node); final URI type = backend.getLiteralType(node); result.add(backend.createLiteral(replaced, lang, type)); } } return result; } catch (PatternSyntaxException pex) { throw new IllegalArgumentException("could not parse regex pattern: '" + regex + "'", pex); } catch (IndexOutOfBoundsException iobex) { throw new IllegalArgumentException("invalid replacement string: '" + replace + "'"); } }