Now the function can look something like this: Agent x=null this is a local variable that you can freely use when you create population of objects This function takes as an argument the index of the source of that population of sources. for this you need to make a function on the new agent parameter that returns an Agent Now, you want to generate a different agent type from each of the sources in the array. On this example i have an array of 2 sources. also im not sure i actually understand what you want exactlyįirst, the source can be created as a population, with each member of the population creating a different agent type. I'm not sure if this helps, but it's what I think can lead you to the right answer. Note that in the above image the code in the Default Value field would actually be changed in the enclosing agent to reflect the correct automatically create method. In this case the population is called 'programmaticAgents' and therefore the method is called 'add_programmaticAgents(.'. This Supplier is then provided with a reference to 'add_' method which is automatically generate by AnyLogic when an agent population object is created. Here the block that will be creating new agents has a dynamic parameter called agentSupplier (see image) of type and calls agentSupplier.get() when new instance of an agent is needed. now you can do whatever with the agent, in this case send itĮrror("Could not instantiate %s, see Console for full error", classNameValue) tell AnyLogic that agent is created and started ((AgentLinkedHashSet)getDefaultPopulation())._add(agent) ĪtParameter("parameterOne", 5.0, false) ĪtParameter("parameterTwo", "some value", false) Īgent.markParametersAreSet() // <- mark that parameters are set (Agent)c.newInstance(getEngine(), this, getDefaultPopulation()) but in actuality it is of type specified in 'classNameValue' above at compile time it has to be declared as 'Agent' here get a handle on the Class of that agentĬlass agentClass = Class.forName(classNameValue) Ĭonstructor c = agentClass.getDeclaredConstructor( String classNameValue = "testprogcreation.ProgrammaticAgent" (in this case 'testprogcreation.' but you'll have your own) NOTE: you need the whole name including package name This is the name of the class that needs to be created The code is as follows, please read the comments for more detailed description. In the example Main agent, the code is in the action property of the 'create directly' button. This is the least preferable method of the two. In this example there are 2 agents Main and ProgrammaticAgent. alp can be found here for the next 2 hrs. Please note that the drawback of both methods is that since the type of the resulting agent is not known in advance the code refers to the new entity as just Agent however it actually creates the agent of correct type, which can be checked using 'instanceof' or cast.īoth versions are provided below. There are two ways to achieve this: via Java Reflection and using. Every project will have different agents, variables, names, parameters, etc. My Issue here is, that I try to make my block as customizable as possible, meaning that I want to use this block in future projects again without changing the code at all (or at least too much). I know that normally I have to use something like ' new Agent()' to create new agents, but I can't find a link between the parameter-value and the agent-type. Obviously, putting just the name of the parameter in the new agent field didn't work (it does work, but only for the first agent - after that I get an error, since the same agent gets created). I want to create agents dynamically depending on the selected parameter (type Agent). Now I want to create the other agent with a source block and the inject function. So far, I was able to verify the incoming agent to make sure, that the right agent-type was selected in Main. What my block does is, it creates new agents depending on the agents, that enter the block (kinda like the batch-block). Two of them are of the type ' Agent' and in Main I selected the corresponding agents. I created my own Flowchart block with a few parameters.
0 Comments
Leave a Reply. |