All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.fbk.knowledgestore.tool.TestUtil Maven / Gradle / Ivy

Go to download

A collection of command line tools for interacting with a KnowledgeStore server, including benchmarking tools to create and perform a performance test of KS retrieval methods as well as a tool for dumping the contents of a KS instance to RDF files.

There is a newer version: 1.7.1
Show newest version
package eu.fbk.knowledgestore.tool;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import org.openrdf.model.Value;
import org.openrdf.query.BindingSet;
import org.openrdf.query.impl.MapBindingSet;

import eu.fbk.knowledgestore.data.Data;
import eu.fbk.rdfpro.util.Namespaces;
import eu.fbk.rdfpro.util.Statements;

final class TestUtil {

    // Properties manipulation

    public static  T read(final Properties properties, final String name, final Class type) {
        final T value = read(properties, name, type, null);
        if (value == null) {
            throw new IllegalArgumentException("No value for mandatory property '" + name + "'");
        }
        return value;
    }

    public static  T read(final Properties properties, final String name, final Class type,
            final T defaultValue) {
        final String value = properties.getProperty(name);
        if (value == null) {
            return defaultValue;
        }
        try {
            return Data.convert(value, type);
        } catch (final Throwable ex) {
            throw new IllegalArgumentException("Invalid value for property '" + name + "': "
                    + value + " (" + ex.getMessage() + ")");
        }
    }

    public static void expand(final Properties targetProperties, final Properties defaultProperties) {
        for (final Map.Entry entry : defaultProperties.entrySet()) {
            final String name = (String) entry.getKey();
            if (!targetProperties.containsKey(name)) {
                final String value = (String) entry.getValue();
                targetProperties.setProperty(name, value);
            }
        }
    }

    public static Map split(final Properties properties) {
        final Map map = Maps.newLinkedHashMap();
        for (final Object key : properties.keySet()) {
            final String keyString = key.toString();
            final int index = keyString.indexOf(".");
            if (index > 0) {
                final String name = keyString.substring(0, index);
                final String property = keyString.substring(index + 1);
                final String value = properties.getProperty(keyString);
                Properties subProperties = map.get(name);
                if (subProperties == null) {
                    subProperties = new Properties();
                    map.put(name, subProperties);
                }
                subProperties.setProperty(property, value);
            }
        }
        return map;
    }

    // TSV and RDF manipulation

    public static BindingSet decode(final List 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);
        }
    }

    public static String encode(final List variables, final BindingSet bindings) {
        final StringBuilder builder = new StringBuilder();
        for (int i = 0; i < variables.size(); ++i) {
            if (i > 0) {
                builder.append('\t');
            }
            final Value value = bindings.getValue(variables.get(i));
            builder.append(format(value));
        }
        return builder.toString();
    }

    public static String format(final Iterable variables, final BindingSet bindings,
            final String separator) {
        final StringBuilder builder = new StringBuilder();
        for (final String variable : variables) {
            final Value value = bindings.getValue(variable);
            if (value != null) {
                builder.append(builder.length() == 0 ? "" : separator);
                builder.append(variable);
                builder.append('=');
                builder.append(format(value));
            }
        }
        return builder.toString();
    }

    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 "";
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy