intent.ex (4546B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule Zenflows.VF.Intent do 19 @moduledoc """ 20 A planned economic flow which has not been committed to, which can lead 21 to economic events (sometimes through commitments). 22 """ 23 use Zenflows.DB.Schema 24 25 alias Ecto.Changeset 26 alias Zenflows.DB.{Schema, Validate} 27 alias Zenflows.File 28 alias Zenflows.VF.{ 29 Action, 30 Agent, 31 EconomicResource, 32 Measure, 33 Process, 34 ProposedIntent, 35 ResourceSpecification, 36 Satisfaction, 37 SpatialThing, 38 Unit, 39 } 40 41 @type t() :: %__MODULE__{ 42 name: String.t() | nil, 43 action: Action.t(), 44 provider: Agent.t() | nil, 45 receiver: Agent.t() | nil, 46 input_of: Process.t() | nil, 47 output_of: Process.t() | nil, 48 resource_classified_as: [String.t()] | nil, 49 resource_conforms_to: ResourceSpecification.t() | nil, 50 resource_inventoried_as: EconomicResource.t() | nil, 51 resource_quantity: Measure.t() | nil, 52 effort_quantity: Measure.t() | nil, 53 available_quantity: Measure.t() | nil, 54 at_location: SpatialThing.t() | nil, 55 has_beginning: DateTime.t() | nil, 56 has_end: DateTime.t() | nil, 57 has_point_in_time: DateTime.t() | nil, 58 due: DateTime.t() | nil, 59 finished: boolean(), 60 images: [File.t()], 61 note: String.t() | nil, 62 # in_scope_of: 63 agreed_in: String.t() | nil, 64 65 published_in: [ProposedIntent.t()], 66 } 67 68 schema "vf_intent" do 69 field :name 70 field :action_id, Action.ID 71 field :action, :map, virtual: true 72 belongs_to :provider, Agent 73 belongs_to :receiver, Agent 74 belongs_to :input_of, Process 75 belongs_to :output_of, Process 76 field :resource_classified_as, {:array, :string} 77 belongs_to :resource_conforms_to, ResourceSpecification 78 belongs_to :resource_inventoried_as, EconomicResource 79 field :resource_quantity, :map, virtual: true 80 belongs_to :resource_quantity_has_unit, Unit 81 field :resource_quantity_has_numerical_value, :decimal 82 field :effort_quantity, :map, virtual: true 83 belongs_to :effort_quantity_has_unit, Unit 84 field :effort_quantity_has_numerical_value, :decimal 85 field :available_quantity, :map, virtual: true 86 belongs_to :available_quantity_has_unit, Unit 87 field :available_quantity_has_numerical_value, :decimal 88 belongs_to :at_location, SpatialThing 89 field :has_beginning, :utc_datetime_usec 90 field :has_end, :utc_datetime_usec 91 field :has_point_in_time, :utc_datetime_usec 92 field :due, :utc_datetime_usec 93 field :finished, :boolean, default: false 94 has_many :images, File 95 field :note, :string 96 # field :in_scope_of 97 field :agreed_in, :string 98 timestamps() 99 100 has_many :satisfied_by, Satisfaction, foreign_key: :satisfies_id 101 has_many :published_in, ProposedIntent, foreign_key: :publishes_id 102 end 103 104 @reqr [:action_id] 105 @cast @reqr ++ ~w[ 106 name provider_id receiver_id input_of_id output_of_id 107 resource_classified_as resource_conforms_to_id resource_inventoried_as_id 108 resource_quantity effort_quantity available_quantity 109 at_location_id has_beginning has_end has_point_in_time due 110 finished note agreed_in 111 ]a # in_scope_of_id 112 113 @doc false 114 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 115 def changeset(schema \\ %__MODULE__{}, params) do 116 schema 117 |> Changeset.cast(params, @cast) 118 |> Changeset.validate_required(@reqr) 119 |> Validate.exist_xor([:provider_id, :receiver_id], method: :both) 120 |> Validate.name(:name) 121 |> Validate.note(:note) 122 |> Changeset.cast_assoc(:images) 123 |> Validate.class(:resource_classified_as) 124 |> Measure.cast(:resource_quantity) 125 |> Measure.cast(:effort_quantity) 126 |> Measure.cast(:available_quantity) 127 |> Changeset.assoc_constraint(:provider) 128 |> Changeset.assoc_constraint(:receiver) 129 |> Changeset.assoc_constraint(:input_of) 130 |> Changeset.assoc_constraint(:output_of) 131 |> Changeset.assoc_constraint(:resource_conforms_to) 132 |> Changeset.assoc_constraint(:resource_inventoried_as) 133 |> Changeset.assoc_constraint(:at_location) 134 end 135 end