Skip to content

Commit bcd4431

Browse files
Merge pull request #143 from diffo-dev/141-refactor-assigner-using-definedsimplerelationship
refactored assigner using defined_simple_relationship
2 parents 651a14b + 4592844 commit bcd4431

8 files changed

Lines changed: 42 additions & 149 deletions

File tree

AGENTS.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ lib/diffo/provider/
4747
assigner/
4848
assigner.ex # Diffo.Provider.Assigner — assign/3 (pools do) and assign/4
4949
assignable_characteristic.ex # AssignableCharacteristic — pool bounds + algorithm
50-
assigned_to_relationship.ex # AssignedToRelationship — assignedTo edges (pool/thing/assigned)
5150
base_instance.ex # Ash Fragment for Instance resources
5251
base_party.ex # Ash Fragment for Party resources
5352
base_place.ex # Ash Fragment for Place resources

lib/diffo/provider.ex

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ defmodule Diffo.Provider do
8383
define :delete_defined_simple_relationship, action: :destroy
8484
end
8585

86-
resource Diffo.Provider.AssignedToRelationship do
87-
define :create_assigned_to_relationship, action: :create_assignment
88-
define :get_assigned_to_relationship_by_id, action: :read, get_by: :id
89-
define :delete_assigned_to_relationship, action: :destroy
90-
end
91-
9286
resource Diffo.Provider.AssignableCharacteristic do
9387
define :create_assignable_characteristic, action: :create
9488
define :get_assignable_characteristic_by_id, action: :read, get_by: :id

lib/diffo/provider/assigner/assigned_to_relationship.ex

Lines changed: 0 additions & 103 deletions
This file was deleted.

lib/diffo/provider/assigner/assigner.ex

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
defmodule Diffo.Provider.Assigner do
66
@moduledoc """
7-
Helper to perform Assignment using `Diffo.Provider.AssignedToRelationship`.
7+
Helper to perform Assignment using `Diffo.Provider.DefinedSimpleRelationship`.
88
9-
Assignment state is stored on `AssignedToRelationship` nodes (pool, thing, assigned),
10-
distinct from regular TMF `Diffo.Provider.Relationship` nodes.
9+
Each assignment is stored as a `DefinedSimpleRelationship` with `type: :assignedTo`
10+
and a single `NameValuePrimitive` characteristic carrying the thing name and assigned value.
1111
"""
1212
alias Diffo.Provider.AssignableCharacteristic
13-
alias Diffo.Provider.AssignedToRelationship
13+
alias Diffo.Provider.DefinedSimpleRelationship
14+
alias Diffo.Type.NameValuePrimitive
15+
alias Diffo.Type.Primitive
1416

1517
@doc """
1618
Assign a thing using the pool declared via `pools do` on the instance module.
@@ -63,13 +65,15 @@ defmodule Diffo.Provider.Assigner do
6365
end
6466
end
6567

66-
defp relate_is_assigned(result, pool, thing, value, assignee_id)
67-
when is_struct(result) and is_atom(pool) and is_atom(thing) and is_integer(value) and
68+
defp relate_is_assigned(result, _pool, thing, value, assignee_id)
69+
when is_struct(result) and is_atom(thing) and is_integer(value) and
6870
is_bitstring(assignee_id) do
69-
case Diffo.Provider.create_assigned_to_relationship(%{
70-
pool: pool,
71-
thing: thing,
72-
assigned: value,
71+
case Diffo.Provider.create_defined_simple_relationship(%{
72+
type: :assignedTo,
73+
characteristic: %NameValuePrimitive{
74+
name: thing,
75+
value: Primitive.wrap("integer", value)
76+
},
7377
source_id: result.id,
7478
target_id: assignee_id
7579
}) do
@@ -102,17 +106,22 @@ defmodule Diffo.Provider.Assigner do
102106
end
103107
end
104108

105-
defp find_assignment(source_id, target_id, pool, thing, value) do
106-
AssignedToRelationship
107-
|> Ash.Query.new()
108-
|> Ash.Query.filter_input(
109-
source_id: source_id,
110-
target_id: target_id,
111-
pool: pool,
112-
thing: thing,
113-
assigned: value
114-
)
115-
|> Ash.read_one(domain: Diffo.Provider)
109+
defp find_assignment(source_id, target_id, _pool, thing, value) do
110+
case DefinedSimpleRelationship
111+
|> Ash.Query.new()
112+
|> Ash.Query.filter_input(source_id: source_id, target_id: target_id, type: :assignedTo)
113+
|> Ash.read(domain: Diffo.Provider) do
114+
{:ok, rels} ->
115+
{:ok,
116+
Enum.find(rels, fn rel ->
117+
rel.characteristic &&
118+
rel.characteristic.name == thing &&
119+
Diffo.Unwrap.unwrap(rel.characteristic.value) == value
120+
end)}
121+
122+
{:error, error} ->
123+
{:error, error}
124+
end
116125
end
117126

118127
defp next(instance, pool, thing)

lib/diffo/provider/components/base_instance.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ defmodule Diffo.Provider.BaseInstance do
188188
{:process_statuses, :STATUSES, :incoming, :ProcessStatus},
189189
{:forward_relationships, :RELATES, :outgoing, :Relationship},
190190
{:reverse_relationships, :RELATES, :incoming, :Relationship},
191-
{:assignments, :RELATES, :outgoing, :AssignedToRelationship},
191+
{:assignments, :RELATES, :outgoing, :DefinedSimpleRelationship},
192192
{:features, :HAS, :outgoing, :Feature},
193193
{:characteristics, :HAS, :outgoing, :Characteristic},
194194
{:entities, :RELATES, :outgoing, :EntityRef},
@@ -409,7 +409,7 @@ defmodule Diffo.Provider.BaseInstance do
409409
public? true
410410
end
411411

412-
has_many :assignments, Diffo.Provider.AssignedToRelationship do
412+
has_many :assignments, Diffo.Provider.DefinedSimpleRelationship do
413413
description "the instance's outgoing pool assignment relationships"
414414
destination_attribute :source_id
415415
public? true

lib/diffo/provider/components/calculations/assigned_values.ex

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ defmodule Diffo.Provider.Calculations.AssignedValues do
1414
thing = context.arguments[:thing]
1515

1616
Enum.map(records, fn record ->
17-
Diffo.Provider.AssignedToRelationship
18-
|> Ash.Query.new()
19-
|> Ash.Query.filter_input(
20-
source_id: record.instance_id,
21-
pool: record.name,
22-
thing: thing
23-
)
17+
Diffo.Provider.DefinedSimpleRelationship
18+
|> Ash.Query.filter_input(source_id: record.instance_id, type: :assignedTo)
2419
|> Ash.read!(domain: Diffo.Provider)
25-
|> Enum.map(& &1.assigned)
20+
|> Enum.filter(fn rel -> rel.characteristic && rel.characteristic.name == thing end)
21+
|> Enum.map(fn rel -> Diffo.Unwrap.unwrap(rel.characteristic.value) end)
2622
end)
2723
end
2824
end

lib/diffo/provider/components/calculations/free_values.ex

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ defmodule Diffo.Provider.Calculations.FreeValues do
1717

1818
record ->
1919
count =
20-
Diffo.Provider.AssignedToRelationship
21-
|> Ash.Query.filter_input(
22-
source_id: record.instance_id,
23-
pool: record.name,
24-
thing: record.thing
25-
)
20+
Diffo.Provider.DefinedSimpleRelationship
21+
|> Ash.Query.filter_input(source_id: record.instance_id, type: :assignedTo)
2622
|> Ash.read!(domain: Diffo.Provider)
27-
|> length()
23+
|> Enum.count(fn rel ->
24+
rel.characteristic && rel.characteristic.name == record.thing
25+
end)
2826

2927
record.last - record.first + 1 - count
3028
end)

test/provider/extension/assigner_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ defmodule Diffo.Provider.Extension.AssignerTest do
190190

191191
assert length(card.assignments) == 1
192192

193-
assigned_port = hd(card.assignments).assigned
193+
assigned_port = Diffo.Unwrap.unwrap(hd(card.assignments).characteristic.value)
194194

195195
{:ok, card} =
196196
Servo.assign_port(card, %{

0 commit comments

Comments
 (0)