Giter Site home page Giter Site logo

Comments (4)

typhoon1978 avatar typhoon1978 commented on May 20, 2024 1

Hi Clayton,

sorry my sample is wrong, because I deleted the real bean name and put at last a generic MyBean.

My real class is declared as follows:

public class AutoEvaluationRuleBook extends CoRRuleBook<AutoEvaluationResult> {
	
	private static final String DATE_PATTERN = "(\\d{4})-(\\d{2})-(\\d{2})";

	private static final int YEAR_GROUP = 1;

	@Override
	public void defineRules() {

		addRule(RuleBuilder.create().withFactType(CalculatedMatching.class).withResultType(AutoEvaluationResult.class)
				.when(personTypeMismatch()).then(unevaluated()).stop().build());
  // and so on...

          }
}

I preferred using the bean approach rather than the map approach.

Thanks and best regards,
Alessandro.

from rulebook.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

You are correct. It looks like that is a bug that was probably introduced in v0.5. I just put the fix in 0.5.1-SNAPSHOT. I used the same basic approach as you. The only difference was I checked for null in the first statement.Why? Because another contributor pointed out that it's not considered a good use of Optional just to check isPresent() on an existing variable - not really a big difference tho, IMO.

Thanks for that! I'll update the tests to catch that condition and push it out as a release in the next couple of days. But for now, it's in the current SNAPSHOT release and in the develop branch.

from rulebook.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

BTW, I can see that you create a RuleBook with the a result type of MyBean here.

public class AutoEvaluationRuleBook extends CoRRuleBook<MyBean> {

	@Override
	public void defineRules() {
           // here I define all my business rules
	}
}

But then you try to instantiate that RuleBook using a different result type of AutoEvaluationResult here.

RuleBook<AutoEvaluationResult> autoEvaluationRuleBook = RuleBookBuilder.create(AutoEvaluationRuleBook.class)
				.withResultType(AutoEvaluationResult.class)
				.withDefaultResult(new AutoEvaluationResult(null, false, false, null))
				.build();

I'm not 100% sure why. But here's an example of what I think you are trying to do. And once again, thanks for finding that bug!

/**
 * Custom RuleBook with a MyBean result type.
 */
public class AutoEvaluationRuleBook extends CoRRuleBook<MyBean> {
  @Override
  public void defineRules() {
    addRule(RuleBuilder.create().withFactType(String.class)
        .withResultType(MyBean.class) //should match the result type of the RuleBook, which is MyBean
        .when(facts -> facts.containsKey("value1"))
        .using("value1")
        .then((facts, result) -> result.getValue().setStrValue1(facts.getOne()))
        .build()
    );
    addRule(RuleBuilder.create().withFactType(String.class)
        .withResultType(MyBean.class) //should match the result type of the RuleBook, which is MyBean
        .when(facts -> facts.containsKey("value2"))
        .using("value2")
        .then((facts, result) -> result.getValue().setStrValue2(facts.getOne()))
        .build()
    );
  }
}
package com.deliveredtechnologies.rulebook.example;

public class MyBean {
  private String strValue1;
  private String strValue2;

  public String getStrValue1() {
    return strValue1;
  }

  public void setStrValue1(String strValue1) {
    this.strValue1 = strValue1;
  }

  public String getStrValue2() {
    return strValue2;
  }

  public void setStrValue2(String strValue2) {
    this.strValue2 = strValue2;
  }
}
public class Application {
  public static void main(String args[]) {
    AutoEvaluationRuleBook ruleBook = new AutoEvaluationRuleBook();
    ruleBook.setDefaultResult(new MyBean());

    NameValueReferableMap factMap = new FactMap();
    factMap.setValue("value1", "First Value");
    factMap.setValue("value2", "Second Value");

    ruleBook.run(factMap);

    ruleBook.getResult().ifPresent(result ->
    {
      MyBean bean = result.getValue();
      System.out.println(bean.getStrValue1());
      System.out.println(bean.getStrValue2());
    });
  }
}

from rulebook.

Clayton7510 avatar Clayton7510 commented on May 20, 2024

I updated the tests and just pushed 0.5.1 to Maven with this bug fix.

from rulebook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.