PointOfFocusProcessor { public static RecommendationType process(ASTQueryContainer ast) throws VisitorException { final List<String> v = new ArrayList<String>(); if (!(ast.getQuery() instanceof ASTSelectQuery)) { final ASTSelectQuery selectQuery = new ASTSelectQuery(SyntaxTreeBuilderTreeConstants.JJTSELECTQUERY); final ASTSelect select = new ASTSelect(SyntaxTreeBuilderTreeConstants.JJTSELECT); selectQuery.jjtAppendChild(select); for (ASTDatasetClause d : ast.getQuery().getDatasetClauseList()) { selectQuery.jjtAppendChild(d); } selectQuery.jjtAppendChild(ast.getQuery().getWhereClause()); if (!(ast.getQuery() instanceof ASTAskQuery)) { if (ast.getQuery().getGroupClause() != null) { selectQuery.jjtAppendChild(ast.getQuery().getGroupClause()); } if (ast.getQuery().getHavingClause() != null) { selectQuery.jjtAppendChild(ast.getQuery().getHavingClause()); } if (ast.getQuery().getOrderClause() != null) { selectQuery.jjtAppendChild(ast.getQuery().getOrderClause()); } if (ast.getQuery().getLimit() != null) { selectQuery.jjtAppendChild(ast.getQuery().getLimit()); } if (ast.getQuery().getOffset() != null) { selectQuery.jjtAppendChild(ast.getQuery().getOffset()); } } if (ast.getQuery().getBindingsClause() != null) { selectQuery.jjtAppendChild(ast.getQuery().getBindingsClause()); } ast.getQuery().jjtReplaceWith(selectQuery); } final ASTMaterializePointOfFocus matPOF = new ASTMaterializePointOfFocus(); matPOF.visit(ast, v); final POFRecType type = new POFRecType(); return (RecommendationType) type.visit(ast, RecommendationType.NONE); } private  PointOfFocusProcessor(); static RecommendationType process(ASTQueryContainer ast);  }
@Test public void testMaterializePOF() throws Exception { final String q = "SELECT ?s WHERE { ?s < ?o }"; final String expectedAst = "QueryContainer\n" + " SelectQuery\n" + " Select\n" + " ProjectionElem\n" + " Var (POF)\n" + " WhereClause\n" + " GraphPatternGroup\n" + " BasicGraphPattern\n" + " TriplesSameSubjectPath\n" + " Var (s)\n" + " PropertyListPath\n" + " Var (POF)\n" + " ObjectList\n" + " Var (o)"; ast = SyntaxTreeBuilder.parseQuery(q); PointOfFocusProcessor.process(ast); assertEquals(expectedAst, ast.dump("")); }
@Test public void testPOFOnObject() throws Exception { final String q = "SELECT ?s WHERE { ?s a < }"; final String expectedAst = "QueryContainer\n" + " SelectQuery\n" + " Select\n" + " ProjectionElem\n" + " Var (POF)\n" + " WhereClause\n" + " GraphPatternGroup\n" + " BasicGraphPattern\n" + " TriplesSameSubjectPath\n" + " Var (s)\n" + " PropertyListPath\n" + " PathAlternative\n" + " PathSequence\n" + " PathElt\n" + " IRI (http: " ObjectList\n" + " Var (POF)"; ast = SyntaxTreeBuilder.parseQuery(q); PointOfFocusProcessor.process(ast); assertEquals(expectedAst, ast.dump("")); }
@Test(expected=DGSException.class) public void testPOFOnObject2() throws Exception { final String q = "SELECT ?s WHERE { ?s <name> < }"; ast = SyntaxTreeBuilder.parseQuery(q); PointOfFocusProcessor.process(ast); }
@Test(expected=DGSException.class) public void testPOFOnObject3() throws Exception { final String q = "SELECT ?s WHERE { ?s ?p < }"; ast = SyntaxTreeBuilder.parseQuery(q); PointOfFocusProcessor.process(ast); }
@Test public void testReplaceWithSelectQueryPOF() throws Exception { final String qConstruct = "CONSTRUCT { ?s a ?o } WHERE { ?s < ?o }"; final String qAsk = "ASK WHERE { ?s < ?o }"; final String qDescribe = "DESCRIBE ?s WHERE { ?s < ?o }"; final String expectedAst = "QueryContainer\n" + " SelectQuery\n" + " Select\n" + " ProjectionElem\n" + " Var (POF)\n" + " WhereClause\n" + " GraphPatternGroup\n" + " BasicGraphPattern\n" + " TriplesSameSubjectPath\n" + " Var (s)\n" + " PropertyListPath\n" + " Var (POF)\n" + " ObjectList\n" + " Var (o)"; ast = SyntaxTreeBuilder.parseQuery(qAsk); PointOfFocusProcessor.process(ast); assertEquals(expectedAst, ast.dump("")); ast = SyntaxTreeBuilder.parseQuery(qConstruct); PointOfFocusProcessor.process(ast); assertEquals(expectedAst, ast.dump("")); ast = SyntaxTreeBuilder.parseQuery(qDescribe); PointOfFocusProcessor.process(ast); assertEquals(expectedAst, ast.dump("")); }
@Test public void testConstructQueryPOFWithSolutionsMod() throws Exception { final String q = "CONSTRUCT { ?s a ?o } WHERE { ?s < ?o } GROUP BY(?s)"; final String expectedAst = "QueryContainer\n" + " SelectQuery\n" + " Select\n" + " ProjectionElem\n" + " Var (POF)\n" + " WhereClause\n" + " GraphPatternGroup\n" + " BasicGraphPattern\n" + " TriplesSameSubjectPath\n" + " Var (s)\n" + " PropertyListPath\n" + " Var (POF)\n" + " ObjectList\n" + " Var (o)\n" + " GroupClause\n" + " GroupCondition\n" + " Var (s)"; ast = SyntaxTreeBuilder.parseQuery(q); PointOfFocusProcessor.process(ast); assertEquals(expectedAst, ast.dump("")); }
@Test public void testRecommendationTypeClass() throws Exception { final String q = "SELECT ?s WHERE { ?s a < }"; ast = SyntaxTreeBuilder.parseQuery(q); final RecommendationType type = PointOfFocusProcessor.process(ast); assertEquals(RecommendationType.CLASS, type); }
@Test public void testRecommendationTypePredicate() throws Exception { final String q = "SELECT ?s WHERE { ?s < ?o }"; ast = SyntaxTreeBuilder.parseQuery(q); final RecommendationType type = PointOfFocusProcessor.process(ast); assertEquals(RecommendationType.PREDICATE, type); }
@Test public void testRecommendationTypeGraph() throws Exception { final String q = "SELECT ?s WHERE { GRAPH < { ?s ?p ?o } }"; ast = SyntaxTreeBuilder.parseQuery(q); final RecommendationType type = PointOfFocusProcessor.process(ast); assertEquals(RecommendationType.GRAPH, type); }
@Test(expected=DGSException.class) public void testWrongRecommendationType() throws Exception { final String q = "SELECT ?s WHERE { GRAPH ?g { < ?p ?o } }"; ast = SyntaxTreeBuilder.parseQuery(q); PointOfFocusProcessor.process(ast); }