@Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("STATIC TERMS (").append(this.staticTerms.size()).append("):"); for (final URI staticTerm : this.staticTerms) { builder.append("\n").append(Statements.formatValue(staticTerm, Namespaces.DEFAULT)); } builder.append("\n\nRULES (").append(this.rules.size()).append("):"); for (final Rule rule : this.rules) { builder.append("\n").append(rule); } return builder.toString(); }
private CountQuery(final String name, final Properties properties, @Nullable final Template condition) { super(name, properties, condition.getVariables(), ImmutableList.of("size")); this.layer = (URI) Statements.parseValue(properties.getProperty("layer")); this.condition = condition; }
private RDFGraphvizRenderer(final Builder builder) { this.nodeNamespaces = builder.nodeNamespaces == null ? ImmutableSet.of() // : ImmutableSet.copyOf(builder.nodeNamespaces); this.nodeTypes = builder.nodeTypes == null ? ImmutableSet.of() // : ImmutableSet.copyOf(builder.nodeTypes); this.ignoredProperties = builder.ignoredProperties == null ? ImmutableSet.of() // : ImmutableSet.copyOf(builder.ignoredProperties); this.collapsedProperties = builder.collapsedProperties == null ? ImmutableSet.of() // : ImmutableSet.copyOf(builder.collapsedProperties); this.colorMap = builder.colorMap == null ? null : ImmutableMap.copyOf(builder.colorMap); this.styleMap = builder.styleMap == null ? null : ImmutableMap.copyOf(builder.styleMap); this.namespaces = builder.namespaces == null ? Namespaces.DEFAULT : builder.namespaces; this.valueComparator = builder.valueComparator != null ? Ordering .from(builder.valueComparator) : Ordering.from(Statements.valueComparator()); this.graphvizCommand = builder.graphvizCommand == null ? "neato" : builder.graphvizCommand; }
private NAFRenderer(final Builder builder) { this.colorMap = builder.colorMap == null ? DEFAULT_COLOR_MAP : ImmutableMap .copyOf(builder.colorMap); this.valueComparator = Ordering.from(Statements.valueComparator(Iterables.toArray( builder.rankedNamespaces == null ? DEFAULT_RANKED_NAMESPACES : builder.rankedNamespaces, String.class))); this.statementComparator = Ordering.from(Statements.statementComparator("spoc", this.valueComparator)); this.graphvizRenderer = RDFGraphvizRenderer .builder() .withNodeNamespaces( builder.nodeNamespaces == null ? DEFAULT_NODE_NAMESPACES : builder.nodeNamespaces) .withNodeTypes(builder.nodeTypes == null ? DEFAULT_NODE_TYPES : builder.nodeTypes) .withValueComparator(this.valueComparator) // .withColorMap(this.colorMap) // .withStyleMap(MoreObjects.firstNonNull(builder.styleMap, DEFAULT_STYLE_MAP)) // .withGraphvizCommand("neato").build(); this.template = MoreObjects.firstNonNull(builder.template, DEFAULT_TEMPLATE); this.templateParameters = builder.templateParameters; }
@SuppressWarnings("unchecked") private <T extends Value> Collection<T> extract(final Class<T> clazz, @Nullable final Object object, @Nullable final Multimap<String, ? extends T> map) { if (object == null) { return ImmutableList.of(); } else if (clazz.isInstance(object)) { return ImmutableList.of((T) object); } else if (object instanceof Iterable<?>) { final List<T> list = Lists.newArrayList(); for (final Object element : (Iterable<?>) object) { list.addAll(extract(clazz, element, map)); } return list; } else if (object.getClass().isArray()) { final List<T> list = Lists.newArrayList(); final int length = Array.getLength(object); for (int i = 0; i < length; ++i) { list.addAll(extract(clazz, Array.get(object, i), map)); } return list; } else if (map != null) { return (Collection<T>) map.get(object.toString()); } else { return ImmutableList.of(Statements.convert(object, clazz)); } }
private static void emit(final RDFHandler handler, final Resource subj, final URI pred, final Object obj, final Resource ctx) throws RDFHandlerException { final Value o = Statements.convert(obj, Value.class); if (subj != null && pred != null && o != null) { if (ctx == null) { handler.handleStatement(Statements.VALUE_FACTORY.createStatement(subj, pred, o)); } else { handler.handleStatement(Statements.VALUE_FACTORY.createStatement(subj, pred, o, ctx)); } } }
private LookupQuery(final String name, final Properties properties, @Nullable final Template id) { super(name, properties, id.getVariables(), ImmutableList.of("size")); List<URI> props = null; if (properties.containsKey("properties")) { props = Lists.newArrayList(); for (final String token : Splitter.onPattern("[ ,;]").omitEmptyStrings() .trimResults().split(properties.getProperty("properties"))) { props.add((URI) Statements.parseValue(token)); } } this.layer = (URI) Statements.parseValue(properties.getProperty("layer"), Namespaces.DEFAULT); this.id = id; this.properties = props; }
public static String format(final Value value) { // Emit literal without lang / datatype for easier consumption in analysis tools if (value instanceof Value) { // was instanceof Resource return Statements.formatValue(value, null); } else if (value != null) { return value.stringValue(); } else { return ""; } }
this.nodeNamespaces = builder.nodeNamespaces == null ? DEFAULT_NODE_NAMESPACES : ImmutableSet.copyOf(builder.nodeNamespaces); this.valueComparator = Ordering.from(Statements.valueComparator(Iterables.toArray( builder.rankedNamespaces == null ? DEFAULT_RANKED_NAMESPACES : builder.rankedNamespaces, String.class)));
private RetrieveQuery(final String name, final Properties properties, @Nullable final Template condition) { super(name, properties, condition.getVariables(), ImmutableList.of("size")); final String offset = properties.getProperty("offset"); final String limit = properties.getProperty("limit"); List<URI> props = null; if (properties.containsKey("properties")) { props = Lists.newArrayList(); for (final String token : Splitter.onPattern("[ ,;]").omitEmptyStrings() .trimResults().split(properties.getProperty("properties"))) { props.add((URI) Statements.parseValue(token)); } } this.layer = (URI) Statements.parseValue(properties.getProperty("layer"), Namespaces.DEFAULT); this.condition = condition; this.offset = offset == null ? null : Long.parseLong(offset); this.limit = limit == null ? null : Long.parseLong(limit); this.properties = props; }
private String format(final Value value) { if (value instanceof URI) { final URI uri = (URI) value; final String ns = uri.getNamespace(); final String prefix = this.namespaces.prefixFor(ns); if (prefix == null) { return escape("<.." + ns.charAt(ns.length() - 1) + uri.getLocalName() + ">"); } else { return prefix + ":" + escape(uri.getLocalName()); } } return escape(Statements.formatValue(value, this.namespaces)); }
final Set<URI> ids = new TreeSet<>(Statements.valueComparator()); final Map<URI, TupleExpr> heads = new HashMap<>(); final Map<URI, TupleExpr> bodies = new HashMap<>();
public static BindingSet decode(final List<String> variables, final String line) { final String[] tokens = line.split("\t"); Preconditions.checkArgument(tokens.length == variables.size(), "Wrong number of values (" + tokens.length + " found, " + variables.size() + " expected) in line: " + line); try { final MapBindingSet bindings = new MapBindingSet(); for (int i = 0; i < tokens.length; ++i) { String token = tokens[i]; if (!Strings.isNullOrEmpty(token)) { final char ch = token.charAt(0); token = ch == '\'' || ch == '"' || ch == '<' || ch == '_' ? token : "\"" + token + "\""; final Value value = Statements.parseValue(token, Namespaces.DEFAULT); bindings.addBinding(variables.get(i), value); } } return bindings; } catch (final Throwable ex) { throw new IllegalArgumentException("Could not parse variable values.\nVariables: " + variables + "\nLine: " + line, ex); } }
final int end = parseURI(string, i); if (end >= 0) { final URI u = (URI) Statements.parseValue(string.substring(i, end)); builder.append("__iri(").append(counter.getAndIncrement()) .append(", \"").append(u.stringValue()).append("\")"); final int end = parseQName(string, i); if (end >= 0) { final URI u = (URI) Statements.parseValue(string.substring(i, end), Namespaces.DEFAULT); builder.append("__iri(").append(counter.getAndIncrement())
base = base == null ? null : Statements.parseValue(base.contains(":") ? base : base + ":", Namespaces.DEFAULT).stringValue(); final int index = token.indexOf('='); if (index >= 0) { bindings.addBinding(token.substring(0, index).trim(), Statements.parseValue(token .substring(index + 1).trim(), Namespaces.DEFAULT)); if (options.hasOption("G")) { final String u = options.getOptionArg("G", String.class); globalURI = (URI) Statements.parseValue(u.contains(":") ? u // : u + ":", Namespaces.DEFAULT); emitStatic = true; final String ctx = options.getOptionArg("c", String.class); staticContext = (URI) Statements.parseValue(ctx.contains(":") ? ctx // : ctx + ":", Namespaces.DEFAULT);
@Override void doEvaluate(final Session session, final BindingSet input, final MapBindingSet output) throws Throwable { final URI id = (URI) Statements.parseValue(this.id.instantiate(input), Namespaces.DEFAULT); long size = 0L; try (final Representation representation = session.download(id) .caching(this.caching).timeout(getTimeout()).exec()) { if (representation != null) { size = representation.writeToByteArray().length; } else { LOGGER.warn("No results for DOWNLOAD request, id " + id); } } catch (final Throwable ex) { throw new RuntimeException("Failed DOWNLOAD, id " + TestUtil.format(id) + ", caching " + this.caching, ex); } finally { output.addBinding("size", FACTORY.createLiteral(size)); } } }
@Override void doEvaluate(final Session session, final BindingSet input, final MapBindingSet output) throws Throwable { final URI id = (URI) Statements.parseValue(this.id.instantiate(input), Namespaces.DEFAULT); long numTriples = 0L; try { final Stream<Record> stream = session.retrieve(this.layer).ids(id) .properties(this.properties).exec(); numTriples = Record.encode(stream, ImmutableList.of(this.layer)).count(); if (numTriples == 0) { LOGGER.warn("No results for LOOKUP request, layer " + TestUtil.format(this.layer) + ", id " + id); } } catch (final Throwable ex) { throw new RuntimeException("Failed LOOKUP " + TestUtil.format(this.layer) + ", id " + TestUtil.format(id) + ", properties " + this.properties, ex); } finally { output.addBinding("size", FACTORY.createLiteral(numTriples)); } }
@Override void doEvaluate(final Session session, final BindingSet input, final MapBindingSet output) throws Throwable { final URI id = (URI) Statements.parseValue(this.id.instantiate(input), Namespaces.DEFAULT); long numTriples = 0L; try { numTriples += Record.encode(session.retrieve(KS.RESOURCE).ids(id).exec(), ImmutableList.of(KS.RESOURCE)).count(); numTriples += Record.encode( session.retrieve(KS.MENTION).condition("ks:mentionOf = $$", id) .limit(100000L).exec(), ImmutableList.of(KS.MENTION)).count(); if (numTriples == 0) { LOGGER.warn("No results for LOOKUP ALL request, id " + id); } } catch (final Throwable ex) { throw new RuntimeException("Failed LOOKUP ALL, id " + TestUtil.format(id), ex); } finally { output.addBinding("size", FACTORY.createLiteral(numTriples)); } } }