Reverse order of args in ValueOut, TupleOut, and AddValueIn

The order of these methods now matches the order of edge/value in every
other context.
This commit is contained in:
Brian Picciano 2021-12-30 11:38:36 -07:00
parent 9c48232ac1
commit 2a96e9a593
6 changed files with 106 additions and 110 deletions

View File

@ -124,7 +124,7 @@ func (d *decoder) parseOpenEdge(
} }
if termed { if termed {
return graph.ValueOut[Value](val, ZeroValue), toks, nil return graph.ValueOut[Value](ZeroValue, val), toks, nil
} }
opTok, toks := toks[0], toks[1:] opTok, toks := toks[0], toks[1:]
@ -143,7 +143,7 @@ func (d *decoder) parseOpenEdge(
return nil, nil, err return nil, nil, err
} }
oe = graph.TupleOut[Value]([]*OpenEdge{oe}, val) oe = graph.TupleOut[Value](val, oe)
return oe, toks, nil return oe, toks, nil
} }
@ -189,7 +189,7 @@ func (d *decoder) parseTuple(
toks = toks[1:] toks = toks[1:]
} }
return graph.TupleOut[Value](edges, ZeroValue), toks, nil return graph.TupleOut[Value](ZeroValue, edges...), toks, nil
} }
// returned boolean value indicates if the token following the graph is a term. // returned boolean value indicates if the token following the graph is a term.
@ -288,7 +288,7 @@ func (d *decoder) parseValIn(into *Graph, toks []LexerToken) (*Graph, []LexerTok
dstVal := Value{Name: &dst.Value, LexerToken: &dst} dstVal := Value{Name: &dst.Value, LexerToken: &dst}
return into.AddValueIn(oe, dstVal), toks, nil return into.AddValueIn(dstVal, oe), toks, nil
} }
func (d *decoder) decode(lexer Lexer) (*Graph, error) { func (d *decoder) decode(lexer Lexer) (*Graph, error) {

View File

@ -21,12 +21,12 @@ func TestDecoder(t *testing.T) {
return Value{Name: &n} return Value{Name: &n}
} }
vOut := func(val, edgeVal Value) *OpenEdge { vOut := func(edgeVal, val Value) *OpenEdge {
return graph.ValueOut(val, edgeVal) return graph.ValueOut(edgeVal, val)
} }
tOut := func(ins []*OpenEdge, edgeVal Value) *OpenEdge { tOut := func(edgeVal Value, ins ...*OpenEdge) *OpenEdge {
return graph.TupleOut(ins, edgeVal) return graph.TupleOut(edgeVal, ins...)
} }
tests := []struct { tests := []struct {
@ -39,107 +39,98 @@ func TestDecoder(t *testing.T) {
}, },
{ {
in: "out = 1;", in: "out = 1;",
exp: zeroGraph.AddValueIn(vOut(i(1), ZeroValue), n("out")), exp: zeroGraph.AddValueIn(n("out"), vOut(ZeroValue, i(1))),
}, },
{ {
in: "out = incr < 1;", in: "out = incr < 1;",
exp: zeroGraph.AddValueIn(vOut(i(1), n("incr")), n("out")), exp: zeroGraph.AddValueIn(n("out"), vOut(n("incr"), i(1))),
}, },
{ {
in: "out = a < b < 1;", in: "out = a < b < 1;",
exp: zeroGraph.AddValueIn( exp: zeroGraph.AddValueIn(
tOut(
[]*OpenEdge{vOut(i(1), n("b"))},
n("a"),
),
n("out"), n("out"),
tOut(
n("a"),
vOut(n("b"),
i(1)),
),
), ),
}, },
{ {
in: "out = a < b < (1; c < 2; d < e < 3;);", in: "out = a < b < (1; c < 2; d < e < 3;);",
exp: zeroGraph.AddValueIn( exp: zeroGraph.AddValueIn(
tOut(
[]*OpenEdge{tOut(
[]*OpenEdge{
vOut(i(1), ZeroValue),
vOut(i(2), n("c")),
tOut(
[]*OpenEdge{vOut(i(3), n("e"))},
n("d"),
),
},
n("b"),
)},
n("a"),
),
n("out"), n("out"),
tOut(
n("a"),
tOut(
n("b"),
vOut(ZeroValue, i(1)),
vOut(n("c"), i(2)),
tOut(
n("d"),
vOut(n("e"), i(3)),
),
),
),
), ),
}, },
{ {
in: "out = a < b < (1; c < (d < 2; 3;); );", in: "out = a < b < (1; c < (d < 2; 3;); );",
exp: zeroGraph.AddValueIn( exp: zeroGraph.AddValueIn(
tOut(
[]*OpenEdge{tOut(
[]*OpenEdge{
vOut(i(1), ZeroValue),
tOut(
[]*OpenEdge{
vOut(i(2), n("d")),
vOut(i(3), ZeroValue),
},
n("c"),
),
},
n("b"),
)},
n("a"),
),
n("out"), n("out"),
tOut(
n("a"),
tOut(
n("b"),
vOut(ZeroValue, i(1)),
tOut(
n("c"),
vOut(n("d"), i(2)),
vOut(ZeroValue, i(3)),
),
),
),
), ),
}, },
{ {
in: "out = { a = 1; b = c < d < 2; };", in: "out = { a = 1; b = c < d < 2; };",
exp: zeroGraph.AddValueIn( exp: zeroGraph.AddValueIn(
vOut(
Value{Graph: zeroGraph.
AddValueIn(vOut(i(1), ZeroValue), n("a")).
AddValueIn(
tOut(
[]*OpenEdge{
vOut(i(2), n("d")),
},
n("c"),
),
n("b"),
),
},
ZeroValue,
),
n("out"), n("out"),
vOut(
ZeroValue,
Value{Graph: zeroGraph.
AddValueIn(n("a"), vOut(ZeroValue, i(1))).
AddValueIn(
n("b"),
tOut(
n("c"),
vOut(n("d"), i(2)),
),
),
},
),
), ),
}, },
{ {
in: "out = a < { b = 1; } < 2;", in: "out = a < { b = 1; } < 2;",
exp: zeroGraph.AddValueIn( exp: zeroGraph.AddValueIn(
tOut(
[]*OpenEdge{
vOut(
i(2),
Value{Graph: zeroGraph.
AddValueIn(vOut(i(1), ZeroValue), n("b")),
},
),
},
n("a"),
),
n("out"), n("out"),
tOut(
n("a"),
vOut(
Value{Graph: zeroGraph.
AddValueIn(n("b"), vOut(ZeroValue, i(1))),
},
i(2),
),
),
), ),
}, },
{ {
in: "a = 1; b = 2;", in: "a = 1; b = 2;",
exp: zeroGraph. exp: zeroGraph.
AddValueIn(vOut(i(1), ZeroValue), n("a")). AddValueIn(n("a"), vOut(ZeroValue, i(1))).
AddValueIn(vOut(i(2), ZeroValue), n("b")), AddValueIn(n("b"), vOut(ZeroValue, i(2))),
}, },
} }

View File

@ -111,7 +111,7 @@ func (oe OpenEdge[E, V]) FromTuple() ([]*OpenEdge[E, V], bool) {
// ValueOut creates a OpenEdge which, when used to construct a Graph, represents // ValueOut creates a OpenEdge which, when used to construct a Graph, represents
// an edge (with edgeVal attached to it) coming from the vertex containing val. // an edge (with edgeVal attached to it) coming from the vertex containing val.
func ValueOut[E, V Value](val V, edgeVal E) *OpenEdge[E, V] { func ValueOut[E, V Value](edgeVal E, val V) *OpenEdge[E, V] {
return &OpenEdge[E, V]{ return &OpenEdge[E, V]{
val: &val, val: &val,
edgeVal: edgeVal, edgeVal: edgeVal,
@ -121,7 +121,7 @@ func ValueOut[E, V Value](val V, edgeVal E) *OpenEdge[E, V] {
// TupleOut creates an OpenEdge which, when used to construct a Graph, // TupleOut creates an OpenEdge which, when used to construct a Graph,
// represents an edge (with edgeVal attached to it) coming from the vertex // represents an edge (with edgeVal attached to it) coming from the vertex
// comprised of the given ordered-set of input edges. // comprised of the given ordered-set of input edges.
func TupleOut[E, V Value](ins []*OpenEdge[E, V], edgeVal E) *OpenEdge[E, V] { func TupleOut[E, V Value](edgeVal E, ins ...*OpenEdge[E, V]) *OpenEdge[E, V] {
if len(ins) == 1 { if len(ins) == 1 {
@ -218,7 +218,7 @@ func (g *Graph[E, V]) dedupeEdge(edge *OpenEdge[E, V]) *OpenEdge[E, V] {
tupEdges[i] = g.dedupeEdge(edge.tup[i]) tupEdges[i] = g.dedupeEdge(edge.tup[i])
} }
return TupleOut(tupEdges, edge.EdgeValue()) return TupleOut(edge.EdgeValue(), tupEdges...)
} }
// ValueIns returns, if any, all OpenEdges which lead to the given Value in the // ValueIns returns, if any, all OpenEdges which lead to the given Value in the
@ -240,7 +240,7 @@ func (g *Graph[E, V]) ValueIns(val Value) []*OpenEdge[E, V] {
// AddValueIn takes a OpenEdge and connects it to the Value vertex containing // AddValueIn takes a OpenEdge and connects it to the Value vertex containing
// val, returning the new Graph which reflects that connection. // val, returning the new Graph which reflects that connection.
func (g *Graph[E, V]) AddValueIn(oe *OpenEdge[E, V], val V) *Graph[E, V] { func (g *Graph[E, V]) AddValueIn(val V, oe *OpenEdge[E, V]) *Graph[E, V] {
valIn := graphValueIn[E, V]{ valIn := graphValueIn[E, V]{
val: val, val: val,

View File

@ -31,78 +31,83 @@ func TestEqual(t *testing.T) {
}, },
{ {
a: zeroGraph, a: zeroGraph,
b: zeroGraph.AddValueIn(ValueOut[S, S]("in", "incr"), "out"), b: zeroGraph.AddValueIn("out", ValueOut[S, S]("incr", "in")),
exp: false, exp: false,
}, },
{ {
a: zeroGraph.AddValueIn(ValueOut[S, S]("in", "incr"), "out"), a: zeroGraph.AddValueIn("out", ValueOut[S, S]("incr", "in")),
b: zeroGraph.AddValueIn(ValueOut[S, S]("in", "incr"), "out"), b: zeroGraph.AddValueIn("out", ValueOut[S, S]("incr", "in")),
exp: true, exp: true,
}, },
{ {
a: zeroGraph.AddValueIn(ValueOut[S, S]("in", "incr"), "out"), a: zeroGraph.AddValueIn("out", ValueOut[S, S]("incr", "in")),
b: zeroGraph.AddValueIn(TupleOut[S, S]([]*OpenEdge[S, S]{ b: zeroGraph.AddValueIn("out", TupleOut[S, S](
ValueOut[S, S]("in", "ident"), "add",
ValueOut[S, S]("1", "ident"), ValueOut[S, S]("ident", "in"),
}, "add"), "out"), ValueOut[S, S]("ident", "1"),
)),
exp: false, exp: false,
}, },
{ {
// tuples are different order // tuples are different order
a: zeroGraph.AddValueIn(TupleOut[S, S]([]*OpenEdge[S, S]{ a: zeroGraph.AddValueIn("out", TupleOut[S, S](
ValueOut[S, S]("1", "ident"), "add",
ValueOut[S, S]("in", "ident"), ValueOut[S, S]("ident", "1"),
}, "add"), "out"), ValueOut[S, S]("ident", "in"),
b: zeroGraph.AddValueIn(TupleOut[S, S]([]*OpenEdge[S, S]{ )),
ValueOut[S, S]("in", "ident"), b: zeroGraph.AddValueIn("out", TupleOut[S, S](
ValueOut[S, S]("1", "ident"), "add",
}, "add"), "out"), ValueOut[S, S]("ident", "in"),
ValueOut[S, S]("ident", "1"),
)),
exp: false, exp: false,
}, },
{ {
// tuple with no edge value and just a single input edge should be // tuple with no edge value and just a single input edge should be
// equivalent to just that edge. // equivalent to just that edge.
a: zeroGraph.AddValueIn(TupleOut[S, S]([]*OpenEdge[S, S]{ a: zeroGraph.AddValueIn("out", TupleOut[S, S](
ValueOut[S, S]("1", "ident"), zeroValue,
}, zeroValue), "out"), ValueOut[S, S]("ident", "1"),
b: zeroGraph.AddValueIn(ValueOut[S, S]("1", "ident"), "out"), )),
b: zeroGraph.AddValueIn("out", ValueOut[S, S]("ident", "1")),
exp: true, exp: true,
}, },
{ {
// tuple with an edge value and just a single input edge that has no // tuple with an edge value and just a single input edge that has no
// edgeVal should be equivalent to just that edge with the tuple's // edgeVal should be equivalent to just that edge with the tuple's
// edge value. // edge value.
a: zeroGraph.AddValueIn(TupleOut[S, S]([]*OpenEdge[S, S]{ a: zeroGraph.AddValueIn("out", TupleOut[S, S](
ValueOut[S, S]("1", zeroValue), "ident",
}, "ident"), "out"), ValueOut[S, S](zeroValue, "1"),
b: zeroGraph.AddValueIn(ValueOut[S, S]("1", "ident"), "out"), )),
b: zeroGraph.AddValueIn("out", ValueOut[S, S]("ident", "1")),
exp: true, exp: true,
}, },
{ {
a: zeroGraph. a: zeroGraph.
AddValueIn(ValueOut[S, S]("in", "incr"), "out"). AddValueIn("out", ValueOut[S, S]("incr", "in")).
AddValueIn(ValueOut[S, S]("in2", "incr2"), "out2"), AddValueIn("out2", ValueOut[S, S]("incr2", "in2")),
b: zeroGraph. b: zeroGraph.
AddValueIn(ValueOut[S, S]("in", "incr"), "out"), AddValueIn("out", ValueOut[S, S]("incr", "in")),
exp: false, exp: false,
}, },
{ {
a: zeroGraph. a: zeroGraph.
AddValueIn(ValueOut[S, S]("in", "incr"), "out"). AddValueIn("out", ValueOut[S, S]("incr", "in")).
AddValueIn(ValueOut[S, S]("in2", "incr2"), "out2"), AddValueIn("out2", ValueOut[S, S]("incr2", "in2")),
b: zeroGraph. b: zeroGraph.
AddValueIn(ValueOut[S, S]("in", "incr"), "out"). AddValueIn("out", ValueOut[S, S]("incr", "in")).
AddValueIn(ValueOut[S, S]("in2", "incr2"), "out2"), AddValueIn("out2", ValueOut[S, S]("incr2", "in2")),
exp: true, exp: true,
}, },
{ {
// order of value ins shouldn't matter // order of value ins shouldn't matter
a: zeroGraph. a: zeroGraph.
AddValueIn(ValueOut[S, S]("in", "incr"), "out"). AddValueIn("out", ValueOut[S, S]("incr", "in")).
AddValueIn(ValueOut[S, S]("in2", "incr2"), "out2"), AddValueIn("out2", ValueOut[S, S]("incr2", "in2")),
b: zeroGraph. b: zeroGraph.
AddValueIn(ValueOut[S, S]("in2", "incr2"), "out2"). AddValueIn("out2", ValueOut[S, S]("incr2", "in2")).
AddValueIn(ValueOut[S, S]("in", "incr"), "out"), AddValueIn("out", ValueOut[S, S]("incr", "in")),
exp: true, exp: true,
}, },
} }

View File

@ -52,14 +52,14 @@ func preEvalEdgeOp(fn func(*gg.OpenEdge) (Value, error)) Operation {
tupEdges := make([]*gg.OpenEdge, len(val.Tuple)) tupEdges := make([]*gg.OpenEdge, len(val.Tuple))
for i := range val.Tuple { for i := range val.Tuple {
tupEdges[i] = graph.ValueOut[gg.Value](val.Tuple[i].Value, gg.ZeroValue) tupEdges[i] = graph.ValueOut[gg.Value](gg.ZeroValue, val.Tuple[i].Value)
} }
edge = graph.TupleOut[gg.Value](tupEdges, gg.ZeroValue) edge = graph.TupleOut[gg.Value](gg.ZeroValue, tupEdges...)
} else { } else {
edge = graph.ValueOut[gg.Value](val.Value, gg.ZeroValue) edge = graph.ValueOut[gg.Value](gg.ZeroValue, val.Value)
} }
@ -92,7 +92,7 @@ func (g *graphOp) Perform(edge *gg.OpenEdge, scope Scope) (Value, error) {
return preEvalEdgeOp(func(edge *gg.OpenEdge) (Value, error) { return preEvalEdgeOp(func(edge *gg.OpenEdge) (Value, error) {
scope = ScopeFromGraph( scope = ScopeFromGraph(
g.Graph.AddValueIn(edge, inVal.Value), g.Graph.AddValueIn(inVal.Value, edge),
g.scope, g.scope,
) )

View File

@ -115,5 +115,5 @@ func EvaluateSource(opSrc io.Reader, input gg.Value, scope Scope) (Value, error)
op := OperationFromGraph(g, scope.NewScope()) op := OperationFromGraph(g, scope.NewScope())
return op.Perform(graph.ValueOut[gg.Value](input, gg.ZeroValue), scope) return op.Perform(graph.ValueOut[gg.Value](gg.ZeroValue, input), scope)
} }