deliveredtechnologies / rulebook

100% Java, Lambda Enabled, Lightweight Rules Engine with a Simple and Intuitive DSL
http://www.deliveredtechnologies.com
Apache License 2.0
716 stars 124 forks source link

Add the Supplier function to avoid the passage by reference for the default result #194

Closed awattez closed 3 years ago

awattez commented 4 years ago

Hello, this is an improvement in the operation of the default result. Indeed I carried out a test to detail my remarks.

The default result works well for primitive types, however for complex object, it cannot work properly.

The reset function does not reset to the default state. It uses the referenced value. And therefore from one execution to another the result may not be reset correctly.

The improvement consists in passing in parameter of the default result a supplier function which will be executed at each reset which allows to correctly define the default result without alteration of it. The reference will change each time the reset function is called. "It's a real reset"

https://github.com/deliveredtechnologies/rulebook/pull/193

  @Test
  public void getResultFailedWithDefaultValueSetToAnObject() {
    ComplexObject complexObject = new ComplexObject();

    Result<ComplexObject> result = new Result<>(complexObject);
    result.getValue()._choices.add("Bad choices");
    complexObject._value = -1.0;
    Assert.assertSame(result.getValue(), complexObject);

    result.reset();
    // Still same !
    Assert.assertSame(result.getValue(), complexObject);
  }

  @Test
  public void getResultWithSupplierAndDefaultValueSetToAnObject() {
    Result<ComplexObject> result = new Result<>(ComplexObject::new);
    result.getValue()._choices.add("Bad choices");

    ComplexObject complexObject = result.getValue();

    result.reset();
    // Not same !
    Assert.assertNotSame(result.getValue(), complexObject);
  }
awattez commented 4 years ago

Update ?

Clayton7510 commented 3 years ago

Thanks for the suggestion. I like it!