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 @@ -13,13 +13,12 @@
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Stream;

public final class Combinator implements Serializable, Iterator<Pair<Origin, Map<Long, Term>>> {
private MatchedVariables variables;
private final Supplier<Stream<Pair<Origin, Fact>>> allFacts;
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;
Expand Down Expand Up @@ -85,6 +84,11 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
Origin currentOrigin = t._1.clone();
Fact fact = t._2;

// the candidates only share the predicate name and arity, check the terms
if (!fact.matchPredicate(predicate)) {
continue;
}

// create a new MatchedVariables in which we fix variables we could unify from our first
// predicate and the current fact
MatchedVariables vars = this.variables.clone();
Expand Down Expand Up @@ -129,7 +133,8 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
new Combinator(
vars,
predicates.subList(1, predicates.size()),
this.allFacts,
this.facts,
this.scope,
this.symbolTable,
this.steps);
}
Expand All @@ -156,15 +161,16 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
public Combinator(
final MatchedVariables variables,
final List<Predicate> predicates,
Supplier<Stream<Pair<Origin, Fact>>> allFacts,
final FactSet facts,
final TrustedOrigins scope,
final SymbolTable symbolTable,
StepCounter steps) {
this.variables = variables;
this.allFacts = allFacts;
this.facts = facts;
this.scope = scope;
this.currentIt = null;
this.predicates = predicates;
this.currentFacts =
allFacts.get().filter((tuple) -> tuple._2.matchPredicate(predicates.get(0))).iterator();
this.currentFacts = facts.stream(predicates.get(0), scope).iterator();
this.symbolTable = symbolTable;
this.currentOrigin = null;
this.nextElement = null;
Expand Down
78 changes: 51 additions & 27 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,54 @@

package org.eclipse.biscuit.datalog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public final class FactSet {
private final HashMap<Origin, HashSet<Fact>> facts;
/** facts grouped by (predicate name, arity), then by origin */
private final HashMap<Pair<Long, Integer>, HashMap<Origin, HashSet<Fact>>> facts;

public FactSet() {
facts = new HashMap<>();
}

public FactSet(Origin o, HashSet<Fact> factSet) {
facts = new HashMap<>();
facts.put(o, factSet);
for (Fact fact : factSet) {
add(o, fact);
}
}

public Map<Origin, List<Fact>> byOrigin() {
Map<Origin, List<Fact>> origins = new HashMap<>();
for (HashMap<Origin, HashSet<Fact>> group : facts.values()) {
for (Map.Entry<Origin, HashSet<Fact>> entry : group.entrySet()) {
origins.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).addAll(entry.getValue());
}
}
return origins;
}

public HashMap<Origin, HashSet<Fact>> facts() {
return this.facts;
private static Pair<Long, Integer> keyOf(Predicate predicate) {
return new Pair<>(predicate.name(), predicate.terms().size());
}

public void add(Origin origin, Fact fact) {
if (!facts.containsKey(origin)) {
facts.put(origin, new HashSet<>());
}
facts.get(origin).add(fact);
Pair<Long, Integer> key = keyOf(fact.predicate());
HashMap<Origin, HashSet<Fact>> group = facts.computeIfAbsent(key, k -> new HashMap<>());
group.computeIfAbsent(origin, k -> new HashSet<>()).add(fact);
}

public int size() {
int size = 0;
for (HashSet<Fact> h : facts.values()) {
size += h.size();
for (HashMap<Origin, HashSet<Fact>> group : facts.values()) {
for (HashSet<Fact> h : group.values()) {
size += h.size();
}
}

return size;
Expand All @@ -45,36 +61,44 @@ public int size() {
public FactSet clone() {
FactSet newFacts = new FactSet();

for (Map.Entry<Origin, HashSet<Fact>> entry : this.facts.entrySet()) {
HashSet<Fact> h = new HashSet<>(entry.getValue());
newFacts.facts.put(entry.getKey(), h);
for (Map.Entry<Pair<Long, Integer>, HashMap<Origin, HashSet<Fact>>> group :
this.facts.entrySet()) {
HashMap<Origin, HashSet<Fact>> newGroup = new HashMap<>();
for (Map.Entry<Origin, HashSet<Fact>> entry : group.getValue().entrySet()) {
newGroup.put(entry.getKey(), new HashSet<>(entry.getValue()));
}
newFacts.facts.put(group.getKey(), newGroup);
}

return newFacts;
}

public void merge(FactSet other) {
for (Map.Entry<Origin, HashSet<Fact>> entry : other.facts.entrySet()) {
if (!facts.containsKey(entry.getKey())) {
facts.put(entry.getKey(), entry.getValue());
} else {
facts.get(entry.getKey()).addAll(entry.getValue());
for (Map.Entry<Pair<Long, Integer>, HashMap<Origin, HashSet<Fact>>> group :
other.facts.entrySet()) {
HashMap<Origin, HashSet<Fact>> ownGroup =
facts.computeIfAbsent(group.getKey(), k -> new HashMap<>());
for (Map.Entry<Origin, HashSet<Fact>> entry : group.getValue().entrySet()) {
ownGroup.computeIfAbsent(entry.getKey(), k -> new HashSet<>()).addAll(entry.getValue());
}
}
}

public Stream<Pair<Origin, Fact>> stream(TrustedOrigins blockIds) {
return facts.entrySet().stream()
.filter(
entry -> {
Origin o = entry.getKey();
return blockIds.contains(o);
})
/** trusted facts sharing the predicate's name and arity, the terms are not checked */
public Stream<Pair<Origin, Fact>> stream(Predicate predicate, TrustedOrigins blockIds) {
HashMap<Origin, HashSet<Fact>> group = facts.get(keyOf(predicate));
if (group == null) {
return Stream.empty();
}
return group.entrySet().stream()
.filter(entry -> blockIds.contains(entry.getKey()))
.flatMap(entry -> entry.getValue().stream().map(fact -> new Pair<>(entry.getKey(), fact)));
}

public Stream<Fact> stream() {
return facts.entrySet().stream().flatMap(entry -> entry.getValue().stream());
return facts.values().stream()
.flatMap(group -> group.values().stream())
.flatMap(HashSet::stream);
}

@Override
Expand All @@ -99,7 +123,7 @@ public int hashCode() {
@Override
public String toString() {
StringBuilder res = new StringBuilder("FactSet {");
for (Map.Entry<Origin, HashSet<Fact>> entry : this.facts.entrySet()) {
for (Map.Entry<Origin, List<Fact>> entry : this.byOrigin().entrySet()) {
res.append("\n\t").append(entry.getKey()).append("[");
for (Fact fact : entry.getValue()) {
res.append("\n\t\t").append(fact);
Expand Down
16 changes: 6 additions & 10 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -46,13 +45,11 @@ public List<Scope> scopes() {
}

public Stream<Result<Pair<Origin, Fact>, Error>> apply(
final Supplier<Stream<Pair<Origin, Fact>>> factsSupplier,
Long ruleOrigin,
SymbolTable symbolTable,
StepCounter steps) {
final FactSet facts, Long ruleOrigin, TrustedOrigins scope, SymbolTable symbolTable, StepCounter steps) {
MatchedVariables variables = variablesSet();

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

Spliterator<Pair<Origin, Map<Long, Term>>> splitItr =
Spliterators.spliteratorUnknownSize(combinator, Spliterator.ORDERED);
Stream<Pair<Origin, Map<Long, Term>>> stream = StreamSupport.stream(splitItr, false);
Expand Down Expand Up @@ -125,8 +122,7 @@ public boolean findMatch(
return variables.checkExpressions(this.expressions, symbolTable, steps).isPresent();
}

Supplier<Stream<Pair<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
var stream = this.apply(factsSupplier, origin, symbolTable, steps);
var stream = this.apply(facts, origin, scope, symbolTable, steps);
var it = stream.iterator();

boolean res = it.hasNext();
Expand Down Expand Up @@ -155,8 +151,8 @@ public boolean checkMatchAll(
return variables.checkExpressions(this.expressions, symbolTable, steps).isPresent();
}

Supplier<Stream<Pair<Origin, Fact>>> factsSupplier = () -> facts.stream(scope);
Combinator combinator = new Combinator(variables, this.body, factsSupplier, symbolTable, steps);
Combinator combinator = new Combinator(variables, this.body, facts, scope, symbolTable, steps);

boolean found = false;

for (Combinator it = combinator; it.hasNext(); ) {
Expand Down
14 changes: 3 additions & 11 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@

import java.io.Serializable;
import java.time.Instant;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.eclipse.biscuit.error.Error;

public final class World implements Serializable {
Expand Down Expand Up @@ -47,10 +44,7 @@ public void run(RunLimits limits, final SymbolTable symbolTable) throws Error {
for (Map.Entry<TrustedOrigins, List<Pair<Long, Rule>>> entry :
this.rules.getRules().entrySet()) {
for (Pair<Long, Rule> t : entry.getValue()) {
Supplier<Stream<Pair<Origin, Fact>>> factsSupplier =
() -> this.facts.stream(entry.getKey());

var stream = t._2.apply(factsSupplier, t._1, symbolTable, this.steps);
var stream = t._2.apply(this.facts, t._1, entry.getKey(), symbolTable, this.steps);
for (var it = stream.iterator(); it.hasNext(); ) {
var res = it.next();
if (Instant.now().compareTo(limit) >= 0) {
Expand Down Expand Up @@ -99,9 +93,7 @@ public FactSet queryRule(
final Rule rule, Long origin, TrustedOrigins scope, SymbolTable symbolTable) throws Error {
final FactSet newFacts = new FactSet();

Supplier<Stream<Pair<Origin, Fact>>> factsSupplier = () -> this.facts.stream(scope);

var stream = rule.apply(factsSupplier, origin, symbolTable, this.steps);
var stream = rule.apply(this.facts, origin, scope, symbolTable, this.steps);
for (var it = stream.iterator(); it.hasNext(); ) {
var res = it.next();

Expand Down Expand Up @@ -156,7 +148,7 @@ public String print(SymbolTable symbolTable) {
StringBuilder s = new StringBuilder();

s.append("World {\n\t\tfacts: [");
for (Map.Entry<Origin, HashSet<Fact>> entry : this.facts.facts().entrySet()) {
for (Map.Entry<Origin, List<Fact>> entry : this.facts.byOrigin().entrySet()) {
s.append("\n\t\t\t").append(entry.getKey()).append(":");
for (Fact f : entry.getValue()) {
s.append("\n\t\t\t\t");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ public Long authorize(RunLimits limits) throws Error {

public String formatWorld() {
StringBuilder facts = new StringBuilder();
for (Map.Entry<Origin, HashSet<org.eclipse.biscuit.datalog.Fact>> entry :
this.world.getFacts().facts().entrySet()) {
for (Map.Entry<Origin, List<org.eclipse.biscuit.datalog.Fact>> entry :
this.world.getFacts().byOrigin().entrySet()) {
facts.append("\n\t\t").append(entry.getKey()).append(":");
for (org.eclipse.biscuit.datalog.Fact f : entry.getValue()) {
facts.append("\n\t\t\t");
Expand Down
41 changes: 41 additions & 0 deletions biscuit/src/test/java/org/eclipse/biscuit/datalog/WorldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.Instant;
Expand Down Expand Up @@ -775,4 +776,44 @@ public void testAddRuleEqualButDistinctTrustedOriginsDoesNotThrow() {
});
assertEquals(1, w.getRules().getRules().size());
}

@Test
public void testFactSetEquality() {
final SymbolTable syms = new SymbolTable();
final long f = syms.insert("f");
final long g = syms.insert("g");
final Fact f1 = new Fact(new Predicate(f, List.of(new Term.Integer(1))));
final Fact g12 = new Fact(new Predicate(g, List.of(new Term.Integer(1), new Term.Integer(2))));

final FactSet first = new FactSet();
first.add(new Origin(0), f1);
first.add(new Origin(1), g12);

// same facts and origins, added in another order
final FactSet reordered = new FactSet();
reordered.add(new Origin(1), g12);
reordered.add(new Origin(0), f1);
assertEquals(first, reordered);
assertEquals(first.hashCode(), reordered.hashCode());

// the same facts reached through clone and merge
assertEquals(first, first.clone());
final FactSet merged = new FactSet();
merged.add(new Origin(0), f1);
final FactSet rest = new FactSet();
rest.add(new Origin(1), g12);
merged.merge(rest);
assertEquals(first, merged);
assertEquals(first.hashCode(), merged.hashCode());

// a fact under another origin is a different set, and so is a missing fact
final FactSet otherOrigin = new FactSet();
otherOrigin.add(new Origin(2), f1);
otherOrigin.add(new Origin(1), g12);
assertNotEquals(first, otherOrigin);

final FactSet missing = new FactSet();
missing.add(new Origin(0), f1);
assertNotEquals(first, missing);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public World(

public World(Authorizer authorizer) {
this.facts =
authorizer.getFacts().facts().entrySet().stream()
authorizer.getFacts().byOrigin().entrySet().stream()
.map(
entry ->
new FactSet(
Expand Down
Loading