Load File NetLogo Model
Produced for the book series "Exercises for Artificial Intelligence";
Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.
powered by NetLogo
view/download model file: Load-File.nlogo
WHAT IS IT?
This model shows how to load text from a file.
WHAT IS ITS PURPOSE?
The model is a solution to Exercise 3.5.3 and 3.5.4 in the book "Exercises for Artificial Intelligence - Agents and Environments."
HOW IT WORKS
The procedure load-file reads each line of the input file one at a time, and appends it to a string variable called text which is displayed as an input box in the Interface. If the type-of-file parameter is set to "NetLogo Model", it will search for the sequence of characters that delimits the end of the source code in NetLogo model files that have been saved to disk (stored in files with the ".nlogo" extension).
HOW TO USE IT
Press the Load File to load a general text file, or Load NetLogo Model button to load a NetLogo model file. This will then open a dialog that allows the user to choose an existing disk file on the system. The text from the file will then be loaded directly into the Text input box.
Note that this model will not work from a Web browser when it has been saved as an applet as it needs to open a file.
The buttons in the Interface are defined as follows:
- Load File: This will load a text file from disk. This file can be any file on disk. If it is a NetLogo model file with the ".nlogo" extension, it will load the full file rather than just the source code when the Load NetLogo Model button is pressed.
- Load NetLogo Model: This will load the source code for a NetLogo model text file from disk. This does not check whether the file really is a NetLogo model, however, so if it isn't, pressing this button will simply have the same effect as the Load File button.
THINGS TO NOTICE
Notice what happens when binary files, such as JPEG and GIF files, are loaded. Also notice what happens when you load a Word document.
THINGS TO TRY
Try loading the same NetLogo model using both the Load File and Load NetLogo Model buttons to see what happens.
Try loading other text files, such as a Word document or a HTML document to see what happens.
If a NetLogo model file has been loaded, see what happens if you add a button to run the code (using the run command).
EXTENDING THE MODEL
Try modifying the code so that it checks the type of file that is being loaded and rejects it if it isn't a NetLogo model or text.
Note the use of the word command to concatenate each input line onto the end of the text string.
CREDITS AND REFERENCES
This model was created by William John Teahan.
To refer to this model in publications, please use:
Load File NetLogo model.
Teahan, W. J. (2010). Exercises for Artificial Intelligence. Ventus Publishing Aps
; Load File model. ; ; This model will load text from a file on disk. ; ; Copyright 2010 William John Teahan. All Rights Reserved. to load-file [ type-of-file ] ; This loads the file. clear-all let line-of-code "" let end-of-code? false set text "" file-open user-file while [not file-at-end? and not end-of-code?] [ set line-of-code file-read-line ifelse (line-of-code = "@#$#@#$#@") and (type-of-file = "NetLogo Model") [ set end-of-code? true ] [ set text (word text line-of-code "\n") ] ] file-close end ; ; Copyright 2010 by William John Teahan. All rights reserved. ; ; Permission to use, modify or redistribute this model is hereby granted, ; provided that both of the following requirements are followed: ; a) this copyright notice is included. ; b) this model will not be redistributed for profit without permission ; from William John Teahan. ; Contact William John Teahan for appropriate licenses for redistribution for ; profit. ; ; To refer to this model in publications, please use: ; ; Teahan, W. J. (2010). Load File NetLogo model. ; Exercises for Artificial Intelligence. Ventus Publishing Aps. ;