Using Jason with Spring Boot #123
Replies: 2 comments 2 replies
-
Hi, thanks for sharing! I am not used to Spring.... so what kind of functionality is being added? (or what we can do with that proposal?) For getting the references of the instance of Agent class, usually, from the AgArch, you can do .getTS().getAg(). |
Beta Was this translation helpful? Give feedback.
-
I've extended this now to work for the AgentArch class as well as BeliefBase implementations. For this most part this was a straight-forward extension of what I did earlier, but the BeliefBase bit did require a little extra trickery because of some details of how the BB creation process works. The bulk of the processing is still just this kind of stuff, which we saw above:
But unfortunately this fails if you stop here, assuming the custom BeliefBase impl has some service injected into it and uses that service in the add() method (which seems like a reasonable assumption - the thing you'd be injecting would likely be a Spring CrudRepository, or JDBCTemplate, etc). That's because inside the Agent class, when after the BB is instantiated, initialBeliefs are add()'d to the BB immediately, before the create method returns. This means that before the AutowireCapableBeanFactory is even called to do the injection, we've already blown up with a NullPointerException. Blimey. 👎 Fortunately the fix isn't too bad. I mean, yeah, it's a little bit ugly, but not a completely brutal hack. Briefly, you just need to create a static holder for the ApplicationContext with a public getter, and then in the constructor of the BeliefBase class, you retrieve the ApplicationContext, and use it to inject into the current object. Something like this works:
and the "holder" class is just:
Source code can be found here. So at this point I've achieved my goal of allowing for injection into Environment, AgentArch, Agent, and BeliefBase classes. I'll use this for some of my own work for sure. And if others find it useful, I'm happy to either make a standalone library that can be added to a Java project to support this, and/or work with the Jason maintainers to incorporate it into Jason itself. And again, I'll work up some samples later to illustrate why this is even useful. These little contrived test scenarios I've been using don't really communicate anything much beyond "Yes, this is possible." :-) |
Beta Was this translation helpful? Give feedback.
-
Team:
I've been doing some work on using Jason within a Spring Boot application, with a goal of being able to inject Spring managed components into things like Agents, AgentArch's, Environments, and (maybe) BeliefBases.
So far, I have injection into Agent classes and Environment classes working. And (thankfully) accomplishing this required no modifications to any Jason code. I just added a new class that's basically a copy of RunLocalMAS and use the Spring AutowireCapableBeanFactory to inject into the various instances. For example, the Environment part looks like this:
Unfortunately doing Agents was a little bit harder, since it doesn't seem to be easy to get your hand on the Agent class instance where the Agents are created. I did have to subclass LocalAgArch and add a new method that is identical to createArchs except for making the return type "Agent" and returning the Agent class there. From that point, I was able to use my new method and the same pattern as above:
Anyway, that's working now and my goal is to be able to extend this to support the AgentArch classes and BeliefBase classes. It would be really handy, I think, to have the ability to easily use things like Spring Data JPA in a BeliefBase for persistence, etc.
Once I get all of this working (assuming I do) and clean this code up a bit I'll put the entire source up in my repo and share with everybody. And in the off chance there is any appetite for including something like this in Jason, I'm happy to contribute my work if it will be of use to anybody.
EDIT: I went ahead and uploaded the code in its current state, just in case anyone is interested in looking at this. The bulk of the work is in the RunSpringMAS.java file.
Beta Was this translation helpful? Give feedback.
All reactions