
com.weaverplatform.sp4rql.model.restriction.RestrictionFactory Maven / Gradle / Ivy
package com.weaverplatform.sp4rql.model.restriction;
import com.weaverplatform.sp4rql.error.ParseException;
import com.weaverplatform.sp4rql.model.expression.ExpressionArgument;
import com.weaverplatform.sp4rql.model.expression.ExpressionFactory;
import com.weaverplatform.sp4rql.model.restriction.path.PathFactory;
import com.weaverplatform.sp4rql.model.restriction.path.PathRecord;
import com.weaverplatform.sp4rql.model.token.*;
import com.weaverplatform.sp4rql.model.scope.Sp4rqlScope;
import com.weaverplatform.sp4rql.parser.QueryUnitListener;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
import sp4rql.Sp4rql11Parser;
import java.util.Iterator;
public class RestrictionFactory {
private QueryUnitListener mainListener;
private TokenFactory tokenFactory;
private PathFactory pathFactory;
private ExpressionFactory expressionFactory;
public RestrictionFactory(QueryUnitListener listener, TokenFactory tokenFactory, PathFactory pathFactory, ExpressionFactory expressionFactory) {
this.mainListener = listener;
this.tokenFactory = tokenFactory;
this.pathFactory = pathFactory;
this.expressionFactory = expressionFactory;
}
public void addSingleTermTriple(Sp4rqlScope scope, Sp4rql11Parser.VarContext varContext) {
VariableToken variable = tokenFactory.getToken(varContext);
SingleTermRestriction restriction = new SingleTermRestriction(variable);
scope.addRestriction(restriction);
}
public void addTriples(Sp4rqlScope scope, Sp4rql11Parser.EmbTPContext embTPCtx,
Sp4rql11Parser.PropertyListPathNotEmptyContext propertyListCtx) {
Sp4rqlToken statement = new NullToken();
BlankToken subject = tokenFactory.getToken(embTPCtx, scope);
Sp4rqlToken predicate = null;
Sp4rqlToken object = null;
QuadRestriction blueprint = new QuadRestriction(statement, subject, predicate, object);
expandPropertyList(scope, blueprint, propertyListCtx);
}
public void addTriples(Sp4rqlScope scope, Sp4rql11Parser.VarOrTermContext statementCtx,
Sp4rql11Parser.VarOrTermContext subjectCtx,
Sp4rql11Parser.PropertyListPathNotEmptyContext propertyListCtx) {
Sp4rqlToken statement = tokenFactory.getToken(statementCtx, scope);
Sp4rqlToken subject = tokenFactory.getToken(subjectCtx, scope);
Sp4rqlToken predicate = null;
Sp4rqlToken object = null;
QuadRestriction blueprint = new QuadRestriction(statement, subject, predicate, object);
expandPropertyList(scope, blueprint, propertyListCtx);
}
public void addTriples(Sp4rqlScope scope, Sp4rql11Parser.VarOrTermContext subjectCtx,
Sp4rql11Parser.PropertyListPathNotEmptyContext propertyListCtx) {
Sp4rqlToken statement = new NullToken();
Sp4rqlToken subject = tokenFactory.getToken(subjectCtx, scope);
Sp4rqlToken predicate = null;
Sp4rqlToken object = null;
QuadRestriction blueprint = new QuadRestriction(statement, subject, predicate, object);
expandPropertyList(scope, blueprint, propertyListCtx);
}
private void expandPropertyList(Sp4rqlScope scope, QuadRestriction unfinishedQuad,
Sp4rql11Parser.PropertyListPathNotEmptyContext propertyListCtx) {
Iterator children = propertyListCtx.children.iterator();
while (children.hasNext()) {
Object verbThing = children.next();
if (verbThing instanceof TerminalNode) {
verbThing = children.next();
}
if (verbThing instanceof Sp4rql11Parser.VerbSimpleContext) {
setVerbSimple(scope, unfinishedQuad, (Sp4rql11Parser.VerbSimpleContext) verbThing);
} else if (verbThing instanceof Sp4rql11Parser.VerbPathContext) {
unfinishedQuad = setVerbPath(unfinishedQuad, (Sp4rql11Parser.VerbPathContext) verbThing);
} else {
throw new ParseException("Could not parse propertyListPathNotEmptyContext, expected VerbSimple or VerbPath");
}
Object objectThing = children.next();
if (objectThing instanceof Sp4rql11Parser.ObjectListContext) {
expandObjectList(scope, unfinishedQuad, (Sp4rql11Parser.ObjectListContext) objectThing);
} else if (objectThing instanceof Sp4rql11Parser.ObjectListPathContext) {
expandObjectListPath(scope, unfinishedQuad, (Sp4rql11Parser.ObjectListPathContext) objectThing);
} else {
throw new ParseException("Could not parse propertyListPathNotEmptyContext, expected objectList or objectListPath");
}
}
}
private void setVerbSimple(Sp4rqlScope scope, QuadRestriction unfinishedQuad, Sp4rql11Parser.VerbSimpleContext verbSimple) {
unfinishedQuad.setPredicate(tokenFactory.getToken(verbSimple, scope));
}
private QuadRestriction setVerbPath(QuadRestriction unfinishedQuad, Sp4rql11Parser.VerbPathContext verbPath) {
QuadPathRestriction pathRestriction = new QuadPathRestriction(unfinishedQuad);
pathFactory.buildPath(verbPath.path(), pathRestriction);
if(pathRestriction.isSingleTokenPath()) {
unfinishedQuad.setPredicate(pathRestriction.getSingleToken());
PathRecord record = pathRestriction.getPathStart().iterator().next().getNext().iterator().next();
if(record.negated) {
unfinishedQuad.setNegated();
}
if(record.reversed) {
unfinishedQuad.setReversed();
}
return unfinishedQuad;
}
return pathRestriction;
}
private void expandObjectList(Sp4rqlScope scope, QuadRestriction unfinishedQuad, Sp4rql11Parser.ObjectListContext objectList) {
for(Sp4rql11Parser.ObjectContext object : objectList.object()) {
QuadRestriction finalQuad = unfinishedQuad.clone();
finalQuad.setObject(tokenFactory.getToken(object, scope));
scope.addRestriction(finalQuad);
}
}
private void expandObjectListPath(Sp4rqlScope scope, QuadRestriction unfinishedQuad, Sp4rql11Parser.ObjectListPathContext objectListPath) {
for(Sp4rql11Parser.ObjectPathContext objectPath : objectListPath.objectPath()) {
expandObjectListPath(scope, unfinishedQuad, objectPath.graphNodePath());
}
}
private void expandObjectListPath(Sp4rqlScope scope, QuadRestriction unfinishedQuad, Sp4rql11Parser.GraphNodePathContext graphNodePath) {
if(graphNodePath.varOrTermOrEmbTP() != null) {
QuadRestriction finalQuad = unfinishedQuad.clone();
finalQuad.setObject(tokenFactory.getToken(graphNodePath.varOrTermOrEmbTP(), scope));
scope.addRestriction(finalQuad);
}
if(graphNodePath.triplesNodePath() != null) {
expandTriplesNodePath(scope, unfinishedQuad, graphNodePath.triplesNodePath());
}
}
private void expandTriplesNodePath(Sp4rqlScope scope, QuadRestriction unfinishedQuad, Sp4rql11Parser.TriplesNodePathContext ctx) {
if(ctx.collectionPath() != null) {
for(Sp4rql11Parser.GraphNodePathContext path : ctx.collectionPath().graphNodePath()) {
expandObjectListPath(scope, unfinishedQuad, path);
}
}
if(ctx.blankNodePropertyListPath() != null) {
BlankToken blankNode = tokenFactory.addBlank();
QuadRestriction finalQuad = unfinishedQuad.clone();
finalQuad.setObject(blankNode);
scope.addRestriction(finalQuad);
QuadRestriction blueprint = new QuadRestriction(new NullToken(), blankNode, null, null);
expandPropertyList(scope, blueprint, ctx.blankNodePropertyListPath().propertyListPathNotEmpty());
}
}
public ValuesRestriction addValuesRestriction(Sp4rql11Parser.DataBlockContext ctx) {
ValuesRestriction restriction = new ValuesRestriction();
if(ctx.inlineDataOneVar() != null) {
VariableToken token = tokenFactory.getToken(ctx.inlineDataOneVar().var());
restriction.addVariable(token);
for(Sp4rql11Parser.DataBlockValueContext value : ctx.inlineDataOneVar().dataBlockValue()) {
restriction.addValue(tokenFactory.getToken(value));
}
} else if(ctx.inlineDataFull() != null) {
for(Sp4rql11Parser.VarContext var : ctx.inlineDataFull().var()) {
VariableToken token = tokenFactory.getToken(var);
restriction.addVariable(token);
}
boolean valueSection = false;
for(ParseTree item : ctx.inlineDataFull().children) {
if(item instanceof Sp4rql11Parser.DataBlockValueContext) {
valueSection = true;
restriction.addValue(tokenFactory.getToken((Sp4rql11Parser.DataBlockValueContext) item));
}
if(valueSection && item instanceof TerminalNodeImpl) {
if(")".equals(item.getText())) {
restriction.addRow();
}
}
}
} else {
throw new ParseException("Could not find values in the dataBlock");
}
return restriction;
}
public void addFilterRestriction(Sp4rqlScope scope, Sp4rql11Parser.FilterContext ctx) {
FilterRestriction restriction = new FilterRestriction(expressionFactory.getExpression(ctx.constraint()));
scope.addRestriction(restriction);
}
public void addBindRestriction(Sp4rqlScope scope, Sp4rql11Parser.BindContext ctx) {
VariableToken token = tokenFactory.getToken(ctx.var());
ExpressionArgument expression;
if(ctx.expressionOrEmbTP().expression() != null) {
expression = expressionFactory.getExpression(ctx.expressionOrEmbTP().expression());
} else if(ctx.expressionOrEmbTP().embTP() != null) {
expression = tokenFactory.getToken(ctx.expressionOrEmbTP().embTP(), scope);
} else {
throw new ParseException("Could not parse bind");
}
addBindRestriction(scope, token, expression);
}
public void addBindRestriction(Sp4rqlScope scope, VariableToken token, ExpressionArgument expression) {
BindRestriction restriction = new BindRestriction(token, expression);
scope.addRestriction(restriction);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy