MCPcopy
hub / github.com/google/mangle / TestTransformPartsExplosion

Function TestTransformPartsExplosion

engine/seminaivebottomup_test.go:602–647  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

600}
601
602func TestTransformPartsExplosion(t *testing.T) {
603 store := factstore.NewSimpleInMemoryStore()
604 program := []ast.Clause{
605 // The 'component(Subpart, Part, Quantity)' relation expresses that
606 // <Quantity> units of <SubPart> go directly into one unit of <Part>.
607 clause(`component(1, 5, 9).`),
608 clause(`component(2, 5, 7).`),
609 clause(`component(3, 5, 2).`),
610 clause(`component(2, 6, 12).`),
611 clause(`component(3, 6, 3).`),
612 clause(`component(4, 7, 1).`),
613 clause(`component(6, 7, 1).`),
614 // The `transitive(DescPart, Part, Quantity, Path)` relation
615 // expresses that <Quantity> units of <DescPart> go overall into one
616 // unit of <Part> along a dependency path <Path>
617 clause(`transitive(DescPart, Part, Quantity, []) :- component(DescPart, Part, Quantity).`),
618 clause(`transitive(DescPart, Part, Quantity, Path) :-
619 component(SubPart, Part, DirectQuantity),
620 transitive(DescPart, SubPart, DescQuantity, SubPath)
621 |> let Quantity = fn:mult(DirectQuantity, DescQuantity),
622 let Path = fn:list:cons(SubPart, SubPath).`),
623 // The `full(DescPart, Part, Quantity) relation expresses that <Quantity>
624 // units of <DescPart> go overall into one unit of <Part>.
625 clause(`full(DescPart, Part, Sum) :-
626 transitive(DescPart, Part, Quantity, Path)
627 |> do fn:group_by(DescPart, Part), let Sum = fn:sum(Quantity).`),
628 }
629 if err := analyzeAndEvalProgram(t, program, store); err != nil {
630 t.Errorf("Program evaluation failed %v program %v", err, program)
631 return
632 }
633 transitiveAtom, err := functional.EvalAtom(atom("transitive(2,7,12,[6])"), ast.ConstSubstList{})
634 if err != nil {
635 t.Fatal(err)
636 }
637 expected := []ast.Atom{
638 transitiveAtom,
639 atom("full(2,7,12)"),
640 }
641
642 for _, fact := range expected {
643 if !store.Contains(fact) {
644 t.Errorf("expected fact %v in store %v", fact, store)
645 }
646 }
647}
648
649func TestTransformFib(t *testing.T) {
650 store := factstore.NewSimpleInMemoryStore()

Callers

nothing calls this directly

Calls 6

ContainsMethod · 0.95
NewSimpleInMemoryStoreFunction · 0.92
EvalAtomFunction · 0.92
analyzeAndEvalProgramFunction · 0.85
clauseFunction · 0.70
atomFunction · 0.70

Tested by

no test coverage detected