Differences

This shows you the differences between two versions of the page.

code:turtle_based_database_example_1 [2009/11/06 11:22]
88.89.146.16
code:turtle_based_database_example_1 [2009/11/06 13:45] (current)
james old revision restored
Line 1: Line 1:
-**Hacked by AlfaHacker** +====== How to create a database using turtles in NetLogo ====== 
-FIXME+ 
 +==== 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 
 + 
 +[[prim:breed(keyword)|Breeds]] and [[prim:turtles-own|-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 [[prim: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 ==== 
 +<code netlogo> 
 +breed [ customers customer ] ;; customer table 
 +breed [ orders order ] ;; orders table 
 +breed [ products product ] ;; products table 
 +breed [ order-items order-item ] many-to-many foreign-key table 
 + 
 +customers-own 
 +[ name address phone ;; regular variables 
 +  my-orders ;; will contain the agentset of all this customer's orders 
 +
 + 
 +orders-own 
 +[ customer-id ;; contains a ref to the customer 
 +  order-date ship-date  
 +  partial? ;; true if some items were "backordered" 
 +
 + 
 +products-own 
 +[ name price qty ] 
 + 
 +order-items-own ;; the items on in an orderlinks products to orders  
 +;; represents a product in an order 
 +[ product-id  
 +  order-id 
 +  customer-id 
 +  qty-ordered    ;; how many the customer wanted 
 +  qty-fulfilled ;; how many the customer has gotten 
 +  unit-price    ;; the unit-price at the time the order was placed 
 +  backordered?  ;; true if qty-fullfilled is less than qty-ordered 
 +
 +</code> 
 + 
 + 
 + 
 + 
 +==== Example Queries ==== 
 + 
 +=== Simple Selection query === 
 + 
 +<code netlogo4> 
 +;; list of bargain products 
 +let bargain-products-in-stock products with [ price < 1.00 and qty > 0 ] 
 +</code> 
 + 
 +=== Queries with indirect lookups === 
 + 
 +<code netlogo4> 
 +;; 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) 
 +
 +</code> 
 + 
 +<code netlogo4> 
 +;; 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 ] ] 
 +</code> 
 + 
 +=== Many to Many query === 
 +<code netlogo4> 
 +;; 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>
 
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