Search in sources :

Example 6 with QueryParser

use of org.openrdf.query.parser.QueryParser 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 QueryModelVisitorBase<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.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Var(org.openrdf.query.algebra.Var) IOException(java.io.IOException) Range(org.apache.accumulo.core.data.Range) ByteRange(org.apache.rya.api.query.strategy.ByteRange) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryParser(org.openrdf.query.parser.QueryParser) MalformedQueryException(org.openrdf.query.MalformedQueryException)

Example 7 with QueryParser

use of org.openrdf.query.parser.QueryParser in project incubator-rya by apache.

the class SparqlQueryPigEngine method generatePigScript.

public String generatePigScript(final String sparql) throws Exception {
    Preconditions.checkNotNull(sparql, "Sparql query cannot be null");
    final QueryParser parser = new SPARQLParser();
    final ParsedQuery parsedQuery = parser.parseQuery(sparql, null);
    final QueryRoot tupleExpr = new QueryRoot(parsedQuery.getTupleExpr());
    if (inference || stats) {
        if (inference) {
            tupleExpr.visit(new TransitivePropertyVisitor(conf, inferenceEngine));
            tupleExpr.visit(new SymmetricPropertyVisitor(conf, inferenceEngine));
            tupleExpr.visit(new InverseOfVisitor(conf, inferenceEngine));
        }
        if (stats) {
            (new QueryJoinOptimizer(rdfCloudTripleStoreEvaluationStatistics)).optimize(tupleExpr, null, null);
        }
    }
    sparqlToPigTransformVisitor.meet(tupleExpr);
    return sparqlToPigTransformVisitor.getPigScript();
}
Also used : QueryParser(org.openrdf.query.parser.QueryParser) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) QueryRoot(org.openrdf.query.algebra.QueryRoot) ParsedQuery(org.openrdf.query.parser.ParsedQuery) QueryJoinOptimizer(org.apache.rya.rdftriplestore.evaluation.QueryJoinOptimizer) SymmetricPropertyVisitor(org.apache.rya.rdftriplestore.inference.SymmetricPropertyVisitor) TransitivePropertyVisitor(org.apache.rya.rdftriplestore.inference.TransitivePropertyVisitor) InverseOfVisitor(org.apache.rya.rdftriplestore.inference.InverseOfVisitor)

Example 8 with QueryParser

use of org.openrdf.query.parser.QueryParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testStatementPattern.

public void testStatementPattern() throws Exception {
    String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + " PREFIX ub: <urn:lubm:rdfts#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + " SELECT * WHERE\n" + " {\n" + "\t?x rdf:type ub:UndergraduateStudent\n" + " }\n" + "";
    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.openrdf.query.parser.QueryParser) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) QueryRoot(org.openrdf.query.algebra.QueryRoot) ParsedQuery(org.openrdf.query.parser.ParsedQuery)

Example 9 with QueryParser

use of org.openrdf.query.parser.QueryParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testFixedStatementPatternInferenceQuery.

public void testFixedStatementPatternInferenceQuery() throws Exception {
    String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + " PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + " PREFIX ub: <urn:lubm:rdfts#>\n" + " SELECT * WHERE\n" + " {\n" + "      ?y ub:memberOf <http://www.Department3.University10.edu>.\n" + "      {?y rdf:type ub:Professor.}\n" + "       UNION \n" + "      {?y rdf:type ub:GraduateStudent.}\n" + " }";
    // System.out.println(query);
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);
    TupleExpr tupleExpr = parsedQuery.getTupleExpr();
    // Configuration conf = new Configuration();
    // Connector connector = new ZooKeeperInstance("stratus", "stratus13:2181").getConnector("root", "password".getBytes());
    // 
    // InferenceEngine inferenceEngine = new InferenceEngine();
    // CloudbaseRdfDAO rdfDAO = new CloudbaseRdfDAO();
    // rdfDAO.setConf(conf);
    // rdfDAO.setConnector(connector);
    // rdfDAO.setNamespaceTable("l_ns");
    // rdfDAO.setSpoTable("l_spo");
    // rdfDAO.setPoTable("l_po");
    // rdfDAO.setOspTable("l_osp");
    // rdfDAO.init();
    // 
    // inferenceEngine.setRdfDao(rdfDAO);
    // inferenceEngine.setConf(conf);
    // inferenceEngine.init();
    // 
    // tupleExpr.visit(new TransitivePropertyVisitor(conf, inferenceEngine));
    // tupleExpr.visit(new SymmetricPropertyVisitor(conf, inferenceEngine));
    // tupleExpr.visit(new InverseOfVisitor(conf, inferenceEngine));
    // tupleExpr.visit(new SubPropertyOfVisitor(conf, inferenceEngine));
    // tupleExpr.visit(new SubClassOfVisitor(conf, inferenceEngine));
    // 
    // CloudbaseRdfEvalStatsDAO rdfEvalStatsDAO = new CloudbaseRdfEvalStatsDAO();
    // rdfEvalStatsDAO.setConnector(connector);
    // rdfEvalStatsDAO.setEvalTable("l_eval");
    // RdfCloudTripleStoreEvaluationStatistics stats = new RdfCloudTripleStoreEvaluationStatistics(conf, rdfEvalStatsDAO);
    // (new QueryJoinOptimizer(stats)).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(new QueryRoot(tupleExpr));
// System.out.println(visitor.getPigScript());
}
Also used : QueryParser(org.openrdf.query.parser.QueryParser) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) QueryRoot(org.openrdf.query.algebra.QueryRoot) ParsedQuery(org.openrdf.query.parser.ParsedQuery) TupleExpr(org.openrdf.query.algebra.TupleExpr)

Example 10 with QueryParser

use of org.openrdf.query.parser.QueryParser in project incubator-rya by apache.

the class SparqlToPigTransformVisitorTest method testMutliReturnJoin.

public void testMutliReturnJoin() throws Exception {
    String query = "select * where {\n" + "?subj <urn:lubm:rdfts#name> 'Department0'.\n" + "?subj <urn:lubm:rdfts#subOrganizationOf> ?suborg.\n" + "}";
    QueryParser parser = new SPARQLParser();
    ParsedQuery parsedQuery = parser.parseQuery(query, null);
    System.out.println(query);
    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.openrdf.query.parser.QueryParser) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) QueryRoot(org.openrdf.query.algebra.QueryRoot) ParsedQuery(org.openrdf.query.parser.ParsedQuery)

Aggregations

ParsedQuery (org.openrdf.query.parser.ParsedQuery)12 QueryParser (org.openrdf.query.parser.QueryParser)12 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)12 QueryRoot (org.openrdf.query.algebra.QueryRoot)11 TupleExpr (org.openrdf.query.algebra.TupleExpr)2 IOException (java.io.IOException)1 Range (org.apache.accumulo.core.data.Range)1 SimilarVarJoinOptimizer (org.apache.rya.accumulo.pig.optimizer.SimilarVarJoinOptimizer)1 ByteRange (org.apache.rya.api.query.strategy.ByteRange)1 QueryJoinOptimizer (org.apache.rya.rdftriplestore.evaluation.QueryJoinOptimizer)1 InverseOfVisitor (org.apache.rya.rdftriplestore.inference.InverseOfVisitor)1 SymmetricPropertyVisitor (org.apache.rya.rdftriplestore.inference.SymmetricPropertyVisitor)1 TransitivePropertyVisitor (org.apache.rya.rdftriplestore.inference.TransitivePropertyVisitor)1 MalformedQueryException (org.openrdf.query.MalformedQueryException)1 StatementPattern (org.openrdf.query.algebra.StatementPattern)1 Var (org.openrdf.query.algebra.Var)1