Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added-comment-on-functions #52

Merged
merged 2 commits into from
Feb 19, 2025
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 attention-bank/attention-value/getter-and-setter.metta
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,3 @@
)

(= (getType $pattern) (get-type $pattern))

38 changes: 33 additions & 5 deletions attention-bank/bank/atom-bins/atombins.metta
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
; Function to insert an atom into the specifc bin
!(bind! &atombin (new-space))

;Function: insertAtom
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding function names as a comment is not necessary as that information is already available on the function it self.

;Description: Inserts an atom into the specified bin index within &atombin space.
; If the atom's type is undefined(ifthe atom has neither AV nor STV)
; , it returns an error message.
; If the bin(the index of the bin in which the atom is to be inserted to)is empty,
; it creates a new bin and adds the atom.
; Otherwise, it updates the existing bin by adding the new atom.
;Parameters:
; $bin_index: The index of the bin where the atom should be inserted.
; $pattern: The atom to be inserted.
;Returns: A confirmation message ("Atom Inserted") if successful,
; or an error message if the atom is invalid.

(: insertAtom (-> Number Atom empty))
(= (insertAtom $bin_index $pattern)
(if(== (getType $pattern) %Undefined%)
Expand All @@ -41,7 +54,18 @@
)
)

;Function to remove an atom from a specific bin
;Function: removeAtom
;Description: Removes a specified atom from a given bin from &atombin space.
; If the atom's type is undefined (if the atom has neither AV nor STV),
; it returns an error message.
; If the bin is empty, it returns a message indicating that there is nothing to remove.
; Otherwise, it updates the bin by removing the specified atom.
;Parameters:
; $bin_index: The index of the bin from which the atom should be removed.
; $atom: The atom to be removed.
;Returns: A confirmation message ("Atom Removed") if successful,
; or an error message if the atom is invalid or the bin is empty.

(: removeAtom (-> Number Atom empty))
(= (removeAtom $bin_index $atom)
(if (== (getType $atom) %Undefined%)
Expand All @@ -50,8 +74,6 @@
(if(== $bin_contents ())
("Bin is empty") ; Bin is empty, nothing to remove
(let $updated_contents (remove_atom_helper $atom (car-atom $bin_contents))
;(if (== $updated_contents ())
; (remove-atom &atombin ($bin_index $bin_contents)) ; Remove empty bin
(match &atombin ($bin_index $x)
(let*
(
Expand All @@ -61,15 +83,21 @@
("Atom Removed")
)
)
;)

)
)
)
)
)

;Function: remove_atom_helper
;Description: Recursively removes a specified atom from a list of contents.
; If the atom is found, it is skipped in the resulting list.
;Parameters:
; $atom: The atom to be removed.
; $contents: The list of atoms from which to remove the specified atom.
;Returns: A new list with all occurrences of the specified atom removed.

; Helper function to remove an atom from a list of contents
(: remove_atom_helper (-> Atom List List))
(= (remove_atom_helper $atom $contents)
(if(== $contents ())
Expand Down
15 changes: 12 additions & 3 deletions attention-bank/bank/atom-bins/get-min-max-content.metta
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,28 @@
)
)

; Calculates the total number of atoms across all bins in &atombin
;Function: binSize
;Description: Calculates the total number of atoms across all bins in &atombin space.
; It retrieves all bin indices and computes the cumulative sum of their sizes.
;Parameters: None.
;Returns: The total number of atoms across all bins.
(: binSize (-> Number))
(= (binSize)
(let* (
($atombin (AtomBin))
($indices (collapse (match $atombin ($x $y) $x)))
($binsSize (sumSizes $indices)) ;cumulative sum of all sizes of bins in atombin
($binsSize (sumSizes $indices))
)
$binsSize
)
)

; Recursively sums the sizes of all bins in a list of bins
;Function: sumSizes
;Description: Recursively computes the total number of atoms across all bins in a given list of bin indices.
; It iterates through the list of indices, retrieves the size of each bin, and sums them up.
;Parameters:
; $indices: A list of bin indices whose sizes should be summed.
;Returns: The cumulative size of all bins in the given list.
(: sumSizes (-> Expression Number))
(= (sumSizes $indices)
(if (== $indices ())
Expand Down
68 changes: 36 additions & 32 deletions attention-bank/bank/attention-bank.metta
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
; Define initial variables and insert them in newspace called space
!(bind! &space (new-space))
!(add-atom &space (startingFundsSTI 100000))
!(add-atom &space (fundsSTI 100000))
Expand All @@ -12,28 +11,13 @@
!(add-atom &space (LTIAtomWage 10))


; Function to compute the minimum of two values
;(: min (-> Number Number Number))
;(= (min $a $b)
; (if (< $a $b) $a $b)
;)

; Function to compute the maximum of two values
;(: max (-> Number Number Number))
;(= (max $a $b)
; (if (> $a $b) $a $b)
;)

; Function to calculate STI wage
; Purpose:
; Calculates the adjustment factor for Short-Term Importance (STI) based on:
; - Current STI funds
; - Target STI funds
; - The buffer for STI adjustments
; Logic:
; Description: Calculates the adjustment factor for Short-Term Importance (STI)
; - Compute the normalized difference between current funds and the target.
; - Limit the adjustment factor between -1.0 and 1.0.
; - Return the adjusted STI wage.
; Parameters: None.
; Returns: The adjusted STI wage value.
(: calculateSTIWage (-> Number))
(= (calculateSTIWage)
(let* (
Expand All @@ -52,15 +36,13 @@
)

; Function to calculate LTI wage
; Purpose:
; Calculates the adjustment factor for Long-Term Importance (LTI) based on:
; - Current LTI funds
; - Target LTI funds
; - The buffer for LTI adjustments
; Logic:
; Description:
; Calculates the adjustment factor for Long-Term Importance (LTI)
; - Compute the normalized difference between current funds and the target.
; - Limit the adjustment factor between -1.0 and 1.0.
; - Return the adjusted LTI wage.
; Parameters: None.
; Returns: The adjusted LTI wage value
(: calculateLTIWage (-> Number))
(= (calculateLTIWage)
(let* (
Expand All @@ -79,14 +61,15 @@
)

; Function to stimulate an atom with a given stimulus
; Purpose:
; Description:
; Adjusts the AttentionValue (STI, LTI, VLTI) of an atom based on:
; - Calculated STI and LTI wages
; - A stimulus factor
; Logic:
; - Calculate the new STI and LTI values using the wages and stimulus.
; - Calculate the changes in STI and LTI funds.
; - Call attentionValueChanged to apply the updates.
;Parameters:
; $pattern: The atom receiving the stimulus.
; $stimulus: The factor by which STI and LTI will be adjusted.
;Returns: Nothing (updates the atom and related values)
(: stimulate (-> Atom Number empty))
(= (stimulate $pattern $stimulus)
(let* (
Expand All @@ -111,12 +94,20 @@
)

; Function to handle attention value changes and update global funds
; Purpose:
; Updates the AttentionValue of an atom and adjusts the global STI and LTI funds.
; Logic:
; Description:
; Updates the AttentionValue of an atom and adjusts the global STI and LTI funds
; -set the new attention value of the atom
; - Remove the current fundsSTI and fundsLTI values from the AtomSpace.
; - Add the updated fundsSTI and fundsLTI values to the AtomSpace.
; - Inserts the atom to the attentional focus if possible
; - Updates the Bin placement of the atom
;Parameters:
; $pattern: The atom whose AttentionValue is being updated.
; $newSTIFund: The new STI fund value.
; $newLTIFund: The new LTI fund value.
; $newAv: The new AttentionValue to be set for the atom.
;Returns: The result of updating the AtomBin.


(: attentionValueChanged (-> Atom Number Number AttentionValue empty))
(= (attentionValueChanged $pattern $newSTIFund $newLTIFund $newAv)
Expand All @@ -134,6 +125,13 @@
)
)

;Function: updateLTIFunds
;Description: Updates the global LTI funds by replacing the old value with a new one
;Parameters:
; $old: The previous LTI fund value
; $new: The new LTI fund value
;Returns: Nothing (updates the fund in the AtomSpace)

(: updateLTIFunds (-> Number Number empty))
(= (updateLTIFunds $old $new)
(match &space (fundsLTI $old)
Expand All @@ -144,6 +142,12 @@
)
)

;Function: updateSTIFunds
;Description: Updates the global STI funds by replacing the old value with a new one.
;Parameters:
; $old: The previous STI fund value.
; $new: The new STI fund value.
;Returns: Nothing (updates the fund in the AtomSpace).
(: updateSTIFunds (-> Number Number empty))
(= (updateSTIFunds $old $new)
(match &space (fundsSTI $old)
Expand Down
75 changes: 58 additions & 17 deletions attention-bank/bank/attentional-focus/attentional-focus.metta
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

(= (maxAFSize) 100)

;checks if an atom is in attentional focus
;Function: atomIsInAF
;Description: Checks if a given atom is in the attentional focus space.
;Parameters:
; $atom: The atom to check.
;Returns: True if the atom is in attentional focus space, otherwise False.

(: atomIsInAF (-> Atom Bool))
(= (atomIsInAF $atom)
Expand All @@ -12,15 +16,25 @@
)
)

;checks if an atom is in attentional focus
;Function: atomIsNotInAF
;Description: Checks if a given atom is NOT in the attentional focus.
;Parameters:
; $atom: The atom to check.
;Returns: True if the atom is not in attentional focus, otherwise False.

(: atomIsNotInAF (-> Atom Bool))
(= (atomIsNotInAF $atom)
(not (atomIsInAF $atom))
)

;adds an atom to the attentional focus if it is valid

;Function: addAtomToAF
;Description:
; if the atom has neither AV nor STV,it returns an error message
; if an atom is valid,it will add it to the attentional focus space.
;Parameters:
; $atom: The atom to add.
;Returns: A message indicating success or failure.
(: addAtomToAF (-> Atom Symbol))
(= (addAtomToAF $atom)
(if (== (getType $atom) %Undefined%)
Expand All @@ -32,24 +46,32 @@
)
)

; Collapse space to list of atoms

;Function: getAtomList
;Description: Retrieves a list of all atoms currently in attentional focus space.
;Parameters: None.
;Returns: A list of atoms in attentional focus space.
(: getAtomList(->Symbol))
(= (getAtomList)
(collapse (get-atoms &attentionalFocus))
)

;it returns the total number of atoms in the attentional focus

;Function: attentionalFocusSize
;Description: Returns the total number of atoms in the attentional focus space
;Parameters: None
;Returns: The number of atoms in attentional focus space
(: attentionalFocusSize (-> Number))
(= (attentionalFocusSize)
(let $atomList (getAtomList)
(size-atom $atomList)
)
)

; Helper function for filtering atoms with STI less than the pivot

;Function: lessThanSti
;Description: Helper function that checks if the STI of $elem is less than the STI of $pivot.
;Parameters:
; $elem: The first atom.
; $pivot: The atom to compare against.
;Returns: True if $elem has a lower STI than $pivot, otherwise False.
(: lessThanSti (-> Atom Atom Bool))
(= (lessThanSti $elem $pivot)
(if (or (== $elem ()) (== $pivot ()))
Expand All @@ -58,7 +80,13 @@
)
)

; Helper function for filtering atoms with STI greater than or equal to the pivot
;Function: greaterEqualSti
;Description: Helper function that checks if the STI of $elem is greater than or equal to the STI of $pivot.
;Parameters:
; $elem: The first atom.
; $pivot: The atom to compare against.
;Returns: True if $elem has an equal or higher STI than $pivot, otherwise False.


(: greaterEqualSti (-> Atom Atom Bool))
(= (greaterEqualSti $elem $pivot)
Expand All @@ -68,7 +96,11 @@
)
)

; Sorts a list of atoms by their STI values in ascending order
;Function: sortAtomsBySti
;Description: Sorts a list of atoms by their STI values in ascending order using recursive filtering.
;Parameters:
; $atoms: A list of atoms.
;Returns: A sorted list of atoms in ascending order of STI.

(: sortAtomsBySti (-> Symbol Symbol))
(= (sortAtomsBySti $atoms)
Expand All @@ -88,8 +120,11 @@
)
)

; Sorts a list of atoms by their STI values in descending order

;Function: sortAtomsByStiDescending
;Description:Sorts a list of atoms by their STI values in descending order using recursive filtering.
;Parameters:
; $atoms: A list of atoms.
;Returns: A sorted list of atoms in descending order of STI.
(: sortAtomsByStiDescending (-> Symbol Symbol))
(= (sortAtomsByStiDescending $atoms)
(if (== $atoms ())
Expand All @@ -108,9 +143,7 @@
)
)

;gets the atom with the lowest STI value in the attentional focus

;(: getLowestStiAtomInAFHelper (-> List Atom Number Atom))
(= (getLowestStiAtomInAFHelper $atoms $minAtom $minSTI)
(if (== $atoms ())
$minAtom
Expand Down Expand Up @@ -165,6 +198,12 @@
;serve logging the additional and removal of an atom as i reviewed in the example/AtomSpaceEventSubscribeExample.cc
;and we may implement it in the future if needed

;Function: updateAttentionalFocus
;Description: Updates the attentional focus by adding a new atom,
; or replacing the lowest STI atom if the attentional focus is full.
;Parameters:
; $atom: The atom to be added or updated.
;Returns: Nothing if no update occurs, otherwise the atom is added.
(: updateAttentionalFocus (-> Atom empty))
(= (updateAttentionalFocus $atom)
(let*
Expand Down Expand Up @@ -199,8 +238,10 @@
)
)

; Function to retrieve a random atom not in attentional focus

;Function: getRandomAtomNotInAF
;Description: Retrieves a random atom that is not in attentional focus.
;Parameters: None.
;Returns: A randomly selected atom from outside attentional focus.
(: getRandomAtomNotInAF (-> Atom))
(= (getRandomAtomNotInAF)
(let*
Expand Down
Loading