Why would I want to do that?

Because your model's data management requirements, such as the agent's memory, is more complicated than can be reasonably managed with lists, and you don't want or don't have an external database extension to work from.

How does it work?

You can use breeds of turtles to create both flat and relational databases. NetLogo's built-in WITH operator can be used to query the database. Many-to-one, one-to-many, and many-to-many relationships can be modeled easily.

  • Breeds are tables
  • Breeds-own'd variables are fields
  • Turtles of a breed are records in that breeds' table
  • Agentsets are groups of records (i.e. recordsets).
  • Queries are created using the WITH operator, and other agentset operators
  • A turtle is its own primary key.
  • A foreign key is a reference to a turtle

Breeds and -OWN'd variables form the structure of the database. Breeds fill the role of tables, while the variables -own'd by the breed represent database fields. Each individual turtle is a record in the table that is its breed. Since each turtle is unique, every record automatically gets a unique primary key–the turtle itself. In records that require a foreign key to link to another turtle/record, the foreign key is a reference to the turtle. The with operator is used to select groups of agents/records. This lets us easily do useful things with the database, such as get groups of records, and do indirect lookups.

Example

Example Database Breeds and Variables

Example Queries

Simple Selection query

;; list of bargain products
let bargain-products-in-stock products with [ price < 1.00 and qty > 0 ]

Queries with indirect lookups

;; print the total price of each customer's order
ask customers
[ let total-price sum [ price ] of order-items with [ customer-id = myself ]
  print (word name " : " total-price)
]
;; Products with insufficient quantity in stock to fullfill all pending orders
let backorders products with [ qty < sum [ qty-ordered - qty-fulfilled ] of orders with [ product-id = myself ] ]

Many to Many query

;; print the orders for each customer
ask customers
[ print ( word "Customer: " name " (#" who ")" )
  let myorders orders with [ customer-id = myself ]
  ask myorders
  [ print ( word "  Order#" who )
    let myitems order-items with [ order-id = myself ]
    print "    item#, name"
    ask myitems
    [ ask product-id
      [ print ( word "    #" who ", " name )
      ]
    ]
  ]
]
 
code/turtle_based_database_example_1.txt · Last modified: 2009/11/06 13:45 by james
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki