Skip to content

Commit

Permalink
Added comments to the module AddOneHoprProertyPath and unit test for …
Browse files Browse the repository at this point in the history
…AST2Text sindice#3
  • Loading branch information
bibhas committed Jul 19, 2012
1 parent cd2d756 commit a41adf9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,9 @@ public Object visit(ASTVar node, Object data)
throws VisitorException {
final StringBuilder sb = (StringBuilder) data;

// if (node.getName().startsWith("?")) { // only the POF can start with '?'
// sb.append(node.getName()).append("< ");
// }
if (node.getName().equals("?POF"))
sb.append(node.getName()).append(' ');
if (node.getName().startsWith("?")) { // only the POF can start with '?'
sb.append("< ");
}
else
sb.append('?').append(node.getName()).append(' ');
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
import org.slf4j.LoggerFactory;

/**
*
* This module keeps on adding a property path between two nodes until either
* the maximum limit is reached or a valid recommendation is obtained.
* @author bibhas [Jul 9, 2012]
* @email bibhas.das@deri.org
*
Expand All @@ -54,6 +55,10 @@ public AddOneHopPropertyPath(SparqlTranslationProcessor sparql2dgs) {
this.sparql2dgs = sparql2dgs;
}

/* The type of object to be sent to the visitor method.
* It stores the path length from one node to another(hops).
* It also stores all the variables to be projected for recommendation (varsToProject).
* */
private class HopsVarsPair {
int hops;
final ArrayList<String> varsToProject = new ArrayList<String>();
Expand All @@ -76,6 +81,10 @@ public PipelineObject process(PipelineObject po)
final HopsVarsPair hopsVars = new HopsVarsPair();
while (hops <= po.getMAX_HOPS()) {
po = sparql2dgs.process(po);

/* convert the DGS query to an ASK query to check if recommendations
* are obtained with the current property paths.
*/
ASTQueryContainer ast = selectToAsk.convert(po.getAst());
QueryIterator<Label, Context> qit = po.getBackend().submit(
AST2TextTranslator.translate(ast));
Expand All @@ -88,6 +97,7 @@ public PipelineObject process(PipelineObject po)
break;
hopsVars.reset(hops);
po.setAst(SyntaxTreeBuilder.parseQuery(query));
// add another hop to the existing query
obj.visit(po.getAst(), hopsVars);
query = AST2TextTranslator.translate(po.getAst());
po.getVarsToProject().addAll(hopsVars.varsToProject);
Expand All @@ -96,6 +106,10 @@ public PipelineObject process(PipelineObject po)
return po;
}

/*The visitor searches for the triple pattern containing the POF
*and adds the requisite number of triples(as mentioned by hops)
*after the found triple.
* */
public class AddHopVisitor extends ASTVisitorBase {
public Object visit(ASTBasicGraphPattern node, Object data)
throws VisitorException {
Expand All @@ -121,6 +135,7 @@ public Object visit(ASTBasicGraphPattern node, Object data)
bgpDGS.jjtAppendChild(triple);
}

// add hops number of triples after the triple containing POF
if (foundPOF) {
for (int i = 2; i < hops; i++) {
bgpDGS.jjtAppendChild(triple);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*******************************************************************************
* Copyright (c) 2012 National University of Ireland, Galway. All Rights Reserved.
*
*
* This project is a free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
package org.sindice.analytics.queryProcessor;


import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openrdf.sindice.query.parser.sparql.ast.ASTQueryContainer;
import org.openrdf.sindice.query.parser.sparql.ast.SyntaxTreeBuilder;

/**
* @author bibhas [Jul 19, 2012]
* @email bibhas.das@deri.org
*
*/
public class TestAST2TextTranslator {

private ASTQueryContainer ast;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
}

@Test
public void testPOF()
throws Exception {
final String query = "SELECT * WHERE { ?s < ?o .}";
ast = SyntaxTreeBuilder.parseQuery(query);
String translatedQuery = AST2TextTranslator.translate(ast);
String expected = "SELECT *\n" +
"WHERE {\n" +
" ?s < ?o .\n" +
"}\n";
assertEquals(expected, translatedQuery);
}

@Test
public void testGraphPOF()
throws Exception {
final String query = "SELECT * { GRAPH < { ?s ?p ?o .}}";
ast = SyntaxTreeBuilder.parseQuery(query);
String translatedQuery = AST2TextTranslator.translate(ast);
String expected = "SELECT *\n" +
"WHERE {\n" +
"GRAPH < {\n" +
" ?s ?p ?o .\n" +
"}\n" +
"}\n";
assertEquals(expected, translatedQuery);
}

}

0 comments on commit a41adf9

Please sign in to comment.