Search in sources :

Example 1 with Prefixes

use of com.inova8.pathql.context.Prefixes in project com.inova8.intelligentgraph by peterjohnlawrence.

the class Tracer method traceFacts.

/**
 * Trace facts.
 *
 * @param thing the thing
 * @param pathQLValue the path QL value
 * @param prefixes the prefixes
 * @param contexts the contexts
 */
public void traceFacts(Thing thing, String pathQLValue, Prefixes prefixes, org.eclipse.rdf4j.model.Resource... contexts) {
    if (!tracing)
        return;
    addTrace(String.format("Getting facts  '%s' of %s", toHTML(pathQLValue), addIRI(thing.getSuperValue())));
    CustomQueryOptions customQueryOptions = CustomQueryOption.getCustomQueryOptions(contexts, prefixes);
    if (!customQueryOptions.isEmpty())
        addTrace(String.format("...using options: [%s]", toHTML(customQueryOptions.toString())));
    ArrayList<org.eclipse.rdf4j.model.Resource> coreContexts = CustomQueryOption.getCoreContexts(contexts);
    if (!coreContexts.isEmpty())
        addTrace(String.format("...within contexts: %s", coreContexts.toString()));
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Resource(com.inova8.intelligentgraph.model.Resource)

Example 2 with Prefixes

use of com.inova8.pathql.context.Prefixes in project com.inova8.intelligentgraph by peterjohnlawrence.

the class CustomQueryOption method splitQuery.

/**
 * Split query.
 *
 * @param query the query
 * @param prefixes the prefixes
 * @return the custom query options
 */
private static CustomQueryOptions splitQuery(String query, Prefixes prefixes) {
    if (query == null || query.isEmpty()) {
        return null;
    }
    query = query.substring(query.indexOf("?") + 1);
    CustomQueryOptions customQueryOptions = new CustomQueryOptions();
    for (String queryParam : query.split("&")) {
        Pair<String, Value> pair = splitQueryParameter(queryParam, prefixes);
        customQueryOptions.add(pair.a, pair.b);
    }
    return customQueryOptions;
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Value(org.eclipse.rdf4j.model.Value)

Example 3 with Prefixes

use of com.inova8.pathql.context.Prefixes in project com.inova8.intelligentgraph by peterjohnlawrence.

the class CustomQueryOption method splitQueryParameter.

/**
 * Split query parameter.
 *
 * @param parameter the parameter
 * @param prefixes the prefixes
 * @return the pair
 */
private static Pair<String, Value> splitQueryParameter(String parameter, Prefixes prefixes) {
    final String enc = "UTF-8";
    List<String> keyValue = Arrays.stream(parameter.split("=")).map(e -> {
        try {
            return URLDecoder.decode(e, enc);
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeUnsupportedEncodingException(ex);
        }
    }).collect(toList());
    if (keyValue.size() == 2) {
        String value = keyValue.get(1);
        String[] valueParts = value.split("\\^\\^");
        Pair<String, Value> pair;
        String valueString = valueParts[0];
        if (valueString.startsWith("<")) {
            valueString = valueString.substring(1, valueString.length() - 1);
            pair = new Pair<String, Value>(keyValue.get(0), iri(valueString));
            return pair;
        } else {
            if (valueString.startsWith("'") || valueString.startsWith("\""))
                valueString = valueString.substring(1, valueString.length() - 1);
            if (valueParts.length == 2) {
                IRI qname = convertQName(valueParts[1], prefixes);
                Literal typedLiteral = Values.literal(Values.getValueFactory(), valueString, qname);
                pair = new Pair<String, Value>(keyValue.get(0), typedLiteral);
            } else {
                Literal literal = literal(valueString);
                pair = new Pair<String, Value>(keyValue.get(0), literal);
            }
            return pair;
        }
    } else {
        return new Pair<String, Value>(keyValue.get(0), null);
    }
}
Also used : IntelligentGraphConstants(com.inova8.intelligentgraph.constants.IntelligentGraphConstants) java.util(java.util) URLDecoder(java.net.URLDecoder) Resource(org.eclipse.rdf4j.model.Resource) Literal(org.eclipse.rdf4j.model.Literal) Values.literal(org.eclipse.rdf4j.model.util.Values.literal) Values(org.eclipse.rdf4j.model.util.Values) CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Value(org.eclipse.rdf4j.model.Value) Prefixes(com.inova8.pathql.context.Prefixes) Utilities(com.inova8.pathql.utilities.Utilities) IRI(org.eclipse.rdf4j.model.IRI) Values.iri(org.eclipse.rdf4j.model.util.Values.iri) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IRI(org.eclipse.rdf4j.model.IRI) Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with Prefixes

use of com.inova8.pathql.context.Prefixes in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternVisitor method visitPname_ns.

/**
 * Visit pname ns.
 *
 * @param ctx the ctx
 * @return the iri ref value element
 */
@Override
public IriRefValueElement visitPname_ns(Pname_nsContext ctx) {
    // pname_ns : PNAME_NS ;
    IriRefValueElement pname_nsElement = new IriRefValueElement(this.repositoryContext);
    Prefixes prefixes = null;
    prefixes = this.repositoryContext.getPrefixes();
    // if(thing!=null) prefixes=thing.getPrefixes();
    IRI qname = this.repositoryContext.convertQName(ctx.getText(), prefixes);
    if (qname != null) {
        pname_nsElement.setIri(qname);
        return pname_nsElement;
    } else {
        if (thing != null)
            thing.getEvaluationContext().getTracer().traceQNameError(ctx.getText());
        throw new ScriptFailedException(String.format("Error identifying namespace of qName %s", ctx.getText()));
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IriRefValueElement(com.inova8.pathql.element.IriRefValueElement) ScriptFailedException(com.inova8.intelligentgraph.exceptions.ScriptFailedException) Prefixes(com.inova8.pathql.context.Prefixes)

Example 5 with Prefixes

use of com.inova8.pathql.context.Prefixes in project com.inova8.intelligentgraph by peterjohnlawrence.

the class CustomQueryOption method getCustomQueryOptions.

/**
 * Gets the custom query options.
 *
 * @param contexts the contexts
 * @param prefixes the prefixes
 * @return the custom query options
 */
public static CustomQueryOptions getCustomQueryOptions(org.eclipse.rdf4j.model.Resource[] contexts, Prefixes prefixes) {
    CustomQueryOptions customQueryOptions = new CustomQueryOptions();
    if (contexts != null) {
        for (org.eclipse.rdf4j.model.Resource context : contexts) {
            if (context.stringValue().startsWith(IntelligentGraphConstants.URN_CUSTOM_QUERY_OPTIONS)) {
                String queryString = null;
                try {
                    queryString = URLDecoder.decode(context.stringValue(), StandardCharsets.UTF_8.toString());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                customQueryOptions = CustomQueryOption.splitQuery(queryString, prefixes);
                return customQueryOptions;
            }
        }
        return customQueryOptions;
    } else
        return null;
}
Also used : CustomQueryOptions(com.inova8.intelligentgraph.context.CustomQueryOptions) Resource(org.eclipse.rdf4j.model.Resource) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

CustomQueryOptions (com.inova8.intelligentgraph.context.CustomQueryOptions)4 Prefixes (com.inova8.pathql.context.Prefixes)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IRI (org.eclipse.rdf4j.model.IRI)2 Resource (org.eclipse.rdf4j.model.Resource)2 Value (org.eclipse.rdf4j.model.Value)2 IntelligentGraphConstants (com.inova8.intelligentgraph.constants.IntelligentGraphConstants)1 ScriptFailedException (com.inova8.intelligentgraph.exceptions.ScriptFailedException)1 Resource (com.inova8.intelligentgraph.model.Resource)1 IriRefValueElement (com.inova8.pathql.element.IriRefValueElement)1 Utilities (com.inova8.pathql.utilities.Utilities)1 URLDecoder (java.net.URLDecoder)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Literal (org.eclipse.rdf4j.model.Literal)1 Values (org.eclipse.rdf4j.model.util.Values)1 Values.iri (org.eclipse.rdf4j.model.util.Values.iri)1 Values.literal (org.eclipse.rdf4j.model.util.Values.literal)1