Skip to content

Commit

Permalink
Faster handling of facts in generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
vale1410 committed Jun 26, 2021
1 parent 4b08e74 commit 31de03f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
16 changes: 12 additions & 4 deletions cmd/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package cmd

import (
"fmt"
bule "github.com/vale1410/bule/grounder"
"log"
"os"
"strconv"
"time"

bule "github.com/vale1410/bule/grounder"
)

func stage0Prerequisites(p *bule.Program) {
Expand Down Expand Up @@ -67,9 +70,10 @@ func stage1GeneratorsAndFacts(p *bule.Program) {
runFixpoint(p.ExpandGroundRanges),
"ExpandGroundRanges",
"p[1..2]. and also X==1..2, but not Y==A..B.")

stage(p, &changed,
p.ConstraintSimplification,
"Do Fixpoint of TransformConstraintsToInstantiation.",
"ConstraintSimplification.",
"For each constraint (X==v) rewrite clause with (X<-v) and remove constraint.")

stage(p, &changed,
Expand All @@ -89,7 +93,7 @@ func stage1GeneratorsAndFacts(p *bule.Program) {

stage(p, &changed,
p.ConstraintSimplification,
"Do Fixpoint of TransformConstraintsToInstantiation.",
"ConstraintSimplification.",
"For each constraint (X==v) rewrite clause with (X<-v) and remove constraint.")

stage(p, &changed,
Expand All @@ -111,7 +115,7 @@ func stage1GeneratorsAndFacts(p *bule.Program) {
stage(p, &changed,
p.RemoveRulesWithGenerators,
"RemoveRulesWithGeneratorsBecauseTheyHaveEmptyDomains",
"Because they have empty domains, e.g. \n edge[_,_,V] => vertex[V]. %% there are no edges!")
"Because they have empty domains, e.g. \n edge[_,_,V] :: vertex[V]. %% there are no edges!")
}

// Unroll all iterators.
Expand Down Expand Up @@ -240,6 +244,7 @@ func stage4Printing(p *bule.Program, args []string) {
}

func stage(p *bule.Program, change *bool, f func() (bool, error), stage string, info string) {
start := time.Now()
stageInfo(p, stage, info)
tmp, err := f()
if err != nil {
Expand All @@ -251,6 +256,9 @@ func stage(p *bule.Program, change *bool, f func() (bool, error), stage string,
debug(2, "Stage changed Program!")
}
*change = *change || tmp

elapsed := time.Since(start)
log.Printf("DEBUGTIME %6.2f %v %v", elapsed.Seconds(), stage, len(p.Rules))
}

func runFixpoint(f func() (bool, error)) func() (bool, error) {
Expand Down
29 changes: 29 additions & 0 deletions grounder/ground.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func (p *Program) ConstraintSimplification() (bool, error) {

finalChanged := true
//start := time.Now()

i := 0
for {
Expand All @@ -22,6 +23,9 @@ func (p *Program) ConstraintSimplification() (bool, error) {
if err != nil {
return true, fmt.Errorf("Constraint simplification, iteration %v. \n %w", i, err)
}
//elapsed := time.Since(start)
//start = time.Now()
//log.Printf("X-DEBUGTIME %6.2f %v %v Constraints->Instantiation", elapsed.Seconds(), i, len(p.Rules))
if !changed {
//Debug(2, "Remove clauses with contradictions, e.g. (1==2) or (1!=1), and remove true constraints, e.g. (1>2, 1==1).")
finalChanged, err = p.CleanRulesFromGroundBoolExpression()
Expand All @@ -30,6 +34,9 @@ func (p *Program) ConstraintSimplification() (bool, error) {
}
break
}
//elapsed = time.Since(start)
//start = time.Now()
//log.Printf("X-DEBUGTIME %6.2f %v %v CleanRules", elapsed.Seconds(), i, len(p.Rules))
}
return finalChanged || i > 1, nil
}
Expand Down Expand Up @@ -153,6 +160,7 @@ func (p *Program) findFilteredTuples(literal Literal) [][]string {
filteredTuples = append(filteredTuples, tuple)
}
}
// fmt.Println("DEBUG", literal, filteredTuples)
return filteredTuples
}

Expand Down Expand Up @@ -421,6 +429,11 @@ func (p *Program) CleanRulesFromGroundBoolExpression() (bool, error) {
return true
}
}
for _, lit := range r.Generators {
if p.FinishCollectingFacts[lit.Name] && lit.IsGround() {
return true
}
}
return false
}

Expand All @@ -437,6 +450,22 @@ func (p *Program) CleanRulesFromGroundBoolExpression() (bool, error) {
newRule.Constraints = append(newRule.Constraints, cons)
}
}
newRule.Generators = []Literal{}
for _, lit := range rule.Generators {
if p.FinishCollectingFacts[lit.Name] && lit.IsGround() {
// fmt.Println("DEBUG", lit.IdString(), "\n", p.FinishCollectingFacts, "\n", p.PredicateTupleMap)
if lit.Neg && p.PredicateTupleMap[lit.createNegatedLiteral().IdString()] {
// fmt.Println("DEBUG REMOVE", rule.String())
return []Rule{}, nil
} else if !lit.Neg && p.PredicateTupleMap[lit.IdString()] {
// fmt.Println("DEBUG DO NOT REMOVE", rule.String())
} else {
newRule.Generators = append(newRule.Generators, lit)
}
} else {
newRule.Generators = append(newRule.Generators, lit)
}
}
return []Rule{newRule}, nil
}
return p.RuleExpansion(check, transform)
Expand Down
25 changes: 1 addition & 24 deletions grounder/transformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grounder

import (
"fmt"

"github.com/scylladb/go-set/strset"
)

Expand Down Expand Up @@ -143,30 +144,6 @@ func TermTranslation(termIterator TermIterator, transform func(Term) (Term, bool
return
}

//func (iterator *Iterator) TermTranslation(transform func(Term) (Term, bool, error)) (changed bool, err error) {
// var ok bool
// for _, term := range iterator.AllTerms() {
// *term, ok, err = transform(*term)
// changed = ok || changed
// if err != nil {
// return changed, err
// }
// }
// return
//}
//
//func (rule *Rule) TermTranslation(transform func(Term) (Term, bool, error)) (changed bool, err error) {
// var ok bool
// for _, term := range rule.AllTerms() {
// *term, ok, err = transform(*term)
// changed = ok || changed
// if err != nil {
// return changed, err
// }
// }
// return
//}

func (iterator Iterator) AllTerms() (terms []*Term) {

for i := range iterator.Head.Terms {
Expand Down

0 comments on commit 31de03f

Please sign in to comment.