NetLogo: Using turtles-own in multiple included files - netlogo

I am working on a model in NetLogo and I would like to separate procedures in different files, grouped by their purpose. I am doing so by writing that code in different files and using __include["name_of_file_1.nls" "name_of_file_2.nls" ...].
As I would like it to be tidy as possible, I use turtles-own[ ] in each included file, declaring the variables that will be used in the procedures from that file. So, in case I'd like to modify the name of that variable or anything else, I would only need to go to that specific included file and modifying the variable in there. Instead of modifying it in the code tab and then switching to one of the included files.
But, if I work like this, turtles won't own the variables declared in a file. For example, I have all setup procedures defined in a file. When the setup button is clicked from the interface, setup recalls all procedures contained in the included file setup_procedures.nls. However, if after setting up, I inspect some of the turtles, they don't own the variables declared with turtles-own[ ] in the setup_procedures.nls file.
Should I declare all variables in the code tab? Is there a way to make it work as I intended?

Related

How do I make a list of agent variables?

I'm trying to make my model compatible with an interface framework that can't handle the csvs I normally export to, and requires lists. What I need to do, specifically, is export a list of lists of variables associated with each of a certain breed of agent.
Ideally:
set master-list (foreach person set traits-list list (who) (color) (heading) (xcor) (ycor) (etc...))
But the primary issue I'm having is that after the first two variables set up in the set traits-list list (items) way, it starts throwing up errors on any subsequent variables. I can just lput each individual variable, but that seems like a really unnecessarily messy way to set it up. Am I missing something?
I've tried seeing if it's the individual variable that's the issue, but the error persists no matter what the third variable is.
If anyone wants to look at the complete code in question, I'm trying to make https://github.com/efyoungud/stationfire work with https://github.com/hlynka-a/SRTI.
JenB had the answer with needing the bracket.
Final, functional code:
ask people [ foreach [self] of people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)(breed)(hidden?)(size) (alarmed?) (age)(visited?)(group-number) (group-type) (group-constant)(speed) (leadership-quality) (leader) (goal) (energy)(speed-limit)
)]] ; doesn't include next-desired-patch or path because that's calculated each step and doesn't need to be exported
set master-list [traits-list] of people
end ```

How to access the ICElements of local variables(variables inside function) and variables in header file?

Objective is to access the elements of C-file in eclipse to check customized naming rules for C-elements(global variable, local variable, function declarations).
Tried to access the C-file elements as mentioned below. In this case, only able to access global variables and function names in the .c file.
How local variables(variables inside functions) & variables in included header files can be accessed?
ITranslationUnit tu = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input);
ICElement[] ele= src.getChildren();
Local variables
ICElement is mostly used for representing code elements in CDT's various views, such as the Outline View or Type Hierarchy. As such, local variables (which do not appear in these views) do not have an ICElement representation.
For code analysis use cases like this, it's probably better to use the AST API. The AST is a detailed representation of the entire code in a file. It can be accessed via ITranslationUnit.getAST(). You can then use an ASTVisitor to traverse the AST and visit any declarations you like and check their names.
Variables in included header files
There are two sub-categories here: header files inside the project directory, and header files outside the project directory.
Header files inside the project directory have their own ITranslationUnit, and you can use either the ICElement API or the AST API to analyze them with that ITranslationUnit as a starting point. Note that a file does not need to be open in an editor to obtain an ITranslationUnit for it. You can traverse all of the files in the project with something like ICElementVisitor, with the ICProject as a stating point.
Header files outside the project directory do not have an ITranslationUnit, and there is no straightforward way to obtain an AST for them. However, assuming your project's indexer is enabled, the indexer does create ASTs for them and store information from those ASTs in the project's index, which you could examine. There are index APIs that can be used to traverse the index; some relevant ones are IIndexManager.getIndex(ICProject), IIndex.getAllFiles(), and IIndexFile.findNames().
Edit: Additional Tips
1) How to differentiate between function declarations and simple declarations.
I can think of two ways:
Syntactically, based on the structure of the AST. For function definitions, the type of the declaration node will be IASTFunctionDefintion. For variable declarations, it will be IASTSimpleDeclaration, with the decl-specifier being IASTSimpleDeclSpecifier or IASTNamedTypeSpecifier (you additionally want to check that the declarator is not an IASTFunctionDeclarator, to filter out function declarations that are not definitions).
Semantically. If you find the IASTName for the declaration, you can call IASTName.resolveBinding(), and check whether the returned binding is an IFunction or an IVariable.
2) How to get the return type of a function and the variable type?
For these tasks, you need to get the binding. A variable's type can be queried by IVariable.getType(), and a function's return type via IFunction.getType().getReturnType().
3) Is there a way to get an ICElement from an IASTSimpleDeclaration?
There isn't a simple way that I know of. However, you shouldn't need to - if you're traversing the AST, all the information you could want can be found in the AST.

How to reset a NetLogo model with an extension?

I'm working on a NetLogo model that has a great deal of information stored in lookup tables and embedded in patches that affect agent behavior. As a result, I'm creating a model "reset" from an extension that clears the turtles, and resets the timer and ticks.
I would also like to have the extension reset specific globals that track model results, but not reset all of the globals that are constants (look up tables of precomputed information). I would also like to reset all the interface plots.
Any thoughts on how to go about this?
I apologize for no code to show, as is the case I can't show what is not working because I'm not even sure what to try!
NetLogo engine internals aren't really documented except in a general way at https://github.com/NetLogo/NetLogo/wiki/Engine-architecture, so if your extension wants to manipulate engine stuff directly, you'll usually have to consult the NetLogo source code to learn specifics of how it's done.
You want something like clear-globals, so check out https://github.com/NetLogo/NetLogo/blob/5.x/src/main/org/nlogo/prim/etc/_clearglobals.scala and https://github.com/NetLogo/NetLogo/blob/37cc1a0aa371c11e89f5b39b5143ed5d951e6081/src/main/org/nlogo/agent/World.java#L849-L866.
You want something like clear-all-plots, so check out https://github.com/NetLogo/NetLogo/blob/37cc1a0aa371c11e89f5b39b5143ed5d951e6081/src/main/org/nlogo/prim/plot/primitives.scala#L45-L50 and https://github.com/NetLogo/NetLogo/blob/37cc1a0aa371c11e89f5b39b5143ed5d951e6081/src/main/org/nlogo/plot/PlotManager.scala#L54-L57.
And so on.
EDIT:
For access to the PlotManager, note that plot/primitives.scala has:
workspace.plotManager.asInstanceOf[PlotManager]
in Java code on an extension, from the workspace plotManager() gets you the PlotManager, except the return type of that method is Object (yes, I know, it's grungy and horrible), so you need to insert a typecast to org.nlogo.plot.PlotManager and you're good to go. PlotManager has methods for getting to particular plots.

Can I work around name conflicts?

I have a fortran project whith some name conflicts (from doxygen's point of view). Sometimes a local variable in a procedure may have the same name as a subroutine or function. For compilation/linking there are no problems, as the different definitions live separate lives, for instance:
progA/main.f defines and uses the variable delta.
libB/delta.f defines a function named delta.
progB/main.f uses the function delta defined in libB.
progB is linked with libB, progA is not linked with libB.
In this case, when generating call/caller graphs, or linked source code, the variable delta in progA/main.f will be identified as the function delta. Is there some combination of doxygen settings I can use to inform it that progA is not supposed to use definitions in libB, or something similar?
Another issue is that I may have functions/subroutines with the same name in different subdirectories. Again, as long as they are not linked together this does not represent a problem for compilation, but doxygen cannot identify which of them is meant in links, calls, etc. Is there some work around this (without renaming procedures, that is)?

What is the closest thing MATLAB has to namespaces?

We have a lot of MATLAB code in my lab. The problem is there's really no way to organize it. Since all the functions have to be in the same folder to be called (or you have to add a bunch of folders to MATLAB's path environment variable), it seems that we're doomed have loads of files in the same folder, all in the global namespace. Is there a better way to organize our files and functions? I really wish there were some sort of module system...
MATLAB has a notion of packages which can be nested and include both classes and functions.
Just make a directory somewhere on your path with a + as the first character, like +mypkg. Then, if there is a class or function in that directory, it may be referred to as mypkg.mything. You can also import from a package using import mypkg.mysubpkg.*.
The one main gotcha about moving a bunch of functions into a package is that functions and classes do not automatically import the package they live in. This means that if you have a bunch of functions in different m-files that call each other, you may have to spend a while dropping imports in or qualifying function calls. Don't forget to put imports into subfunctions that call out as well. More info:
http://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html
I don't see the problem with having to add some folder to Matlab's search path. I have modified startup.m so that it recursively looks for directories in my Matlab startup directory, and adds them to the path (it also runs svn update on everything). This way, if I change the directory structure, Matlab is still going to see all the functions the next time I start it.
Otherwise, you can look into object-oriented code, where you store all the methods in a #objectName folder. However, this may lead to a lot of re-writing code that can be avoided by updating the path (there is even a button add with subfolders if you add the folder to the path from the File menu) and doing a bit of moving code.
EDIT
If you would like to organize your code so that some functions are only visible to the functions that call them directly (and if you don't want to re-write in OOP), you put the calling functions in a directory, and within this directory, you create a subdirectory called private. The functions in there will only be visible to the functions in the parent directory. This is very useful if you have to overload some built-in Matlab functions for a subset of your code.
Another way to organize & reuse code is using matlab's object-oriented features. Each Object is customarily in a folder that begins with an "#" and has the file(s) for that class inside. (though the newer syntax does not require this for a class defined in a single file.) Using private folders inside class folders, matlab even supports private class members. Matlab's new class notation is relatively fully-featured, but even the old syntax is useful.
BTW, my startup.m that examines a well-known location that I do my SVN checkouts into, and adds all of the subfolders onto my path automatically.
The package system is probably the best. I use the class system (#ClassName folder), but I actually write objects. If you're not doing that, it's silly just to write a bunch of static methods. One thing that can be helpful is to put all your matlab code into a folder that isn't on the matlab path. Then you can selectively add just the code you need to the path.
So say you have two projects, stored in "c:\matlabcode\foo" and "c"\matlabcode\bar", that both use common code stored in "c:\matlabcode\common," you might have a function "setupPaths.m" like this:
function setupPaths(projectName)
basedir = fullfile('c:', 'matlabcode');
addpath(genpath(fullfile(basedir, projectName)));
switch (projectName)
case {'foo', 'bar'}
addpath(genpath(fullfile(basedir, 'common')));
end
Of course you could extend this. An obvious extension would be to include a text file in each directory saying what other directories should be added to the path to use the functions in that directory.
Another useful thing if you share code is to set up a "user specific/LabMember" directory structure, where you have different lab members save code they are working on. That way you have access to their code if you need it, but don't get clobbered when they write a function with the same name as one of yours.