lookuptable example
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
A short note to introduce the lookuptablefunctionality.nls
a breed for maintaining and reporting lookuptables
the nls. should be self-supportive, i.e. including the .nls should be enough to make use of the whole lookuptable functionality
HOW IT WORKS
The breed is declared in the nls. provides observer/turtle/patch procedures for external access (luto_*) provides tlookuptable own procedures for internal manipulation (lutt*)
rp are reporters p are common procedures
everything is stored as string. So if a number is expercted use "read-from-string" might be needed.
a list of procedures for external access
the setup: to-report lutorpsetuplookuptable [filename]
lookup a single value (string or value) to-report lutorpShowFromLookuptable [whostore colname rowname] to-report lutorpShowValueFromLookuptable [whostore colname rowname]
get the column or row names to-report lutorpColumnsInLookuptable [whostore] to-report lutorpRowsInLookuptable [whostore]
get a column from the table (string or value) to-report lutorpShowColumnFromLookuptable [whostore columnname] to-report lutorpShowColumnValuesFromLookuptable [whostore columnname]
no extraction of rows is implemented yet
a list of procedures for internal access
setup (no check on validity of filename) to luttpreadtable [filename]
lookup (always string!) to-report luttrplookup [colname rowname]
reporting whole column (string or value)
to-report luttrpcolumn [colname]
to-report luttrpcolumnvalues [colname]
HOW TO USE IT
Make sure you store the who number of the t_lookuptable you initialise to be able to access the table
THINGS TO NOTICE
There is no check on completeness of the lookuptable (i.e., if all rows/columns are equal size)
the lookup function will provide a false if the column or rowname is not in the list, but the program will give an error if the item does not exist in the list.
CREDITS AND REFERENCES
If it doesn't work, blame me Geerten.Hengeveld@wur.nl
Comments and Questions
;__includes ["lookuptablefunctionality.nls"] globals [lookuptablenr] ;implements a crude lookuptable breed. ;a turtle of this breed contains a list of row names, a list of column names and a table ;a procedure is provided to initialise a turtle with a filename where it can read a table from ;a procedure is provided to read the table into the turtle (extracting the lists of rownames and columnnames) ;a procedure is provided to report the cell content as string based on row and column names (as strings) ; ; #### this implementation does not check for the correctness of the table ; ####in case of repeated row or columnames, only the first can be found, no warnings given ; ####this does not hanlde missing values in the table ; ; ### the table is stored as a list of lists (each row is a list, for simplicity in reading the file) ; ### the current implenetation sees the rownames as part of the lookuptable, the columnames are not ; ; ### the input file should be a comma-seperated fulle matrix ; e.g. ; names,eyes,hair,shirt ; pete,blue,brown,black ; dan,brown,blond,white ; ; in this example the reporter would need the inputs "shirt" and "pete" to report "black" ; ; If it does not work, blame me Geerten.Hengeveld@wur.nl breed [t_lookuptables t_lookuptable] t_lookuptables-own [lutt_table lutt_collist lutt_rowlist] to-report luto_rp_setuplookuptable [filename] ; observer procedure from the lookuptable let whostore 0 create-t_lookuptables 1 [ lutt_p_readtable filename set hidden? true ; this also makes it difficult to visually inspect the lookuptable set whostore who ] report whostore ; use this to be able to extract information from this lookuptable! end to-report luto_rp_ShowFromLookuptable [whostore colname rowname] ; observer procedure from the lookuptable report [lutt_rp_lookup colname rowname] of t_lookuptable whostore end to-report luto_rp_ShowValueFromLookuptable [whostore colname rowname] ; observer procedure from the lookuptable let tablestring [lutt_rp_lookup colname rowname] of t_lookuptable whostore ifelse (tablestring != false) [ report read-from-string [lutt_rp_lookup colname rowname] of t_lookuptable whostore ] [report false] end to-report luto_rp_ColumnsInLookuptable [whostore] report [lutt_collist] of t_lookuptable whostore end to-report luto_rp_RowsInLookuptable [whostore] report [lutt_rowlist] of t_lookuptable whostore end to-report luto_rp_ShowColumnFromLookuptable [whostore columnname] report [lutt_rp_column columnname] of t_lookuptable whostore end to-report luto_rp_ShowColumnValuesFromLookuptable [whostore columnname] report [lutt_rp_columnvalues columnname] of t_lookuptable whostore end ;--------------lookup table internals------------- to lutt_p_readtable [filename] ; turtle procedure from the lookuptable ;should actually be a reporter procedure reporting true if initalisation worked... let temp_list (list 0) ; initialise the lists use but-first lateron to remove the 0 set lutt_collist (list 0) set lutt_rowlist (list 0) set lutt_table (list 0) file-open filename ;should be a csv - no check currently on whether the file opens correctly! let p file-read-line ;the header line, should be put into the list lutt_collist while [position "," p != false] [ set lutt_collist lput (substring p 0 (position "," p)) lutt_collist set p substring p (position "," p + 1) length p ] set lutt_collist lput p lutt_collist ;add the last element set lutt_collist but-first lutt_collist ;remove the initial 0 ;initialise the table: each row will be a list in the table while [not file-at-end?] [ set p file-read-line set temp_list (list 0) while [position "," p != false] [ set temp_list lput ( substring p 0 (position "," p)) temp_list set p substring p (position "," p + 1) length p ;store elements in the right order! ] set temp_list lput p temp_list set temp_list but-first temp_list ;remove 0 from start set lutt_table lput temp_list lutt_table ] set lutt_table but-first lutt_table foreach lutt_table ; initialise a list of all rownames: the first items of the rows in the table [ set lutt_rowlist lput item 0 ? lutt_rowlist ] set lutt_rowlist but-first lutt_rowlist file-close end ;this reporter will report a string! use read-from-string to interpret output as value to-report lutt_rp_lookup [colname rowname] ; turtle procedure from the lookuptable let colnr position colname lutt_collist let rownr position rowname lutt_rowlist ifelse (colnr != false and rownr != false) [ let targetrow item rownr lutt_table report item colnr targetrow ] [ report false ] end ;report a whole column to-report lutt_rp_column [colname] ; turtle procedure from the lookuptable let colnr position colname lutt_collist let templist (list 0) let targetrow 0 ifelse (colnr != false) [ let i 0 repeat length lutt_rowlist [ set targetrow item i lutt_table set templist lput item colnr targetrow templist set i i + 1 ] set templist but-first templist report templist ] [ report false ] end to-report lutt_rp_columnvalues [colname] ; turtle procedure from the lookuptable let colnr position colname lutt_collist let templist (list 0) let targetrow 0 ifelse (colnr != false) [ let i 0 repeat length lutt_rowlist [ set targetrow item i lutt_table set templist lput read-from-string item colnr targetrow templist set i i + 1 ] set templist but-first templist report templist ] [ report false ] end ;;----------not part of the nls to setup ca ;creates a turtle from breed t_lookuptable, reads the table from the .csv and stores the who number of this turtle in the global lookuptable nr set lookuptablenr luto_rp_setuplookuptable "testtable.csv" end
There are 2 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
lookuptablefunctionality.nls | background | The nls that implements the lookuptable breed and its procedures | over 12 years ago, by Geerten Hengeveld | Download |
Geerten Hengeveld
why posting this
In implementing this breed, I found that you can completely declare all breed-relevant information in one .nls (including the breed[] and breed-own[] section) which makes this .nls a library that implements this fun code example for a lookuptable in a list-of-lists style. just download the .nls from the files section, and include it in your model, and you can use the functionality...
Posted over 12 years ago