We are proud to announce that the version 2.2.0 of MRules is released.
This version comes with some new features, among which:
Local Java Class or Package imports are now handled in rule sets, to ease the use of Java objects.
Static access for Fields and Methods have been added to the property framework.
The DSL engine has been enrich with new possibilities.
The Rule Set configuration DSL handles all these new features, and more (like raw string litterals, rule set properties configuration, Classes and Packages auto completion, …)
Lots of improvements and optimizations have been made to already existing features. Also, the DSL editor and specifically the autocompletion feature has been improved to be more accurate and intuitive.
Please note the breaking changes in the list of modifications:
AdvancedReader has been moved to another package: if you use it directly, you will have to update the code.
An order is now mandatory in ruleset DSL configuration file: imports, then configuration and then rules. You may have to update your rule set configurations.
We are proud to announce that the version 2.1.0 of MRules is released.
The changes related to this release focus mainly on our functional grammar engine. Many optimizations and new possibilities have been brought. Also, the autocompletion feature has been improved to be more accurate and intuitive.
Some optimizations and new features have also been added to the rules engine.
We are proud to announce that the version 1.10.0 of MRules is released.
This version brings lots of new features and improvements. Amongst these, a breaking change on average computations should be noted and taken into account if you are using this functionality. A dedicated post has been written to describe changes.
If you want to know what’s included in version 1.10.0, the release notes provide details on all modifications.
Also, version 1.10.0 will be the last major 1.x release. It paves the way for the version 2.0.0, which is planned to be released at the beginning of the year 2018.
This does not mean that versions 1.x will not be supported. Minor fix versions will be built if necessary. But our efforts are now concentrated on the development of the version 2.0.0, which will include lots of very exciting changes, one of which being a new way of configuring rulesets, by writing rules “as you talk”, with a fully functional language.
This article will provide you with details on how to use MRules built-in layer allowing reading and writing data from or to Java beans.
This layer can be used in a standalone way, it’s not coupled to the rule engine itself.
If you are familiar with anyone of the popular Java frameworks using reflection to access data layer, you won’t be surprised by the syntax, even if it’s slightly different sometimes because it offers more possibilities.
How it works
The property to access is represented by a String. For example: “myProperty.myNestedProperty“.
This String must be processed, meaning parsed and transformed to a specific Object. We call this step “compiling” the property. This is the role of the IPropertyCompiler utility.
When the String representation of the property is compiled, you obtain an instance of a class implementing the interface IProperty. This resulting instance is immutable and might be used several times, in a multi-threaded environment, to get or set the targeted property.
* Shows how to get and set a property from a Java Bean.
property.set(newPropertySource(this),"Hello World !");
Assert.assertEquals("Hello World !",this.getMyProperty());
What is possible?
The property access framework shipped with MRules allows to perform all of the following operations:
Read an unlimited number of nested properties in an Objects hierarchy.
Read Arrays elements, whatever is the number of dimensions of the array. The index of the element to read might be a constant or a value read from another property.
Lists are handled in the same way as Arrays.
Read Maps values, given the key. The key of the value to read might be a constant or a value read from another property.
Read instance fields without getter.
Invoke methods with or without arguments. The arguments might be constants or values read from another properties.
Read static fields.
Invoke static methods with or without arguments.
Combine all the above, whatever is the protection of the element to access (even if private).
Extra: Arrays length can be accessed with the “length” keyword used as if it were a direct field access.
Write any property of a bean in an Objects hierarchy, with no limitation of level.
Write an element of an Array. The Array will be automatically adjusted if too small.
Lists are handled in the same way as Arrays.
Set Maps values, given the key.
Write an instance field, even if private and / or final.
Write a static field, even if private. Final static fields can not be written.
Indexed and mapped getters / setters:
Reading or Writing Arrays and Lists might be achieved in two ways:
A getter / setter which returns / sets directly the Array or List.
An indexed getter / setter, with an integer argument, returning or writing the corresponding element.
The same feature is available for Maps access, with an extra argument corresponding to the key.
This feature allows complying with Apache Common Beanutils possibilities, but we advise to limit its usage for when it’s absolutely necessary and to prefer the standard getters / setters.
Thanks to the two-phase life cycle of the IProperty beans and to a very optimized code, our framework offers impressive performances for data access: up to 40% faster than the Apache common bean utils.
Moreover, an extension allowing generating bytecode at runtime avoid the usage of reflection for public member accesses, and further improves the execution speed.
Indexed and mapped getters / setters: only one dimension or level.
Static fields could not be directly accessed until version 2.2.0.
Static methods could not be directly accessed until version 2.2.0.
Methods with more than one argument could not be directly accessed until version 1.10.0.
Reading an object hierarchy is as simple as separating all properties names to traverse with a dot.
For example: “myProperty.myNestedProperty.deeperProperty.alwaysDeeper.leafValue”
Accessing Maps values id done by using parenthesis. For example: “myMap('My Key')” or “myMap("My Key")“.
If the key value comes from another sub-property, just write “myMap(myBean.myKey)“.
Another way to access Mapped properties is to use the same syntax as basic access. For example : “myMap.myKey” will work fine. If the “myMap” property value is not an extension of Map with a “getMyKey()” method, then the framework will intent to use the string “myKey” as the key.
This second way has some disadvantages, so we advise to use the parenthesis syntax :
Keys are not limited to Strings
Key values can be read from other properties.
For Lists and Arrays, the syntax is the same. It’s also the same if the Java Bean model used indexes getters / setters or not.
Accessing an Array or List element is achieved by : “myArray['2']” or “myArray["2"]“.
Note: The syntax “myArray” is accepted here, as an integer cannot be interpreted as a sub-property.
If the index value comes from another sub-property, just write “myArray[myBean.myIntegerProperty]“.
MRules allows directly accessing fields or invoking methods. The syntax to achieve this operation uses the exclamation mark as a delimiter.
The field name or the method invocation instruction (name + parenthesis + argument) start and end with an exclamation mark.
Method: “!myMethod()!” or “!myMethod('My Argument")!” or “!myMethod(myBean.myArgument)!“.
The last exclamation mark might be omitted when it’s obvious (the dot implies a closing exclamation mark), but take care that it does not change the meaning of the property !
Field: “!myField” is OK.
Method: “!myMethod()” is OK.
This one cannot be omitted : “!myMapField!("My Key")“.
Extra : the length of an array is accessed using the “!length!” field access: “myArray.!length“.
All possibilities above can be mixed, and allow to adapt to most of the Java Beans models.
Some detailed examples:
Accessing a bean, then Map, for which the key comes from the second element of an List: “myBean.myMap(myList)“.
Accessing a multi-dimensionnal array: “myArray[myBean.myIntegerProperty][!myMethod()]“.
Retrieving the size of a List: “myBean.myList.!size()“.
And even using it to access the element of another list: “myBean.myMainList[myBean.myList.!size()].myListElementProperty“.
Almost everything is possible, you are only limited by the complexity of your data model!
Apache Commons connector
You might work with an existing application, making intensive use of Java common bean utils. And you might want to improve speed and user experience.
We developed a connector just for you, allowing to replace part of the common bean utils code with ours. This allows making the legacy code based on bean utils using MRules data access framework, without refactoring.
Just add MRules framework in your classpath, and execute on unique instruction at startup:
An issue regarding automatic conversion while searching in collections has be filled by a user. This problem only applies on cases when the type of the objects contained in the collection is not directly available via generics. Version 1.9.1 corrects this issue and goes further by improving type guessing and automatic conversions in these cases.
Please update your dependencies to stay up-to-date.