Search in sources :

Example 16 with SPARQLParser

use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class IndexWritingTool method setVarOrders.

public void setVarOrders(final String s, final Configuration conf) throws MalformedQueryException {
    final SPARQLParser parser = new SPARQLParser();
    final TupleExpr query = parser.parseQuery(s, null).getTupleExpr();
    final List<String> projList = Lists.newArrayList(((Projection) query).getProjectionElemList().getTargetNames());
    final String projElems = Joiner.on(";").join(projList);
    conf.set("projElems", projElems);
    final Pattern splitPattern1 = Pattern.compile("\n");
    final Pattern splitPattern2 = Pattern.compile(",");
    final String[] lines = splitPattern1.split(s);
    final List<String> varOrders = Lists.newArrayList();
    final List<String> varOrderPos = Lists.newArrayList();
    int orderNum = 0;
    final int projSizeSq = projList.size() * projList.size();
    for (String t : lines) {
        if (orderNum > projSizeSq) {
            break;
        }
        String[] order = null;
        if (t.startsWith("#prefix")) {
            t = t.substring(7).trim();
            order = splitPattern2.split(t, projList.size());
        }
        String tempVarOrder = "";
        String tempVarOrderPos = "";
        if (order != null) {
            for (final String u : order) {
                if (tempVarOrder.length() == 0) {
                    tempVarOrder = u.trim();
                } else {
                    tempVarOrder = tempVarOrder + ";" + u.trim();
                }
                final int pos = projList.indexOf(u.trim());
                if (pos < 0) {
                    throw new IllegalArgumentException("Invalid variable order!");
                } else {
                    if (tempVarOrderPos.length() == 0) {
                        tempVarOrderPos = tempVarOrderPos + pos;
                    } else {
                        tempVarOrderPos = tempVarOrderPos + ";" + pos;
                    }
                }
            }
            varOrders.add(tempVarOrder);
            varOrderPos.add(tempVarOrderPos);
        }
        if (tempVarOrder.length() > 0) {
            orderNum++;
        }
    }
    if (orderNum == 0) {
        varOrders.add(projElems);
        String tempVarPos = "";
        for (int i = 0; i < projList.size(); i++) {
            if (i == 0) {
                tempVarPos = Integer.toString(0);
            } else {
                tempVarPos = tempVarPos + ";" + i;
            }
        }
        varOrderPos.add(tempVarPos);
    }
    final String[] vOrders = varOrders.toArray(new String[varOrders.size()]);
    final String[] vOrderPos = varOrderPos.toArray(new String[varOrderPos.size()]);
    conf.setStrings("varOrders", vOrders);
    conf.setStrings("varOrderPos", vOrderPos);
}
Also used : Pattern(java.util.regex.Pattern) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) Projection(org.eclipse.rdf4j.query.algebra.Projection) TupleExpr(org.eclipse.rdf4j.query.algebra.TupleExpr)

Example 17 with SPARQLParser

use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class StatementPatternStorage method addStatementPatternRange.

protected void addStatementPatternRange(String subj, String pred, String obj, String ctxt) throws IOException {
    logger.info("Adding statement pattern[subject:" + subj + ", predicate:" + pred + ", object:" + obj + ", context:" + ctxt + "]");
    StringBuilder sparqlBuilder = new StringBuilder();
    sparqlBuilder.append("select * where {\n");
    if (ctxt != null) {
        /**
         * select * where {
         *             GRAPH ?g {
         *             <http://www.example.org/exampleDocument#Monica> ?p ?o.
         *             }
         *             }
         */
        sparqlBuilder.append("GRAPH ").append(ctxt).append(" {\n");
    }
    sparqlBuilder.append(subj).append(" ").append(pred).append(" ").append(obj).append(".\n");
    if (ctxt != null) {
        sparqlBuilder.append("}\n");
    }
    sparqlBuilder.append("}\n");
    String sparql = sparqlBuilder.toString();
    if (logger.isDebugEnabled()) {
        logger.debug("Sparql statement range[" + sparql + "]");
    }
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = null;
    try {
        parsedQuery = parser.parseQuery(sparql, null);
    } catch (MalformedQueryException e) {
        throw new IOException(e);
    }
    parsedQuery.getTupleExpr().visitChildren(new AbstractQueryModelVisitor<IOException>() {

        @Override
        public void meet(StatementPattern node) throws IOException {
            Var subjectVar = node.getSubjectVar();
            Var predicateVar = node.getPredicateVar();
            Var objectVar = node.getObjectVar();
            subject_value = getValue(subjectVar);
            predicate_value = getValue(predicateVar);
            object_value = getValue(objectVar);
            Var contextVar = node.getContextVar();
            Map.Entry<TABLE_LAYOUT, Range> temp = createRange(subject_value, predicate_value, object_value);
            layout = temp.getKey();
            Range range = temp.getValue();
            addRange(range);
            if (contextVar != null && contextVar.getValue() != null) {
                String context_str = contextVar.getValue().stringValue();
                addColumnPair(context_str, "");
            }
        }
    });
}
Also used : SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) Var(org.eclipse.rdf4j.query.algebra.Var) IOException(java.io.IOException) Range(org.apache.accumulo.core.data.Range) ByteRange(org.apache.rya.api.query.strategy.ByteRange) StatementPattern(org.eclipse.rdf4j.query.algebra.StatementPattern) QueryParser(org.eclipse.rdf4j.query.parser.QueryParser) MalformedQueryException(org.eclipse.rdf4j.query.MalformedQueryException)

Example 18 with SPARQLParser

use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testMutlipleJoins.

public void testMutlipleJoins() throws Exception {
    String query = "select * where {\n" + "?subj <urn:lubm:rdfts#name> 'Department0'.\n" + "?subj <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" + "?subj <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:lubm:rdfts#Department>.\n" + "}";
    // System.out.println(query);
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);
    // System.out.println(parsedQuery);
    SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
    visitor.setTablePrefix(tablePrefix);
    visitor.setInstance(instance);
    visitor.setZk(zk);
    visitor.setUser(user);
    visitor.setPassword(password);
    visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
// System.out.println(visitor.getPigScript());
}
Also used : QueryParser(org.eclipse.rdf4j.query.parser.QueryParser) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery)

Example 19 with SPARQLParser

use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testLimit.

public void testLimit() throws Exception {
    String query = "select * where {\n" + "?subj <urn:lubm:rdfts#name> 'Department0'.\n" + "?subj <urn:lubm:rdfts#subOrganizationOf> ?suborg.\n" + "} limit 100";
    // System.out.println(query);
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);
    // System.out.println(parsedQuery);
    SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
    visitor.setTablePrefix(tablePrefix);
    visitor.setInstance(instance);
    visitor.setZk(zk);
    visitor.setUser(user);
    visitor.setPassword(password);
    visitor.meet(new QueryRoot(parsedQuery.getTupleExpr()));
// System.out.println(visitor.getPigScript());
}
Also used : QueryParser(org.eclipse.rdf4j.query.parser.QueryParser) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery)

Example 20 with SPARQLParser

use of org.eclipse.rdf4j.query.parser.sparql.SPARQLParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testCross.

public void testCross() throws Exception {
    String query = "select * where {\n" + "?subj0 <urn:lubm:rdfts#name> 'Department0'.\n" + "?subj1 <urn:lubm:rdfts#name> 'Department1'.\n" + "?subj0 <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" + "?subj1 <urn:lubm:rdfts#subOrganizationOf> <http://www.University0.edu>.\n" + "}";
    // System.out.println(query);
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);
    QueryRoot tupleExpr = new QueryRoot(parsedQuery.getTupleExpr());
    SimilarVarJoinOptimizer similarVarJoinOptimizer = new SimilarVarJoinOptimizer();
    similarVarJoinOptimizer.optimize(tupleExpr, null, null);
    // System.out.println(tupleExpr);
    SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor();
    visitor.setTablePrefix(tablePrefix);
    visitor.setInstance(instance);
    visitor.setZk(zk);
    visitor.setUser(user);
    visitor.setPassword(password);
    visitor.meet(tupleExpr);
// System.out.println(visitor.getPigScript());
}
Also used : QueryParser(org.eclipse.rdf4j.query.parser.QueryParser) SPARQLParser(org.eclipse.rdf4j.query.parser.sparql.SPARQLParser) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) ParsedQuery(org.eclipse.rdf4j.query.parser.ParsedQuery) SimilarVarJoinOptimizer(org.apache.rya.accumulo.pig.optimizer.SimilarVarJoinOptimizer)

Aggregations

SPARQLParser (org.eclipse.rdf4j.query.parser.sparql.SPARQLParser)260 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)244 Test (org.junit.Test)208 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)162 ArrayList (java.util.ArrayList)99 ExternalTupleSet (org.apache.rya.indexing.external.tupleSet.ExternalTupleSet)99 SimpleExternalTupleSet (org.apache.rya.indexing.external.tupleSet.SimpleExternalTupleSet)86 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)82 QueryModelNode (org.eclipse.rdf4j.query.algebra.QueryModelNode)62 PCJOptimizer (org.apache.rya.indexing.pcj.matching.PCJOptimizer)46 QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)43 RyaIRI (org.apache.rya.api.domain.RyaIRI)42 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)40 RyaStatement (org.apache.rya.api.domain.RyaStatement)39 BindingSet (org.eclipse.rdf4j.query.BindingSet)37 Projection (org.eclipse.rdf4j.query.algebra.Projection)37 RyaType (org.apache.rya.api.domain.RyaType)35 HashSet (java.util.HashSet)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)20 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)18