Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
82e56f4
Stratified aggregation
guillembartrina Oct 19, 2023
ae94fe6
Add some tests and examples, fix broken tests
guillembartrina Oct 19, 2023
9c84a88
Handle anonymous variables in grouping atoms, add a few examples
guillembartrina Oct 22, 2023
9fab8d6
Simplify Grouping volcano operator, add constants generated by aggreg…
guillembartrina Oct 23, 2023
84cf40f
Remove unused AST parameter
guillembartrina Oct 26, 2023
5a746a5
Built-in constraints
guillembartrina Oct 30, 2023
21668b7
Built-in constraints
guillembartrina Oct 30, 2023
e5e71c5
Merge branch 'builtin-constraints' of github.com:guillembartrina/cara…
guillembartrina Oct 30, 2023
80f636c
Move creation of static operations, finish StagedSnippet
guillembartrina Nov 1, 2023
eb24a37
Built-in constraints
guillembartrina Oct 30, 2023
5f3088c
Move creation of static operations
guillembartrina Nov 1, 2023
2176b5a
Merge branch 'builtin-constraints' of github.com:guillembartrina/cara…
guillembartrina Nov 1, 2023
0f323c5
Optimize negation: avoid combinatorial explosion
guillembartrina Nov 5, 2023
9bb0349
Add synthetic test
guillembartrina Nov 5, 2023
e062d6e
Add bugfix comment
guillembartrina Nov 7, 2023
5a18074
Optimize negation: avoid combinatorial explosion
guillembartrina Nov 5, 2023
43451f4
Add synthetic test
guillembartrina Nov 5, 2023
0edb4a3
Merge branch 'improved-negation' of github.com:guillembartrina/carac …
guillembartrina Nov 7, 2023
dea321d
Quick fix: Prohibit negated variables from being guarded by aggregate…
guillembartrina Nov 12, 2023
61a817b
Merge branch 'main' into improved-negation
guillembartrina Nov 12, 2023
cdbd3ae
Simplify negation suboperations
guillembartrina Nov 12, 2023
9c4313f
Merge branch 'improved-negation' into builtin-constraints
guillembartrina Nov 12, 2023
5d4b52c
Remove metals files
guillembartrina Nov 12, 2023
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
Prev Previous commit
Next Next commit
Quick fix: Prohibit negated variables from being guarded by aggregate…
…d variables
  • Loading branch information
guillembartrina committed Nov 12, 2023
commit dea321d65d7379986fa5bab81f923b92115558f0
25 changes: 12 additions & 13 deletions src/main/scala/datalog/execution/JoinIndexes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ case class GroupingJoinIndexes(varIndexes: Seq[Seq[Int]],
* @param edb - for rules that have EDBs defined on the same predicate, just read
* @param atoms - the original atoms from the DSL
* @param cxns - convenience data structure tracking how many variables in common each atom has with every other atom.
* @param negationInfo - information needed to build the complement relation of negated atoms: for each term, either a constant or a list of pairs (relationid, column) of the ocurrences of the variable in the rule (empty for anonynous variable)
*/
case class JoinIndexes(varIndexes: Seq[Seq[Int]],
constIndexes: mutable.Map[Int, Constant],
Expand Down Expand Up @@ -92,23 +93,19 @@ object JoinIndexes {
case _ => if (a.negated) PredicateType.NEGATED else PredicateType.POSITIVE
, a.rId))

val typeHelper = body.flatMap(a => a.terms.map(* => !a.negated))

val bodyVars = body
.flatMap(a => a.terms) // all terms in one seq
.flatMap(a => a.terms.zipWithIndex.map((t, i) => (t, (a.negated, a.isInstanceOf[GroupingAtom] && i >= a.asInstanceOf[GroupingAtom].gv.length)))) // all terms in one seq
.zipWithIndex // term, position
.groupBy(z => z._1) // group by term
.groupBy(z => z._1._1) // group by term
.filter((term, matches) => // matches = Seq[(var, pos1), (var, pos2), ...]
term match {
case v: Variable =>
matches.map(_._2).find(typeHelper) match
case Some(pos) =>
variables(v) = pos
case None =>
if (v.oid != -1)
throw new Exception(s"Variable with varId ${v.oid} appears only in negated rules")
else
()
val wrong = v.oid != -1 && matches.exists(_._1._2._1) && matches.forall(x => x._1._2._1 || x._1._2._2) // Var occurs negated and all occurrences are either negated or aggregated
if wrong then
throw new Exception(s"Variable with varId ${v.oid} appears only in negated atoms (and possibly in aggregated positions of grouping atoms)")
else
if (v.oid != -1)
variables(v) = matches.find(!_._1._2._1).get._2
!v.anon && matches.length >= 2
case c: Constant =>
matches.foreach((_, idx) => constants(idx) = c)
Expand Down Expand Up @@ -147,7 +144,9 @@ object JoinIndexes {
)


val variables2 = body.filterNot(_.negated).flatMap(a => a.terms.zipWithIndex.collect{ case (v: Variable, i) if !v.anon => (v, i) }.map((v, i) => (v, (a.rId, i)))).groupBy(_._1).view.mapValues(_.map(_._2))
val variables2 = body.filterNot(_.negated).flatMap(a =>
a.terms.zipWithIndex.collect{ case (v: Variable, i) if !v.anon => (v, i) }.map((v, i) => (v, (a.rId, i)))
).groupBy(_._1).view.mapValues(_.map(_._2))

val negationInfo = body.filter(_.negated).map(a =>
a.hash -> a.terms.map{
Expand Down
20 changes: 17 additions & 3 deletions src/test/scala/test/StratifiedNegationTests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package test

import datalog.dsl.{Constant, Program, __, not}
import datalog.dsl.{Constant, Program, __, not, groupBy, AggOp}
import datalog.execution.*
import datalog.storage.DefaultStorageManager

Expand All @@ -18,7 +18,7 @@ class StratifiedNegationTests extends munit.FunSuite {
t(x, y) :- e(x, y)
t(x, z) :- (t(x, y), e(y, z))

interceptMessage[java.lang.Exception]("Variable with varId 0 appears only in negated rules") {
interceptMessage[java.lang.Exception]("Variable with varId 0 appears only in negated atoms (and possibly in aggregated positions of grouping atoms)") {
tc(x, y) :- not(t(x, y)) // x and y are not limited.
}
}
Expand All @@ -35,7 +35,7 @@ class StratifiedNegationTests extends munit.FunSuite {
t(x, y) :- e(x, y)
t(x, z) :- (t(x, y), e(y, z))

interceptMessage[java.lang.Exception]("Variable with varId 2 appears only in negated rules") {
interceptMessage[java.lang.Exception]("Variable with varId 2 appears only in negated atoms (and possibly in aggregated positions of grouping atoms)") {
tc(x, y) :- (e(x, y), !e(x, z), !e(x, z))
}
}
Expand All @@ -52,4 +52,18 @@ class StratifiedNegationTests extends munit.FunSuite {
p.solve(e.id)
}
}

test("stratified negation with aggregation") {
val p = Program(new StagedExecutionEngine(new DefaultStorageManager()))
val a = p.relation[Constant]("a")
val b = p.relation[Constant]("b")
val x, y = p.variable()

a("A", 1) :- ()
a("B", 2) :- ()

interceptMessage[java.lang.Exception]("Variable with varId 0 appears only in negated atoms (and possibly in aggregated positions of grouping atoms)") {
b(x) :- (!a(__, x), groupBy(a(__, y), Seq(), AggOp.SUM(y) -> x))
}
}
}