Search code examples
javareflectiondroolspojo

java: create runtime a pojo object from a properties file


Within a java project, I would like that my system to create runtime an object (POJO) from a configuration file and then use it in a drools-based system. Point to be noted here is that the pojo class too should be generated at runtime If I understand this is the only way to make controls on a data structure that does not have a fixed structure What is the best way to do this? It would be interesting to be able to cache the java class obtained so you do not have to create it all the time but only when the property file changes Can someone help me ? I'm a bit confused Thank you so much


Solution

  • You need to dynamically define your drl rule file. In that file you can define types. e.g.

    declare MyEvent
       MyType : String
       MyValue : double
    end
    

    So after you have created the drl file contents from your properties file see KieFileSystem for loading up the drl file from memory. This will create a POJO that you can then access using kieContainer.getKieBase().getFactType to get the type and then you can create an instance and set properties on it.