Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ lib/diffo/provider/
assigner/
assigner.ex # Diffo.Provider.Assigner — assign/3 (pools do) and assign/4
assignable_characteristic.ex # AssignableCharacteristic — pool bounds + algorithm
assigned_to_relationship.ex # AssignedToRelationship — assignedTo edges (pool/thing/assigned)
base_instance.ex # Ash Fragment for Instance resources
base_party.ex # Ash Fragment for Party resources
base_place.ex # Ash Fragment for Place resources
Expand Down
6 changes: 0 additions & 6 deletions lib/diffo/provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ defmodule Diffo.Provider do
define :delete_defined_simple_relationship, action: :destroy
end

resource Diffo.Provider.AssignedToRelationship do
define :create_assigned_to_relationship, action: :create_assignment
define :get_assigned_to_relationship_by_id, action: :read, get_by: :id
define :delete_assigned_to_relationship, action: :destroy
end

resource Diffo.Provider.AssignableCharacteristic do
define :create_assignable_characteristic, action: :create
define :get_assignable_characteristic_by_id, action: :read, get_by: :id
Expand Down
103 changes: 0 additions & 103 deletions lib/diffo/provider/assigner/assigned_to_relationship.ex

This file was deleted.

51 changes: 30 additions & 21 deletions lib/diffo/provider/assigner/assigner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

defmodule Diffo.Provider.Assigner do
@moduledoc """
Helper to perform Assignment using `Diffo.Provider.AssignedToRelationship`.
Helper to perform Assignment using `Diffo.Provider.DefinedSimpleRelationship`.

Assignment state is stored on `AssignedToRelationship` nodes (pool, thing, assigned),
distinct from regular TMF `Diffo.Provider.Relationship` nodes.
Each assignment is stored as a `DefinedSimpleRelationship` with `type: :assignedTo`
and a single `NameValuePrimitive` characteristic carrying the thing name and assigned value.
"""
alias Diffo.Provider.AssignableCharacteristic
alias Diffo.Provider.AssignedToRelationship
alias Diffo.Provider.DefinedSimpleRelationship
alias Diffo.Type.NameValuePrimitive
alias Diffo.Type.Primitive

@doc """
Assign a thing using the pool declared via `pools do` on the instance module.
Expand Down Expand Up @@ -63,13 +65,15 @@ defmodule Diffo.Provider.Assigner do
end
end

defp relate_is_assigned(result, pool, thing, value, assignee_id)
when is_struct(result) and is_atom(pool) and is_atom(thing) and is_integer(value) and
defp relate_is_assigned(result, _pool, thing, value, assignee_id)
when is_struct(result) and is_atom(thing) and is_integer(value) and
is_bitstring(assignee_id) do
case Diffo.Provider.create_assigned_to_relationship(%{
pool: pool,
thing: thing,
assigned: value,
case Diffo.Provider.create_defined_simple_relationship(%{
type: :assignedTo,
characteristic: %NameValuePrimitive{
name: thing,
value: Primitive.wrap("integer", value)
},
source_id: result.id,
target_id: assignee_id
}) do
Expand Down Expand Up @@ -102,17 +106,22 @@ defmodule Diffo.Provider.Assigner do
end
end

defp find_assignment(source_id, target_id, pool, thing, value) do
AssignedToRelationship
|> Ash.Query.new()
|> Ash.Query.filter_input(
source_id: source_id,
target_id: target_id,
pool: pool,
thing: thing,
assigned: value
)
|> Ash.read_one(domain: Diffo.Provider)
defp find_assignment(source_id, target_id, _pool, thing, value) do
case DefinedSimpleRelationship
|> Ash.Query.new()
|> Ash.Query.filter_input(source_id: source_id, target_id: target_id, type: :assignedTo)
|> Ash.read(domain: Diffo.Provider) do
{:ok, rels} ->
{:ok,
Enum.find(rels, fn rel ->
rel.characteristic &&
rel.characteristic.name == thing &&
Diffo.Unwrap.unwrap(rel.characteristic.value) == value
end)}

{:error, error} ->
{:error, error}
end
end

defp next(instance, pool, thing)
Expand Down
4 changes: 2 additions & 2 deletions lib/diffo/provider/components/base_instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ defmodule Diffo.Provider.BaseInstance do
{:process_statuses, :STATUSES, :incoming, :ProcessStatus},
{:forward_relationships, :RELATES, :outgoing, :Relationship},
{:reverse_relationships, :RELATES, :incoming, :Relationship},
{:assignments, :RELATES, :outgoing, :AssignedToRelationship},
{:assignments, :RELATES, :outgoing, :DefinedSimpleRelationship},
{:features, :HAS, :outgoing, :Feature},
{:characteristics, :HAS, :outgoing, :Characteristic},
{:entities, :RELATES, :outgoing, :EntityRef},
Expand Down Expand Up @@ -409,7 +409,7 @@ defmodule Diffo.Provider.BaseInstance do
public? true
end

has_many :assignments, Diffo.Provider.AssignedToRelationship do
has_many :assignments, Diffo.Provider.DefinedSimpleRelationship do
description "the instance's outgoing pool assignment relationships"
destination_attribute :source_id
public? true
Expand Down
12 changes: 4 additions & 8 deletions lib/diffo/provider/components/calculations/assigned_values.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ defmodule Diffo.Provider.Calculations.AssignedValues do
thing = context.arguments[:thing]

Enum.map(records, fn record ->
Diffo.Provider.AssignedToRelationship
|> Ash.Query.new()
|> Ash.Query.filter_input(
source_id: record.instance_id,
pool: record.name,
thing: thing
)
Diffo.Provider.DefinedSimpleRelationship
|> Ash.Query.filter_input(source_id: record.instance_id, type: :assignedTo)
|> Ash.read!(domain: Diffo.Provider)
|> Enum.map(& &1.assigned)
|> Enum.filter(fn rel -> rel.characteristic && rel.characteristic.name == thing end)
|> Enum.map(fn rel -> Diffo.Unwrap.unwrap(rel.characteristic.value) end)
end)
end
end
12 changes: 5 additions & 7 deletions lib/diffo/provider/components/calculations/free_values.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ defmodule Diffo.Provider.Calculations.FreeValues do

record ->
count =
Diffo.Provider.AssignedToRelationship
|> Ash.Query.filter_input(
source_id: record.instance_id,
pool: record.name,
thing: record.thing
)
Diffo.Provider.DefinedSimpleRelationship
|> Ash.Query.filter_input(source_id: record.instance_id, type: :assignedTo)
|> Ash.read!(domain: Diffo.Provider)
|> length()
|> Enum.count(fn rel ->
rel.characteristic && rel.characteristic.name == record.thing
end)

record.last - record.first + 1 - count
end)
Expand Down
2 changes: 1 addition & 1 deletion test/provider/extension/assigner_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ defmodule Diffo.Provider.Extension.AssignerTest do

assert length(card.assignments) == 1

assigned_port = hd(card.assignments).assigned
assigned_port = Diffo.Unwrap.unwrap(hd(card.assignments).characteristic.value)

{:ok, card} =
Servo.assign_port(card, %{
Expand Down
Loading