ObjectValidation

I think we can easy add a validation framework to drools. So that facts are validated on assertion, invalid facts are auto retracted. First we need to add support for sub accessors - we can make a compromise here and only allow access to immutables and primitives, then we don't have to  worry about chaning values - this way we can add it to the existing engine, without many changes.

 

So with the sub accessors now allowed we could write a validation system as below. This checks that the name is less than 15 chars and that the location is in a valid location. The beauty of this is that it will leverage existing indexes, so it can validate the location very quickly.

 

rule "Validate Person"
    when
        $person  : Person(name.length < 15, $location :location)
        Location(country  =="UK", name == $location )    
    then
        invalid.remove($person)
end

 

So any asserted facts go into an invalid list, they are only removed when they valid thus matching the rule that. However this is quite verbose, what  we need is a special validation dsl. This validation dsl would be essential for expressing ontology validations:

 

validate Person
            name.length < 15
            location  == Location( countr == "UK" ).name

 

So here I have added a  new type  of constraint - "location  == Location( countr == "UK" ).name" standard field constraints go inside the Location pattern, we allow t he .name to  show that it is joinedwith the location field. further we could allow bound variables to also be used in the Location pattern, I  think it would  be  a good idea to auto bind variables in this, so that variabels are automatically available for each field - probably done using a naming convention of just adding a $.

 

You could even take this a step further and have the validation information embedded in the java code as attributes.