nars.storage
Class Bag<Type extends Item>

java.lang.Object
  extended by nars.storage.Bag<Type>
Direct Known Subclasses:
ConceptBag, TaskBuffer, TaskLinkBag, TermLinkBag

public abstract class Bag<Type extends Item>
extends java.lang.Object

A Bag is a storage with a constant capacity and maintains an internal priority distribution for retrieval.

Each entity in a bag must extend Item, which has a BudgetValue and a key.

A name table is used to merge duplicate items that have the same key.

The bag space is divided by a threshold, above which is mainly time management, and below, space mamagement. Differences: (1) level selection vs. item selection, (2) decay rate


Field Summary
protected  int capacity
          defined in different bags
protected  int currentCounter
          maximum number of items to be taken out at current level
protected  int currentLevel
          current take out level
(package private) static Distributor DISTRIBUTOR
          shared distributor that produce the probability distribution
protected  java.util.ArrayList<Type>[] itemTable
          array of lists of items, for items on different level
protected  int levelIndex
          index to get next level, kept in individual objects
protected static float LOAD_FACTOR
          hashtable load factor
protected  int mass
          current sum of occupied level
protected  java.util.HashMap<java.lang.String,Type> nameTable
          mapping from key to item
protected static float RELATIVE_THRESHOLD
          relative threshold, only calculate once
protected  boolean showing
          whether this bag has an active window
protected static int THRESHOLD
          firing threshold
protected static int TOTAL_LEVEL
          priority levels
protected  BagWindow window
          display window
 
Constructor Summary
protected Bag()
          constructor, called from subclasses
 
Method Summary
 float averagePriority()
          Get the average priority of Items
protected abstract  int capacity()
          To get the capacity of the concrete subclass
 boolean contains(Type it)
          Check if an item is in the bag
private  boolean emptyLevel(int n)
          Check whether a level is empty
protected abstract  int forgetRate()
          Get the item decay rate, which differs in difference subclass, and can be changed in run time by the user, so not a constant.
 Type get(java.lang.String key)
          Get an Item by key
private  int getLevel(Type item)
          Decide the put-in level according to priority
private  Type intoBase(Type newItem)
          Insert an item into the itemTable, and return the overflow
protected  void outOfBase(Type oldItem)
          Remove an item from itemTable, then adjust mass
 Type pickOut(java.lang.String key)
          Pick an item by key, then remove it from the bag
 void play()
          Resume display
 void putBack(Type oldItem)
          Put an item back into the itemTable
 void putIn(Type newItem)
          Add a new Item into the Bag
 void refresh()
          Refresh display
 void startPlay(java.lang.String title)
          To start displaying the Bag in a BagWindow
 void stop()
          Stop display
 Type takeOut()
          Choose an Item according to priority distribution and take it out of the Bag
private  Type takeOutFirst(int level)
          Take out the first or last Type in a level from the itemTable
 java.lang.String toString()
          Collect Bag content into a String for display
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TOTAL_LEVEL

protected static final int TOTAL_LEVEL
priority levels

See Also:
Constant Field Values

THRESHOLD

protected static final int THRESHOLD
firing threshold

See Also:
Constant Field Values

RELATIVE_THRESHOLD

protected static final float RELATIVE_THRESHOLD
relative threshold, only calculate once

See Also:
Constant Field Values

LOAD_FACTOR

protected static final float LOAD_FACTOR
hashtable load factor

See Also:
Constant Field Values

DISTRIBUTOR

static final Distributor DISTRIBUTOR
shared distributor that produce the probability distribution


nameTable

protected java.util.HashMap<java.lang.String,Type extends Item> nameTable
mapping from key to item


itemTable

protected java.util.ArrayList<Type extends Item>[] itemTable
array of lists of items, for items on different level


capacity

protected int capacity
defined in different bags


mass

protected int mass
current sum of occupied level


levelIndex

protected int levelIndex
index to get next level, kept in individual objects


currentLevel

protected int currentLevel
current take out level


currentCounter

protected int currentCounter
maximum number of items to be taken out at current level


showing

protected boolean showing
whether this bag has an active window


window

protected BagWindow window
display window

Constructor Detail

Bag

protected Bag()
constructor, called from subclasses

Method Detail

capacity

protected abstract int capacity()
To get the capacity of the concrete subclass

Returns:
Bag capacity, in number of Items allowed

forgetRate

protected abstract int forgetRate()
Get the item decay rate, which differs in difference subclass, and can be changed in run time by the user, so not a constant.

Returns:
The number of times for a decay factor to be fully applied

averagePriority

public float averagePriority()
Get the average priority of Items

Returns:
The average priority of Items in the bag

contains

public boolean contains(Type it)
Check if an item is in the bag

Parameters:
it - An item
Returns:
Whether the Item is in the Bag

get

public Type get(java.lang.String key)
Get an Item by key

Parameters:
key - The key of the Item
Returns:
The Item with the given key

putIn

public void putIn(Type newItem)
Add a new Item into the Bag

Parameters:
newItem - The new Item

putBack

public void putBack(Type oldItem)
Put an item back into the itemTable

The only place where the forgetting rate is applied

Parameters:
oldItem - The Item to put back

takeOut

public Type takeOut()
Choose an Item according to priority distribution and take it out of the Bag

Returns:
The selected Item

pickOut

public Type pickOut(java.lang.String key)
Pick an item by key, then remove it from the bag

Parameters:
key - The given key
Returns:
The Item with the key

emptyLevel

private boolean emptyLevel(int n)
Check whether a level is empty

Parameters:
n - The level index
Returns:
Whether that level is empty

getLevel

private int getLevel(Type item)
Decide the put-in level according to priority

Parameters:
item - The Item to put in
Returns:
The put-in level

intoBase

private Type intoBase(Type newItem)
Insert an item into the itemTable, and return the overflow

Parameters:
newItem - The Item to put in
Returns:
The overflow Item

takeOutFirst

private Type takeOutFirst(int level)
Take out the first or last Type in a level from the itemTable

Parameters:
level - The current level
Returns:
The first Item

outOfBase

protected void outOfBase(Type oldItem)
Remove an item from itemTable, then adjust mass

Parameters:
oldItem - The Item to be removed

startPlay

public void startPlay(java.lang.String title)
To start displaying the Bag in a BagWindow

Parameters:
title - The title of the window

play

public void play()
Resume display


refresh

public void refresh()
Refresh display


stop

public void stop()
Stop display


toString

public java.lang.String toString()
Collect Bag content into a String for display

Overrides:
toString in class java.lang.Object