SparqlTranslationProcessor { public static RecommendationQuery process(POFMetadata meta, ASTQueryContainer ast) throws MalformedQueryException, VisitorException { final Dataset datasets = DatasetDeclProcessor.process(ast); final ASTIRI d; astQueryContainer = ast; if (datasets != null) { if (!datasets.getDefaultGraphs().isEmpty() || !datasets.getNamedGraphs().isEmpty()) { if (!datasets.getDefaultGraphs().isEmpty() && !datasets.getNamedGraphs().isEmpty()) { throw new DGSException("There can be only one default/named graph"); } if (!datasets.getDefaultGraphs().isEmpty() && datasets.getDefaultGraphs().size() != 1) { throw new DGSException("There can be only one default/named graph"); } if (!datasets.getNamedGraphs().isEmpty() && datasets.getNamedGraphs().size() != 1) { throw new DGSException("There can be only one default/named graph"); } } if (!datasets.getDefaultGraphs().isEmpty() && datasets.getNamedGraphs().isEmpty()) { d = new ASTIRI(SyntaxTreeBuilderTreeConstants.JJTIRI); final String value = datasets.getDefaultGraphs().toArray(new URI[1])[0].stringValue(); d.setValue(URIUtil.getSndDomainFromUrl(value)); } else if (datasets.getDefaultGraphs().isEmpty() && !datasets.getNamedGraphs().isEmpty()) { d = new ASTIRI(SyntaxTreeBuilderTreeConstants.JJTIRI); final String value = datasets.getNamedGraphs().toArray(new URI[1])[0].stringValue(); d.setValue(URIUtil.getSndDomainFromUrl(value)); } else { d = null; } } else { d = null; } String filter = null; if (meta != null && meta.pofNode.getMetadata() != null) { final Object keyword = meta.pofNode.getMetadata(SyntaxTreeBuilder.Keyword); final Object prefix = meta.pofNode.getMetadata(SyntaxTreeBuilder.Prefix); final Object qname = meta.pofNode.getMetadata(SyntaxTreeBuilder.Qname); if (keyword != null) { filter = "regex(str(?" + SyntaxTreeBuilder.PointOfFocus + "), \"" + keyword + "\", \"i\")"; } else if (prefix != null) { filter = "regex(str(?" + SyntaxTreeBuilder.PointOfFocus + "), \"^" + prefix + "\", \"i\")"; } else if (qname != null) { filter = "regex(str(?" + SyntaxTreeBuilder.PointOfFocus + "), \"^" + qname + "\", \"i\")"; } } final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, filter); final SparqlTranslationVisitor v = new SparqlTranslationVisitor(rq, d); v.visit(ast, null); changeToPofResource(rq, v.pofResourceName); return rq; } private  SparqlTranslationProcessor(); static RecommendationQuery process(POFMetadata meta, ASTQueryContainer ast);  }
@Test public void testTranslateSimplePredicate() throws Exception { final String query = "SELECT * { ?s <name> ?o1 . }"; final ASTQueryContainer ast = SyntaxTreeBuilder.parseQuery(query); final RecommendationQuery actual = SparqlTranslationProcessor.process(null, ast); final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, null); rq.edge().add(new Edge(null, "v1", "s", "<name>", null)); helper.assertDGSQuery(rq, actual); }
@Test public void testTranslateClassTriplePattern() throws Exception { final String query = "SELECT * { ?s a <Person> . }"; final ASTQueryContainer ast = SyntaxTreeBuilder.parseQuery(query); final RecommendationQuery actual = SparqlTranslationProcessor.process(null, ast); final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, null); rq.type().add(new Type(null, "s", "<Person>")); helper.assertDGSQuery(rq, actual); }
@Test public void testPOFonClass() throws Exception { final String query = "SELECT * { ?a a ?POF . }"; final ASTQueryContainer ast = SyntaxTreeBuilder.parseQuery(query); final RecommendationQuery actual = SparqlTranslationProcessor.process(null, ast); final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, null); rq.addProjection(QueryProcessor.POF_RESOURCE); rq.addProjection(SyntaxTreeBuilder.PointOfFocus); rq.addProjection(QueryProcessor.CARDINALITY_VAR); rq.addProjection(QueryProcessor.CLASS_ATTRIBUTE_LABEL_VAR); rq.addProjection(QueryProcessor.CLASS_ATTRIBUTE_CARD_VAR); rq.type().add(new Type(null, "s", "?" + SyntaxTreeBuilder.PointOfFocus)); helper.assertDGSQuery(rq, actual); }
@Test public void testConnectedObject1() throws Exception { final String query = "SELECT * { ?s ?p ?o . ?a ?POF ?s . }"; final ASTQueryContainer ast = SyntaxTreeBuilder.parseQuery(query); final RecommendationQuery actual = SparqlTranslationProcessor.process(null, ast); final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, null); rq.addProjection(QueryProcessor.POF_RESOURCE); rq.addProjection(SyntaxTreeBuilder.PointOfFocus); rq.addProjection(QueryProcessor.CARDINALITY_VAR); rq.edge().add(new Edge(null, "v1", "s", "?p", null)); rq.edge().add(new Edge(null, "v2", "a", "?" + SyntaxTreeBuilder.PointOfFocus, "s")); helper.assertDGSQuery(rq, actual); }
@Test public void testConnectedObject2() throws Exception { final String query = "SELECT * { ?s ?p ?o . ?a ?POF ?o . }"; final ASTQueryContainer ast = SyntaxTreeBuilder.parseQuery(query); final RecommendationQuery actual = SparqlTranslationProcessor.process(null, ast); final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, null); rq.addProjection(QueryProcessor.POF_RESOURCE); rq.addProjection(SyntaxTreeBuilder.PointOfFocus); rq.addProjection(QueryProcessor.CARDINALITY_VAR); rq.edge().add(new Edge(null, "v1", "s", "?p", "o")); rq.edge().add(new Edge(null, "v2", "a", "?" + SyntaxTreeBuilder.PointOfFocus, "o")); helper.assertDGSQuery(rq, actual); }
@Test public void testConnectedObject3() throws Exception { final String query = "SELECT * { ?s ?p ?o . ?a ?POF ?p . }"; final ASTQueryContainer ast = SyntaxTreeBuilder.parseQuery(query); final RecommendationQuery actual = SparqlTranslationProcessor.process(null, ast); final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, null); rq.addProjection(QueryProcessor.POF_RESOURCE); rq.addProjection(SyntaxTreeBuilder.PointOfFocus); rq.addProjection(QueryProcessor.CARDINALITY_VAR); rq.edge().add(new Edge(null, "v1", "s", "?p", null)); rq.edge().add(new Edge(null, "v2", "a", "?" + SyntaxTreeBuilder.PointOfFocus, "p")); helper.assertDGSQuery(rq, actual); }