|
| 1 | +package org.sindice.analytics.queryProcessor; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTBasicGraphPattern; |
| 6 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTConstraint; |
| 7 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTGraphPatternGroup; |
| 8 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTQueryContainer; |
| 9 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTRDFLiteral; |
| 10 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTRegexExpression; |
| 11 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTStr; |
| 12 | +import org.openrdf.sindice.query.parser.sparql.ast.ASTString; |
| 13 | +import org.openrdf.sindice.query.parser.sparql.ast.Node; |
| 14 | +import org.openrdf.sindice.query.parser.sparql.ast.SyntaxTreeBuilder; |
| 15 | +import org.openrdf.sindice.query.parser.sparql.ast.SyntaxTreeBuilderTreeConstants; |
| 16 | +import org.sindice.analytics.queryProcessor.QueryProcessor.POFMetadata; |
| 17 | + |
| 18 | +public class PofFilterProcessor { |
| 19 | + |
| 20 | + private PofFilterProcessor() { |
| 21 | + } |
| 22 | + |
| 23 | + public static void process(ASTQueryContainer ast, POFMetadata meta) { |
| 24 | + final ASTGraphPatternGroup gpg = ast.getQuery().getWhereClause().getGraphPatternGroup(); |
| 25 | + |
| 26 | + final List<Object> keyword = meta.pofNode.getMetadata() == null ? null : meta.pofNode |
| 27 | + .getMetadata(SyntaxTreeBuilder.Keyword); |
| 28 | + final List<Object> prefix = meta.pofNode.getMetadata() == null ? null : meta.pofNode |
| 29 | + .getMetadata(SyntaxTreeBuilder.Prefix); |
| 30 | + final List<Object> qname = meta.pofNode.getMetadata() == null ? null : meta.pofNode |
| 31 | + .getMetadata(SyntaxTreeBuilder.Qname); |
| 32 | + |
| 33 | + if (keyword != null) { |
| 34 | + final ASTBasicGraphPattern bgp = addRegexFilter(meta.pofNode, keyword.get(0).toString(), true); |
| 35 | + gpg.jjtAppendChild(bgp); |
| 36 | + } else if (prefix != null) { |
| 37 | + final ASTBasicGraphPattern bgp = addRegexFilter(meta.pofNode, "^" + prefix.get(0).toString().substring(1), true); |
| 38 | + gpg.jjtAppendChild(bgp); |
| 39 | + } else if (qname != null) { |
| 40 | + final ASTBasicGraphPattern bgp = addRegexFilter(meta.pofNode, "^" + qname.get(0).toString(), true); |
| 41 | + gpg.jjtAppendChild(bgp); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private static ASTBasicGraphPattern addRegexFilter(Node pof, String regex, boolean caseInsensitive) { |
| 46 | + final ASTBasicGraphPattern bgp = new ASTBasicGraphPattern(SyntaxTreeBuilderTreeConstants.JJTBASICGRAPHPATTERN); |
| 47 | + final ASTConstraint cst = new ASTConstraint(SyntaxTreeBuilderTreeConstants.JJTCONSTRAINT); |
| 48 | + final ASTRegexExpression astRegex = new ASTRegexExpression(SyntaxTreeBuilderTreeConstants.JJTREGEXEXPRESSION); |
| 49 | + |
| 50 | + // variable to test |
| 51 | + final ASTStr str = new ASTStr(SyntaxTreeBuilderTreeConstants.JJTSTR); |
| 52 | + str.jjtAppendChild(pof); |
| 53 | + astRegex.jjtAppendChild(str); |
| 54 | + // regular expression |
| 55 | + final ASTString strRegex = new ASTString(SyntaxTreeBuilderTreeConstants.JJTSTRING); |
| 56 | + strRegex.setValue(regex); |
| 57 | + final ASTRDFLiteral rdfLiteral = new ASTRDFLiteral(SyntaxTreeBuilderTreeConstants.JJTRDFLITERAL); |
| 58 | + rdfLiteral.jjtAppendChild(strRegex); |
| 59 | + astRegex.jjtAppendChild(rdfLiteral); |
| 60 | + // case insensitive or not |
| 61 | + if (caseInsensitive) { |
| 62 | + final ASTRDFLiteral ci = new ASTRDFLiteral(SyntaxTreeBuilderTreeConstants.JJTRDFLITERAL); |
| 63 | + final ASTString cistr = new ASTString(SyntaxTreeBuilderTreeConstants.JJTSTRING); |
| 64 | + cistr.setValue("i"); |
| 65 | + ci.jjtAppendChild(cistr); |
| 66 | + astRegex.jjtAppendChild(ci); |
| 67 | + } |
| 68 | + cst.jjtAppendChild(astRegex); |
| 69 | + bgp.jjtAppendChild(cst); |
| 70 | + return bgp; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments