diff --git a/src/Connection.f08 b/src/Connection.f08 index 28811ea8fb60e698dfc84acfd3a1be6c2f201238..18f14a7c4ec4ba83ff3d056332059db7fe5e4af2 100644 --- a/src/Connection.f08 +++ b/src/Connection.f08 @@ -1,13 +1,11 @@ -module Connection_mod + module Connection_mod implicit none public ! TODO smazat - !> Dummy class Neuron - !! blabla type :: Neuron - real :: state ! TODO v realne tride 'private'! + real :: state contains procedure :: get_state => get_state_impl @@ -20,41 +18,57 @@ module Connection_mod !> Represents a connection between two neurons. type, abstract :: Connection - class(Neuron), pointer, private :: input_neuron - class(Neuron), pointer, private :: output_neuron - real, private :: weight + private + + class(Neuron), pointer :: input_neuron !< Pointer to an input neuron + class(Neuron), pointer :: output_neuron !< Pointer to an output neuron + real :: weight !< Weight of the connection contains - !> Adds a gven value to the current weight of the + !> Initializes the common Connection class components + !! 'input_neuron', 'output_neuron' and 'weight'. + !! I.e. serves similarly to an abstract constructor. + procedure :: init_components => init_components_impl + + !> Adds a given value to the current weight of the !! connection. !! @param added_value Number (real) to be added to the current weight procedure :: adjust_weight => adjust_weight_impl - ! Constructors - procedure :: new_connection => new_connection_impl - - ! Getters and setters + !> Getter for the private 'input_neuron' component + !! @return Pointer to the input neuron (type Neuron, pointer) procedure :: get_input_neuron => get_input_neuron_impl + + !> Getter for the private 'output_neuron' component + !! @return Pointer to the output neuron (type Neuron, pointer) procedure :: get_output_neuron => get_output_neuron_impl + + !> Getter for the private 'weight' component + !! @return Weight of the connection (type real) procedure :: get_weight => get_weight_impl end type Connection + !> Represents a connection between two neurons. + !! Able to pass a signal from an input neuron to + !! an output one. type, extends(Connection) :: IntervalConnection contains + + !> Passes (assigns) the product + !! input neuron state * weight) + !! to an output neuron. procedure :: pass_signal => pass_signal_impl end type IntervalConnection !------------!------------------------------------------------------------------------------ ! Interfaces ! !------------! - - ! For signatures of abstract functions - - !interface Connection - ! - !end interface Connection + interface IntervalConnection + !> Constructor of IntervalConnection class + module procedure :: new_interval_connection + end interface IntervalConnection contains !------------------------!------------------------------------------------------------------ @@ -83,6 +97,17 @@ module Connection_mod !------------------! ! class Connection ! !------------------! + subroutine init_components_impl(this, input_neuron, output_neuron, weight) + class(Connection), intent(inout) :: this + type(Neuron), pointer :: input_neuron + type(Neuron), pointer :: output_neuron + real, intent(in) :: weight + + this%input_neuron => input_neuron + this%output_neuron => output_neuron + this%weight = weight + end subroutine init_components_impl + subroutine adjust_weight_impl(this, added_value) class(Connection), intent(inout) :: this real, intent(in) :: added_value @@ -96,28 +121,29 @@ module Connection_mod subroutine pass_signal_impl(this) class(IntervalConnection), intent(in) :: this - call this%output_neuron%set_state(5.0) + call this%output_neuron%set_state(this%input_neuron%get_state() * this%weight) end subroutine pass_signal_impl !--------------!------------------------------------------------------------------------ ! Constructors ! !--------------! - subroutine new_connection_impl(this, input_neuron, output_neuron, weight) - class(Connection), intent(out) :: this - class(Neuron), pointer, intent(in) :: input_neuron - class(Neuron), pointer, intent(in) :: output_neuron - real, intent(in) :: weight + function new_interval_connection(input_neuron, output_neuron, weight) result(ret_obj) + type(Neuron), pointer :: input_neuron + type(Neuron), pointer :: output_neuron + real, intent(in) :: weight + type(IntervalConnection), pointer :: ret_obj - this%input_neuron => input_neuron - this%output_neuron => output_neuron - this%weight = weight + allocate(ret_obj) - print *, 'Constructor!' - end subroutine new_connection_impl + call ret_obj%init_components(input_neuron, output_neuron, weight) + end function new_interval_connection !-------------------!------------------------------------------------------------------- ! Getters & Setters ! !-------------------! + + !> asdf + !! aaaa function get_input_neuron_impl(this) result (input_neuron) class(Connection), target, intent(in) :: this class(Neuron), pointer :: input_neuron @@ -131,13 +157,14 @@ module Connection_mod output_neuron => this%output_neuron end function get_output_neuron_impl - + function get_weight_impl(this) result (weight) class(Connection), intent(in) :: this real :: weight weight = this%weight end function get_weight_impl + end module Connection_mod program a @@ -150,14 +177,15 @@ program a type(Neuron), pointer :: n2_p type(Neuron), pointer :: dummy_p - type(IntervalConnection) :: con - + type(IntervalConnection), pointer :: con + n1 = Neuron(21) n2 = Neuron(13) n1_p => n1 n2_p => n2 - call con%new_connection(n1, n2, 5.12) + + con => IntervalConnection(n1_p, n2_p, 5.0) print *, 'weight: ', con%get_weight() @@ -166,4 +194,8 @@ program a dummy_p => con%get_output_neuron() print *, dummy_p%get_state() + call con%pass_signal() + + print *, dummy_p%get_state() + end program a