
com.weaverplatform.sp4rql.model.scope.ScopeUnion Maven / Gradle / Ivy
package com.weaverplatform.sp4rql.model.scope;
import com.weaverplatform.sp4rql.error.ParseException;
import com.weaverplatform.sp4rql.model.restriction.Sp4rqlRestriction;
import com.weaverplatform.sp4rql.model.solution.Sp4rqlSolution;
import com.weaverplatform.sp4rql.model.token.VariableToken;
import com.weaverplatform.util.IndentStringBuilder;
import java.util.*;
public class ScopeUnion implements Sp4rqlScope {
private Sp4rqlScope inScope;
private ArrayList restrictions = new ArrayList<>();
private String alias;
public ScopeUnion(Sp4rqlScope inScope) {
this.inScope = inScope;
}
@Override
public void setAlias(String alias) {
this.alias = alias;
}
@Override
public String getAlias() {
return 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;
}
@Override
public void addRestriction(Sp4rqlRestriction restriction) {
if(restriction instanceof Sp4rqlScope) {
restriction.setAlias(String.format("%s_r%d", getAlias(), restrictions.size()));
restrictions.add(restriction);
} else {
throw new ParseException("A union block can not contain restrictions directly");
}
}
@Override
public void addExpressionScope(ScopeExists expressionScope) {
throw new ParseException("A union block can not contain expressions directly");
}
@Override
public ArrayList getRestrictions() {
return restrictions;
}
@Override
public ArrayList getExpressionScopes() {
return new ArrayList<>();
}
@Override
public void resetSolutions() {
for(Sp4rqlRestriction restriction : restrictions) {
if(restriction instanceof Sp4rqlScope) {
((Sp4rqlScope) restriction).resetSolutions();
}
}
}
@Override
public Collection getSolutions() {
return inScope.getSolutions();
}
@Override
public Sp4rqlSolution getSolution(VariableToken token) {
return inScope.getSolution(token);
}
@Override
public void setSolution(VariableToken token, Sp4rqlSolution solution) {
inScope.setSolution(token, solution);
}
@Override
public HashSet getExposedVariables() {
HashSet set = new HashSet<>();
for(Sp4rqlRestriction restriction : restrictions) {
set.addAll(restriction.getExposedVariables());
}
return set;
}
@Override
public void buildString(IndentStringBuilder builder) {
Iterator scopes = restrictions.iterator();
while(scopes.hasNext()) {
Sp4rqlRestriction subScope = scopes.next();
builder.appendLine(String.format("{ # %s", subScope.getAlias()), 1);
subScope.buildString(builder);
builder.newline(-1);
builder.append("}");
if(scopes.hasNext()) {
builder.newline();
builder.append("UNION");
builder.newline();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy