zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

process.ex (2816B)


      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.Process do
     19 @moduledoc """
     20 An activity that changes inputs into outputs.  It could transform or
     21 transport economic resource(s).
     22 """
     23 
     24 use Zenflows.DB.Schema
     25 
     26 alias Ecto.Changeset
     27 alias Zenflows.DB.{Schema, Validate}
     28 alias Zenflows.VF.{
     29 	EconomicEvent,
     30 	Plan,
     31 	ProcessSpecification,
     32 	Scenario,
     33 }
     34 
     35 @type t() :: %__MODULE__{
     36 	name: String.t(),
     37 	note: String.t() | nil,
     38 	has_beginning: DateTime.t() | nil,
     39 	has_end: DateTime.t() | nil,
     40 	finished: boolean(),
     41 	deletable: boolean(),
     42 	classified_as: [String.t()],
     43 	based_on: ProcessSpecification.t() | nil,
     44 	planned_within: Plan.t() | nil,
     45 	nested_in: Scenario.t() | nil,
     46 }
     47 
     48 @derive {Jason.Encoder, only: ~w[
     49 	id
     50 	name note
     51 	has_beginning has_end
     52 	finished deletable
     53 	classified_as
     54 	based_on_id planned_within_id
     55 ]a}
     56 schema "vf_process" do
     57 	field :name, :string
     58 	field :note, :string
     59 	field :has_beginning, :utc_datetime_usec
     60 	field :has_end, :utc_datetime_usec
     61 	field :finished, :boolean, default: false
     62 	field :deletable, :boolean, default: false, virtual: true
     63 	field :classified_as, {:array, :string}
     64 	belongs_to :based_on, ProcessSpecification
     65 	# belongs_to :in_scope_of
     66 	belongs_to :planned_within, Plan
     67 	belongs_to :nested_in, Scenario
     68 	timestamps()
     69 
     70 	has_many :inputs, EconomicEvent, foreign_key: :input_of_id
     71 	has_many :outputs, EconomicEvent, foreign_key: :output_of_id
     72 end
     73 
     74 @reqr [:name]
     75 @cast @reqr ++ ~w[
     76 	has_beginning has_end
     77 	finished note classified_as
     78 	based_on_id planned_within_id nested_in_id
     79 ]a # in_scope_of_id
     80 
     81 @doc false
     82 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t()
     83 def changeset(schema \\ %__MODULE__{}, params) do
     84 	schema
     85 	|> Changeset.cast(params, @cast)
     86 	|> Changeset.validate_required(@reqr)
     87 	|> Validate.name(:name)
     88 	|> Validate.note(:note)
     89 	|> Validate.class(:classified_as)
     90 	|> Changeset.assoc_constraint(:based_on)
     91 	#|> Changeset.assoc_constraint(:in_scope_of)
     92 	|> Changeset.assoc_constraint(:planned_within)
     93 	|> Changeset.assoc_constraint(:nested_in)
     94 end
     95 end