Skip to content

Commit

Permalink
internal/core: build graphs of Features
Browse files Browse the repository at this point in the history
We construct a graph where each node is an adt.Feature, and the edges
indicate the desired ordering of nodes in the output. The graph is
directed, and can model cycles. Cycles are caused by the unification of
two or more structs that share at least two fields, but in opposite
orders. E.g.

```
x: {
  a: _
  b: _
  c: _
}

y: {
  c: _
  a: _
}

z: x & y
```

From the graph, we can construct the Strongly Connected Components. Each
component contains one or more nodes (every node in the graph exists in
exactly one component). Nodes i and j are grouped into the same component
when there is a path from i to j and also a path from j to i. Thus a
component contains more than one node if-and-only-if there are cycles
involving every node in the component.

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: I5082c0273e883b69f854943994542067f35e8afc
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202377
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
cuematthew committed Nov 5, 2024
1 parent b3eed8b commit 5855903
Show file tree
Hide file tree
Showing 4 changed files with 590 additions and 0 deletions.
105 changes: 105 additions & 0 deletions internal/core/toposort/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2024 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package toposort

import (
"cuelang.org/go/internal/core/adt"
)

const (
NodeUnsorted = -1
)

type Graph struct {
nodes Nodes
}

type Node struct {
Feature adt.Feature
Outgoing Nodes
Incoming Nodes
// temporary state for calculating the Strongly Connected
// Components of a graph.
sccNodeState *sccNodeState
position int
}

func (n *Node) IsSorted() bool {
return n.position >= 0
}

type Nodes []*Node

func (nodes Nodes) Features() []adt.Feature {
features := make([]adt.Feature, len(nodes))
for i, node := range nodes {
features[i] = node.Feature
}
return features
}

type edge struct {
from adt.Feature
to adt.Feature
}

type GraphBuilder struct {
edgesSet map[edge]struct{}
nodesByFeature map[adt.Feature]*Node
}

func NewGraphBuilder() *GraphBuilder {
return &GraphBuilder{
edgesSet: make(map[edge]struct{}),
nodesByFeature: make(map[adt.Feature]*Node),
}
}

// Adds an edge between the two features. Nodes for the features will
// be created if they don't already exist. This method is idempotent:
// multiple calls with the same arguments will not create multiple
// edges, nor error.
func (builder *GraphBuilder) AddEdge(from, to adt.Feature) {
edge := edge{from: from, to: to}
if _, found := builder.edgesSet[edge]; found {
return
}

builder.edgesSet[edge] = struct{}{}
fromNode := builder.EnsureNode(from)
toNode := builder.EnsureNode(to)
fromNode.Outgoing = append(fromNode.Outgoing, toNode)
toNode.Incoming = append(toNode.Incoming, fromNode)
}

// Ensure that a node for this feature exists. This is necessary for
// features that are not necessarily connected to any other feature.
func (builder *GraphBuilder) EnsureNode(feature adt.Feature) *Node {
node, found := builder.nodesByFeature[feature]
if !found {
node = &Node{Feature: feature, position: NodeUnsorted}
builder.nodesByFeature[feature] = node
}
return node
}

func (builder *GraphBuilder) Build() *Graph {
nodesByFeature := builder.nodesByFeature
nodes := make(Nodes, 0, len(nodesByFeature))
for _, node := range nodesByFeature {
nodes = append(nodes, node)
}
return &Graph{nodes: nodes}
}
179 changes: 179 additions & 0 deletions internal/core/toposort/graph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright 2024 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package toposort_test

import (
"cmp"
"fmt"
"slices"
"testing"

"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/core/toposort"
)

func makeFeatures(index adt.StringIndexer, inputs [][]string) [][]adt.Feature {
result := make([][]adt.Feature, len(inputs))
for i, names := range inputs {
features := make([]adt.Feature, len(names))
for j, name := range names {
features[j] = adt.MakeStringLabel(index, name)
}
result[i] = features
}
return result
}

func compareStringses(a, b []string) int {
lim := min(len(a), len(b))
for i := 0; i < lim; i++ {
if comparison := cmp.Compare(a[i], b[i]); comparison != 0 {
return comparison
}
}
return cmp.Compare(len(a), len(b))
}

func allPermutations(featureses [][]adt.Feature) [][][]adt.Feature {
nonNilIdx := -1
var results [][][]adt.Feature
for i, features := range featureses {
if features == nil {
continue
}
nonNilIdx = i
featureses[i] = nil
for _, result := range allPermutations(featureses) {
results = append(results, append(result, features))
}
featureses[i] = features
}
if len(results) == 0 && nonNilIdx != -1 {
return [][][]adt.Feature{{featureses[nonNilIdx]}}
}
return results
}

func permutationNames(index adt.StringIndexer, permutation [][]adt.Feature) [][]string {
permNames := make([][]string, len(permutation))
for i, features := range permutation {
permNames[i] = featuresNames(index, features)
}
return permNames
}

func featuresNames(index adt.StringIndexer, features []adt.Feature) []string {
names := make([]string, len(features))
for i, feature := range features {
names[i] = feature.StringValue(index)
}
return names
}

func buildGraphFromPermutation(permutation [][]adt.Feature) *toposort.Graph {
builder := toposort.NewGraphBuilder()

for _, chain := range permutation {
if len(chain) == 0 {
continue
}

prev := chain[0]
builder.EnsureNode(prev)
for _, cur := range chain[1:] {
builder.AddEdge(prev, cur)
prev = cur
}
}
return builder.Build()
}

func testAllPermutations(t *testing.T, index adt.StringIndexer, inputs [][]string, fun func(*testing.T, [][]adt.Feature, *toposort.Graph)) {
features := makeFeatures(index, inputs)
for i, permutation := range allPermutations(features) {
t.Run(fmt.Sprint(i), func(t *testing.T) {
graph := buildGraphFromPermutation(permutation)
fun(t, permutation, graph)
})
}
}

func TestAllPermutations(t *testing.T) {
a, b, c, d := []string{"a"}, []string{"b"}, []string{"c"}, []string{"d"}

type PermutationTestCase struct {
name string
inputs [][]string
expected [][][]string
}

testCases := []PermutationTestCase{
{
name: "empty",
},
{
name: "one",
inputs: [][]string{a},
expected: [][][]string{{a}},
},
{
name: "two",
inputs: [][]string{a, b},
expected: [][][]string{{b, a}, {a, b}},
},
{
name: "three",
inputs: [][]string{a, b, c},
expected: [][][]string{
{c, b, a}, {b, c, a}, {c, a, b}, {a, c, b}, {b, a, c}, {a, b, c},
},
},
{
name: "four",
inputs: [][]string{a, b, c, d},
expected: [][][]string{
{d, c, b, a}, {c, d, b, a}, {d, b, c, a}, {b, d, c, a}, {c, b, d, a}, {b, c, d, a},
{d, c, a, b}, {c, d, a, b}, {d, a, c, b}, {a, d, c, b}, {c, a, d, b}, {a, c, d, b},
{d, b, a, c}, {b, d, a, c}, {d, a, b, c}, {a, d, b, c}, {b, a, d, c}, {a, b, d, c},
{c, b, a, d}, {b, c, a, d}, {c, a, b, d}, {a, c, b, d}, {b, a, c, d}, {a, b, c, d},
},
},
}

index := runtime.New()

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fs := makeFeatures(index, tc.inputs)
permutations := allPermutations(fs)
permutationsNames := make([][][]string, len(permutations))
for i, permutation := range permutations {
permutationsNames[i] = permutationNames(index, permutation)
}

if !slices.EqualFunc(permutationsNames, tc.expected,
func(gotPerm, expectedPerm [][]string) bool {
return slices.EqualFunc(gotPerm, expectedPerm, slices.Equal)
}) {
t.Fatalf(`
For inputs: %v
Expected: %v
Got: %v`,
tc.inputs, tc.expected, permutations)
}
})
}
}
Loading

0 comments on commit 5855903

Please sign in to comment.