
com.weaverplatform.sp4rql.model.scope.ScopeSimple Maven / Gradle / Ivy
package com.weaverplatform.sp4rql.model.scope;
import com.weaverplatform.sp4rql.error.ParseException;
import com.weaverplatform.sp4rql.model.expression.ExpressionArgument;
import com.weaverplatform.sp4rql.model.restriction.Sp4rqlRestriction;
import com.weaverplatform.sp4rql.model.solution.Sp4rqlSolution;
import com.weaverplatform.sp4rql.model.token.Sp4rqlToken;
import com.weaverplatform.sp4rql.model.token.VariableToken;
import com.weaverplatform.util.IndentStringBuilder;
import java.util.*;
/*
* A select and sub select block are closed contexts of variables. A filter and
* minus can enclose certain variables. Blank nodes are enclosed within a context.
* See https://www.w3.org/TR/sparql11-query/#variableScope
*/
public class ScopeSimple implements Sp4rqlScope, ExpressionArgument {
Sp4rqlScope inScope; // null means this is the root-scope
ArrayList restrictions = new ArrayList<>();
ArrayList expressionScopes = new ArrayList<>();
HashMap solutions = null;
Sp4rqlToken graphSelector = null;
private String alias;
public ScopeSimple(Sp4rqlScope inScope) {
super();
this.inScope = inScope;
}
@Override
public void setAlias(String alias) {
this.alias = alias;
}
@Override
public void setInScope(Sp4rqlScope scope) {
throw new ParseException("Do not set the super scope of scopes, use the constructor");
}
@Override
public Sp4rqlScope inScope() {
return inScope;
}
public Sp4rqlRestriction getRestriction(String alias) {
for(Sp4rqlRestriction scope : restrictions) {
if(scope.getAlias().equals(alias)) {
return scope;
}
}
for(Sp4rqlRestriction scope : expressionScopes) {
if(scope.getAlias().equals(alias)) {
return scope;
}
}
throw new ParseException(String.format("Could not find any restriction for alias %s", alias));
}
@Override
public String getAlias() {
return alias;
}
public void addRestriction(Sp4rqlRestriction restriction) {
restriction.setAlias(String.format("%s_r%d", getAlias(), restrictions.size()));
restrictions.add(restriction);
if(!(restriction instanceof Sp4rqlScope)) {
restriction.setInScope(this);
}
}
public void addExpressionScope(ScopeExists expressionScope) {
expressionScope.setAlias(String.format("%s_es%d", getAlias(), expressionScopes.size()));
expressionScopes.add(expressionScope);
}
@Override
public ArrayList getRestrictions() {
return restrictions;
}
@Override
public ArrayList getExpressionScopes() {
return expressionScopes;
}
@Override
public void resetSolutions() {
solutions = new HashMap<>();
for(Sp4rqlRestriction restriction : restrictions) {
if(restriction instanceof Sp4rqlScope) {
((Sp4rqlScope) restriction).resetSolutions();
}
}
for(ScopeExists restriction : expressionScopes) {
if(restriction instanceof Sp4rqlScope) {
restriction.resetSolutions();
}
}
}
@Override
public Collection getSolutions() {
return solutions.values();
}
@Override
public Sp4rqlSolution getSolution(VariableToken token) {
return solutions.get(token);
}
public void setSolution(VariableToken token, Sp4rqlSolution solution) {
solutions.put(token, solution);
}
@Override
public Integer resolvesTo() {
throw new RuntimeException("This makes no sense");
}
public void setGraphSelector(Sp4rqlToken token) {
graphSelector = token;
}
public Sp4rqlToken getGraphSelector() {
return graphSelector;
}
@Override
public HashSet getExposedVariables() {
HashSet set = new HashSet<>();
for(Sp4rqlRestriction restriction : getRestrictions()) {
if(!(restriction instanceof ScopeMinus)) {
set.addAll(restriction.getExposedVariables());
}
}
return set;
}
@Override
public void buildString(IndentStringBuilder builder) {
Iterator iterator = this.getRestrictions().iterator();
while(iterator.hasNext()) {
Sp4rqlRestriction restriction = iterator.next();
if(restriction instanceof Sp4rqlScope) {
if(restriction instanceof ScopeMinus) {
builder.append("MINUS {");
} else if(restriction instanceof ScopeOptional) {
builder.append("OPTIONAL {");
} else {
if(restriction instanceof ScopeSimple) {
Sp4rqlToken selector = ((ScopeSimple) restriction).getGraphSelector();
if(selector != null) {
builder.append("GRAPH ");
selector.buildString(builder);
builder.append(" ");
}
}
builder.append("{");
}
builder.appendLine(String.format(" # %s", restriction.getAlias()), 1);
restriction.buildString(builder);
builder.newline(-1);
builder.append("}");
if(iterator.hasNext()) {
builder.newline();
}
} else {
restriction.buildString(builder);
if (iterator.hasNext()) {
builder.newline();
}
}
}
}
@Override
public int hashCode() {
return getAlias().hashCode();
}
@Override
public boolean equals(Object object) {
if(!(object instanceof ScopeSimple)) {
return false;
}
return ((ScopeSimple) object).getAlias().equals(getAlias());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy