Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;

public final class Combinator implements Iterator<Pair<Origin, Map<Long, Term>>> {
public final class Combinator implements Iterator<Result<Pair<Origin, Map<Long, Term>>, Error>> {
private MatchedVariables variables;
private final List<Predicate> predicates;
private final FactSet facts;
private final TrustedOrigins scope;
private final Iterator<Pair<Origin, Fact>> currentFacts;
private Combinator currentIt;
private final SymbolTable symbolTable;
private final StepCounter steps;
private final StepBudget steps;
private Origin currentOrigin;

private Optional<Pair<Origin, Map<Long, Term>>> nextElement;
private Optional<Result<Pair<Origin, Map<Long, Term>>, Error>> nextElement;

@Override
public boolean hasNext() {
Expand All @@ -36,22 +38,20 @@ public boolean hasNext() {
}

@Override
public Pair<Origin, Map<Long, Term>> next() {
public Result<Pair<Origin, Map<Long, Term>>, Error> next() {
if (this.nextElement == null || this.nextElement.isEmpty()) {
this.nextElement = getNext();
}
if (this.nextElement == null || this.nextElement.isEmpty()) {
throw new NoSuchElementException();
} else {
Pair<Origin, Map<Long, Term>> t = this.nextElement.get();
Result<Pair<Origin, Map<Long, Term>>, Error> t = this.nextElement.get();
this.nextElement = Optional.empty();
return t;
}
}

public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
if (steps.exceeded()) return Optional.empty();

public Optional<Result<Pair<Origin, Map<Long, Term>>, Error>> getNext() {
if (this.predicates.isEmpty()) {
final Optional<Map<Long, Term>> vOpt = this.variables.complete();
if (vOpt.isEmpty()) {
Expand All @@ -66,7 +66,7 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
set.add((long) 0);

this.variables = new MatchedVariables(set);
return Optional.of(new Pair<>(new Origin(), variables));
return Optional.of(Result.ok(new Pair<>(new Origin(), variables)));
}
}

Expand All @@ -77,7 +77,11 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
while (true) {
// we iterate over the facts that match the current predicate
if (this.currentFacts.hasNext()) {
if (!this.steps.step()) return Optional.empty();
try {
this.steps.step();
} catch (Error.TooManySteps e) {
return Optional.of(Result.err(e));
}

final Pair<Origin, Fact> t = this.currentFacts.next();
Origin currentOrigin = t._1.clone();
Expand Down Expand Up @@ -121,7 +125,7 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
if (vOpt.isEmpty()) {
continue;
} else {
return Optional.of(new Pair<>(currentOrigin, vOpt.get()));
return Optional.of(Result.ok(new Pair<>(currentOrigin, vOpt.get())));
}
} else {
this.currentOrigin = currentOrigin;
Expand All @@ -145,11 +149,15 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
}
}

Optional<Pair<Origin, Map<Long, Term>>> opt = this.currentIt.getNext();
Optional<Result<Pair<Origin, Map<Long, Term>>, Error>> opt = this.currentIt.getNext();

if (opt.isPresent()) {
Pair<Origin, Map<Long, Term>> t = opt.get();
return Optional.of(new Pair<>(t._1.union(currentOrigin), t._2));
Result<Pair<Origin, Map<Long, Term>>, Error> res = opt.get();
if (res.isErr()) {
return opt;
}
Pair<Origin, Map<Long, Term>> t = res.getOk();
return Optional.of(Result.ok(new Pair<>(t._1.union(currentOrigin), t._2)));
} else {
currentOrigin = null;
currentIt = null;
Expand All @@ -163,7 +171,7 @@ public Combinator(
final FactSet facts,
final TrustedOrigins scope,
final SymbolTable symbolTable,
StepCounter steps) {
StepBudget steps) {
this.variables = variables;
this.facts = facts;
this.scope = scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public MatchedVariables(final Set<Long> ids) {
}

public Optional<Map<Long, Term>> checkExpressions(
List<Expression> expressions, SymbolTable symbolTable, StepCounter steps) throws Error {
List<Expression> expressions, SymbolTable symbolTable, StepBudget steps) throws Error {
final Optional<Map<Long, Term>> vars = this.complete();
if (vars.isPresent()) {
Map<Long, Term> variables = vars.get();
Expand Down
31 changes: 17 additions & 14 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,22 @@ public Stream<Result<Pair<Origin, Fact>, Error>> apply(
Long ruleOrigin,
TrustedOrigins scope,
SymbolTable symbolTable,
StepCounter steps) {
StepBudget steps) {
MatchedVariables variables = variablesSet();

Combinator combinator = new Combinator(variables, this.body, facts, scope, symbolTable, steps);

Spliterator<Pair<Origin, Map<Long, Term>>> splitItr =
Spliterator<Result<Pair<Origin, Map<Long, Term>>, Error>> splitItr =
Spliterators.spliteratorUnknownSize(combinator, Spliterator.ORDERED);
Stream<Pair<Origin, Map<Long, Term>>> stream = StreamSupport.stream(splitItr, false);
Stream<Result<Pair<Origin, Map<Long, Term>>, Error>> stream =
StreamSupport.stream(splitItr, false);

return stream.flatMap(
t -> {
res -> {
if (res.isErr()) {
return Stream.of(Result.err(res.getErr()));
}
Pair<Origin, Map<Long, Term>> t = res.getOk();
Origin origin = t._1;
Map<Long, Term> generatedVariables = t._2;
TemporarySymbolTable temporarySymbols = new TemporarySymbolTable(symbolTable);
Expand Down Expand Up @@ -117,7 +122,7 @@ public boolean findMatch(
Long origin,
TrustedOrigins scope,
SymbolTable symbolTable,
StepCounter steps)
StepBudget steps)
throws Error {
MatchedVariables variables = variablesSet();

Expand All @@ -128,11 +133,7 @@ public boolean findMatch(
var stream = this.apply(facts, origin, scope, symbolTable, steps);
var it = stream.iterator();

boolean res = it.hasNext();

if (steps.exceeded()) throw new Error.TooManySteps();

if (!res) {
if (!it.hasNext()) {
return false;
}

Expand All @@ -146,7 +147,7 @@ public boolean findMatch(

// verifies that the expressions return true for every matching set of facts
public boolean checkMatchAll(
final FactSet facts, TrustedOrigins scope, SymbolTable symbolTable, StepCounter steps)
final FactSet facts, TrustedOrigins scope, SymbolTable symbolTable, StepBudget steps)
throws Error {
MatchedVariables variables = variablesSet();

Expand All @@ -159,7 +160,11 @@ public boolean checkMatchAll(
boolean found = false;

for (Combinator it = combinator; it.hasNext(); ) {
Pair<Origin, Map<Long, Term>> t = it.next();
Result<Pair<Origin, Map<Long, Term>>, Error> res = it.next();
if (res.isErr()) {
throw res.getErr();
}
Pair<Origin, Map<Long, Term>> t = res.getOk();
Map<Long, Term> generatedVariables = t._2;
found = true;

Expand All @@ -179,8 +184,6 @@ public boolean checkMatchAll(
}
}

if (steps.exceeded()) throw new Error.TooManySteps();

return found;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public final class RunLimits {
private int maxFacts = 1000;
private int maxIterations = 100;
private Duration maxTime = Duration.ofMillis(5);
private int maxSteps = 1000;
private long maxSteps = 1000;

public RunLimits() {}

public RunLimits(int maxFacts, int maxIterations, Duration maxTime) {
this(maxFacts, maxIterations, maxTime, 1000);
}

public RunLimits(int maxFacts, int maxIterations, Duration maxTime, int maxSteps) {
public RunLimits(int maxFacts, int maxIterations, Duration maxTime, long maxSteps) {
this.maxFacts = maxFacts;
this.maxIterations = maxIterations;
this.maxTime = maxTime;
Expand All @@ -38,7 +38,7 @@ public Duration getMaxTime() {
return this.maxTime;
}

public int getMaxSteps() {
public long getMaxSteps() {
return this.maxSteps;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.eclipse.biscuit.datalog;

import org.eclipse.biscuit.error.Error;

public final class StepBudget {
private final StepCounter counter;
private final long maxSteps;

StepBudget(StepCounter counter, long maxSteps) {
this.counter = counter;
this.maxSteps = maxSteps;
}

public void step() throws Error.TooManySteps {
if (++counter.count > maxSteps) {
throw new Error.TooManySteps();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package org.eclipse.biscuit.datalog;

public class StepCounter implements Cloneable {
private int counter;
private final int maxSteps;
public final class StepCounter {
long count;

public StepCounter(int maxSteps) {
this.maxSteps = maxSteps;
this.counter = 0;
public StepCounter() {
this(0);
}

public boolean step() {
return ++counter <= maxSteps;
public StepCounter(long taken) {
this.count = taken;
}

public boolean exceeded() {
return counter > maxSteps;
public long taken() {
return this.count;
}

@Override
public StepCounter clone() {
try {
return (StepCounter) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
public StepBudget budget(long maxSteps) {
return new StepBudget(this, maxSteps);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void evaluate(
Deque<Op> stack,
java.util.Map<Long, Term> variables,
TemporarySymbolTable temporarySymbolTable,
StepCounter steps)
StepBudget steps)
throws Error.Execution {
if (this instanceof Term.Variable) {
Term.Variable var = (Term.Variable) this;
Expand Down
Loading