Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jan 3, 2024
1 parent bc9b7f7 commit 4881d20
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 181 deletions.
14 changes: 0 additions & 14 deletions src/main/java/com/clevercloud/biscuit/datalog/AuthorizedWorld.java

This file was deleted.

12 changes: 6 additions & 6 deletions src/main/java/com/clevercloud/biscuit/datalog/Combinator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class Combinator implements Serializable, Iterator<Tuple2<Origin, M

@Override
public boolean hasNext() {
if(this.nextElement != null && this.nextElement.isDefined()) {
if (this.nextElement != null && this.nextElement.isDefined()) {
return true;
}
this.nextElement = getNext();
Expand All @@ -31,10 +31,10 @@ public boolean hasNext() {

@Override
public Tuple2<Origin, Map<Long, Term>> next() {
if(this.nextElement == null || !this.nextElement.isDefined()) {
if (this.nextElement == null || !this.nextElement.isDefined()) {
this.nextElement = getNext();
}
if(this.nextElement == null || !this.nextElement.isDefined()) {
if (this.nextElement == null || !this.nextElement.isDefined()) {
throw new NoSuchElementException();
} else {
Tuple2<Origin, Map<Long, Term>> t = this.nextElement.get();
Expand All @@ -46,7 +46,7 @@ public Tuple2<Origin, Map<Long, Term>> next() {
public Option<Tuple2<Origin, Map<Long, Term>>> getNext() {
if (this.predicates.isEmpty()) {
final Option<Map<Long, Term>> v_opt = this.variables.complete();
if(v_opt.isEmpty()) {
if (v_opt.isEmpty()) {
return Option.none();
} else {
Map<Long, Term> variables = v_opt.get();
Expand All @@ -55,14 +55,14 @@ public Option<Tuple2<Origin, Map<Long, Term>>> getNext() {
// successful calls, we create a set of variables that cannot
// possibly be completed, so the next call will fail
Set<Long> set = new HashSet();
set.add((long)0);
set.add((long) 0);

this.variables = new MatchedVariables(set);
return Option.some(new Tuple2(new Origin(), variables));
}
}

while(true) {
while (true) {
if (this.currentIt == null) {
Predicate predicate = this.predicates.get(0);

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/clevercloud/biscuit/datalog/FactSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.vavr.Tuple2;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FactSet {
Expand Down Expand Up @@ -55,7 +54,7 @@ public void merge(FactSet other) {
}
}
}
public Stream iterator(TrustedOrigins blockIds) {
public Stream stream(TrustedOrigins blockIds) {
return facts.entrySet()
.stream()
.filter(entry -> {
Expand All @@ -67,7 +66,7 @@ public Stream iterator(TrustedOrigins blockIds) {
.map(fact -> new Tuple2(entry.getKey(), fact)));
}

public Stream<Fact> iterator() {
public Stream<Fact> stream() {
return facts.entrySet()
.stream()
.flatMap(entry -> entry.getValue()
Expand Down
96 changes: 45 additions & 51 deletions src/main/java/com/clevercloud/biscuit/datalog/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,56 +50,50 @@ public Stream<Either<Error, Tuple2<Origin, Fact>>> apply(
.spliteratorUnknownSize(combinator, Spliterator.ORDERED);
Stream<Tuple2<Origin, Map<Long, Term>>> stream = StreamSupport.stream(splitItr, false);

//somehow we have inference errors when writing this as a lambda
return stream.map(t -> {
Origin origin = t._1;
Map<Long, Term> generatedVariables = t._2;
TemporarySymbolTable temporarySymbols = new TemporarySymbolTable(symbols);
for (Expression e : this.expressions) {
Option<Term> res = e.evaluate(generatedVariables, temporarySymbols);
if (res.isDefined()) {
Term term = res.get();
if (term instanceof Term.Bool) {
Term.Bool b = (Term.Bool) term;
if (!b.value()) {
return Either.right(new Tuple3(origin, generatedVariables, false));
}
// continue evaluating if true
} else {
return Either.left(new InvalidType());
}
}
}
return Either.right(new Tuple3(origin, generatedVariables, true));
/*
.filter((java.util.function.Predicate<? super Either<? extends Object, ? extends Object>>) new java.util.function.Predicate<Either<Error, Tuple3<Origin, Map<Long, Term>, Boolean>>>() {
//somehow we have inference errors when writing this as a lambda
@Override
public boolean test(Either<Error, Tuple3<Origin, Map<Long, Term>, Boolean>> res) {
return res.isRight() & res.get()._3.booleanValue();
}
})*/
}).filter((java.util.function.Predicate<? super Either<? extends Object, ? extends Object>>)
res -> res.isRight() & ((Tuple3<Origin, Map<Long, Term>, Boolean>)res.get())._3.booleanValue()).map(res -> {
Tuple3<Origin, Map<Long, Term>, Boolean> t = (Tuple3<Origin, Map<Long, Term>, Boolean>) res.get();
Origin origin = t._1;
Map<Long, Term> generatedVariables = t._2;

Predicate p = this.head.clone();
for (int index = 0; index < p.terms().size(); index++) {
if (p.terms().get(index) instanceof Term.Variable) {
Term.Variable var = (Term.Variable) p.terms().get(index);
if(!generatedVariables.containsKey(var.value())) {
//throw new Error("variables that appear in the head should appear in the body as well");
return Either.left(new Error.InternalError());
}
p.terms().set(index, generatedVariables.get(var.value()));
}
}
//somehow we have inference errors when writing this as a lambda
return stream.map(t -> {
Origin origin = t._1;
Map<Long, Term> generatedVariables = t._2;
TemporarySymbolTable temporarySymbols = new TemporarySymbolTable(symbols);
for (Expression e : this.expressions) {
Option<Term> res = e.evaluate(generatedVariables, temporarySymbols);
if (res.isDefined()) {
Term term = res.get();
if (term instanceof Term.Bool) {
Term.Bool b = (Term.Bool) term;
if (!b.value()) {
return Either.right(new Tuple3(origin, generatedVariables, false));
}
// continue evaluating if true
} else {
return Either.left(new InvalidType());
}
}
}
return Either.right(new Tuple3(origin, generatedVariables, true));
})
// sometimes we need to make the compiler happy
.filter((java.util.function.Predicate<? super Either<? extends Object, ? extends Object>>)
res -> res.isRight() & ((Tuple3<Origin, Map<Long, Term>, Boolean>) res.get())._3.booleanValue()).map(res -> {
Tuple3<Origin, Map<Long, Term>, Boolean> t = (Tuple3<Origin, Map<Long, Term>, Boolean>) res.get();
Origin origin = t._1;
Map<Long, Term> generatedVariables = t._2;

Predicate p = this.head.clone();
for (int index = 0; index < p.terms().size(); index++) {
if (p.terms().get(index) instanceof Term.Variable) {
Term.Variable var = (Term.Variable) p.terms().get(index);
if (!generatedVariables.containsKey(var.value())) {
//throw new Error("variables that appear in the head should appear in the body as well");
return Either.left(new Error.InternalError());
}
p.terms().set(index, generatedVariables.get(var.value()));
}
}

origin.add(ruleOrigin);
return Either.right(new Tuple2<Origin, Fact>(origin, new Fact(p)));
});
origin.add(ruleOrigin);
return Either.right(new Tuple2<Origin, Fact>(origin, new Fact(p)));
});
}

private MatchedVariables variablesSet() {
Expand All @@ -119,7 +113,7 @@ public boolean find_match(final FactSet facts, Long origin, TrustedOrigins scope
return variables.check_expressions(this.expressions, symbols).isDefined();
}

Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier = () -> facts.iterator(scope);
Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
Stream<Either<Error, Tuple2<Origin, Fact>>> stream = this.apply(factsSupplier, origin, symbols);

Iterator<Either<Error, Tuple2<Origin, Fact>>> it = stream.iterator();
Expand All @@ -144,7 +138,7 @@ public boolean check_match_all(final FactSet facts, TrustedOrigins scope, Symbol
return variables.check_expressions(this.expressions, symbols).isDefined();
}

Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier = () -> facts.iterator(scope);
Supplier<Stream<Tuple2<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
Combinator combinator = new Combinator(variables, this.body, factsSupplier, symbols);
boolean found = false;

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/clevercloud/biscuit/datalog/RuleSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public RuleSet() {
}

public void add(Long origin, TrustedOrigins scope, Rule rule) {
if(!rules.containsKey(scope)) {
if (!rules.containsKey(scope)) {
rules.put(scope, Arrays.asList(new Tuple2(origin, rule)));
} else {
rules.get(scope).add(new Tuple2(origin, rule));
Expand All @@ -23,7 +23,7 @@ public void add(Long origin, TrustedOrigins scope, Rule rule) {
public RuleSet clone() {
RuleSet newRules = new RuleSet();

for(Map.Entry<TrustedOrigins, List<Tuple2<Long, Rule>>> entry: this.rules.entrySet()) {
for (Map.Entry<TrustedOrigins, List<Tuple2<Long, Rule>>> entry : this.rules.entrySet()) {
List<Tuple2<Long, Rule>> l = new ArrayList<>();
l.addAll(entry.getValue());
newRules.rules.put(entry.getKey(), l);
Expand All @@ -32,13 +32,14 @@ public RuleSet clone() {
return newRules;
}

public Stream<Rule> iterator() {
public Stream<Rule> stream() {
return rules.entrySet()
.stream()
.flatMap(entry -> entry.getValue()
.stream()
.map(t -> t._2));
}

public void clear() {
rules.clear();
}
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/com/clevercloud/biscuit/datalog/SchemaVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ public class SchemaVersion {
private boolean containsV4;

public SchemaVersion(List<Fact> facts, List<Rule> rules, List<Check> checks, List<Scope> scopes) {
// TODO
containsScopes = !scopes.isEmpty();

if(!containsScopes) {
for(Rule r: rules) {
if (!containsScopes) {
for (Rule r : rules) {
if (!r.scopes().isEmpty()) {
containsScopes = true;
break;
}
}
}
if(!containsScopes) {
for(Check check: checks) {
for(Rule query: check.queries()) {
if(!query.scopes().isEmpty()) {
if (!containsScopes) {
for (Check check : checks) {
for (Rule query : check.queries()) {
if (!query.scopes().isEmpty()) {
containsScopes = true;
break;
}
Expand All @@ -41,16 +40,16 @@ public SchemaVersion(List<Fact> facts, List<Rule> rules, List<Check> checks, Lis
}

containsCheckAll = false;
for(Check check: checks) {
for (Check check : checks) {
if (check.kind() == All) {
containsCheckAll = true;
break;
}
}

containsV4 = false;
for(Check check: checks) {
for(Rule query: check.queries()) {
for (Check check : checks) {
for (Rule query : check.queries()) {
if (containsV4Ops(query.expressions())) {
containsV4 = true;
break;
Expand All @@ -61,7 +60,7 @@ public SchemaVersion(List<Fact> facts, List<Rule> rules, List<Check> checks, Lis

public int version() {
if (containsScopes || containsV4 || containsCheckAll) {
return 4;
return 4;
} else {
return MIN_SCHEMA_VERSION;
}
Expand All @@ -72,10 +71,10 @@ public Either<Error.FormatError, Void> checkCompatibility(int version) {
if (containsScopes) {
return Left(new Error.FormatError.DeserializationError("v3 blocks must not have scopes"));
}
if(containsV4) {
if (containsV4) {
return Left(new Error.FormatError.DeserializationError("v3 blocks must not have v4 operators (bitwise operators or !="));
}
if(containsCheckAll) {
if (containsCheckAll) {
return Left(new Error.FormatError.DeserializationError("v3 blocks must not use check all"));
}
}
Expand All @@ -84,8 +83,8 @@ public Either<Error.FormatError, Void> checkCompatibility(int version) {
}

public static boolean containsV4Ops(List<Expression> expressions) {
for(Expression e: expressions) {
for (Op op: e.getOps()) {
for (Expression e : expressions) {
for (Op op : e.getOps()) {
if (op instanceof Op.Binary) {
Op.Binary b = (Op.Binary) op;
switch (b.getOp()) {
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/clevercloud/biscuit/datalog/Scope.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.clevercloud.biscuit.datalog;

import biscuit.format.schema.Schema;
import com.clevercloud.biscuit.datalog.expressions.Expression;
import com.clevercloud.biscuit.error.Error;
import io.vavr.control.Either;

import java.util.ArrayList;

import static io.vavr.API.Left;
import static io.vavr.API.Right;
Expand All @@ -24,6 +22,7 @@ private Scope(Kind kind, long publicKey) {
this.kind = kind;
this.publicKey = publicKey;
}

public static Scope authority() {
return new Scope(Kind.Authority, 0);
}
Expand All @@ -47,7 +46,7 @@ public long publicKey() {
public Schema.Scope serialize() {
Schema.Scope.Builder b = Schema.Scope.newBuilder();

switch(this.kind) {
switch (this.kind) {
case Authority:
b.setScopeType(Schema.Scope.ScopeType.Authority);
break;
Expand All @@ -66,8 +65,8 @@ static public Either<Error.FormatError, Scope> deserialize(Schema.Scope scope) {
long publicKey = scope.getPublicKey();
return Right(Scope.publicKey(publicKey));
}
if(scope.hasScopeType()) {
switch(scope.getScopeType()) {
if (scope.hasScopeType()) {
switch (scope.getScopeType()) {
case Authority:
return Right(Scope.authority());
case Previous:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ public String print_check(final Check c) {
}

public String print_world(final World w) {
final List<String> facts = w.facts().iterator().map((f) -> this.print_fact(f)).collect(Collectors.toList());
final List<String> rules = w.rules().iterator().map((r) -> this.print_rule(r)).collect(Collectors.toList());
final List<String> facts = w.facts().stream().map((f) -> this.print_fact(f)).collect(Collectors.toList());
final List<String> rules = w.rules().stream().map((r) -> this.print_rule(r)).collect(Collectors.toList());

StringBuilder b = new StringBuilder();
b.append("World {\n\tfacts: [\n\t\t");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public TemporarySymbolTable(SymbolTable base) {
}

public Option<String> get_s(int i) {
if(i >= this.offset) {
if(i - this.offset < this.symbols.size()) {
if (i >= this.offset) {
if (i - this.offset < this.symbols.size()) {
return Option.some(this.symbols.get(i - this.offset));
} else {
return Option.none();
Expand All @@ -32,12 +32,12 @@ public Option<String> get_s(int i) {

public long insert(final String symbol) {
Option<Long> opt = this.base.get(symbol);
if(opt.isDefined()) {
if (opt.isDefined()) {
return opt.get().longValue();
}

int index = this.symbols.indexOf(symbol);
if(index != -1) {
if (index != -1) {
return (long) (this.offset + index);
}
this.symbols.add(symbol);
Expand Down
Loading

0 comments on commit 4881d20

Please sign in to comment.