Skip to content

Commit

Permalink
Utvide RuleService getSpecification med argument (#119)
Browse files Browse the repository at this point in the history
* Utvide RuleService getSpecification med argument

* Depreker DRS avdepreker scope/sporing

* Mer tid til å fjerne

* Redusere omfang

* Fjerne hackertest

* Kosmetiske forbedringer
  • Loading branch information
jolarsen authored Sep 13, 2022
1 parent c66e663 commit 538118c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/main/java/no/nav/fpsak/nare/DynamicRuleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import no.nav.fpsak.nare.evaluation.Evaluation;

@Deprecated // Enten konstruer en liste av specs selv eller bruk en variant med ForeachSpecification
public abstract class DynamicRuleService<T> implements RuleService<T> {

protected T regelmodell;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/no/nav/fpsak/nare/Ruleset.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public SequenceSpecification.Builder<V> sekvensRegel(String id, String beskrivel
return SequenceSpecification.<V>regel(id, beskrivelse);
}

public ComputationalIfSpecification.Builder<V> sekvensHvisRegel() {
return ComputationalIfSpecification.<V>regel();
public ComputationalIfSpecification.Builder<V> dersomRegel(Specification<V> ifSpec) {
return ComputationalIfSpecification.<V>regel(ifSpec);
}

/**
Expand Down Expand Up @@ -193,7 +193,7 @@ public Specification<V> beregningsForeachThenRegel(String id, String beskrivelse
* @param spec2 utføres 1 gang etter at spec1 er utført N ganger
* @return
*/
@Deprecated // Bruk beregningForeach eller beregningForeachThen
@Deprecated // Konsturer en liste selv og bruk beregningsRegel(spec1list, spec2) eller Bruk beregningForeach eller beregningForeachThen
public Specification<V> beregningsRegel(String id, String beskrivelse, Class<? extends DynamicRuleService<V>> spec1,
V regelmodell, String argumentBeskrivelse, List<? extends Object> args1, Specification<V> spec2) {
Objects.requireNonNull(spec1, "spec1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,22 @@
*/
public class ComputationalIfSpecification<T> extends AbstractSpecification<T> {

public static <V> Builder<V> regel() {
return new Builder<>();
public static <V> Builder<V> regel(Specification<V> ifSpec) {
return new Builder<>(ifSpec);
}

public static class Builder<T> {
private Specification<T> testSpec;
private Specification<T> ifTrueSpec;
private Specification<T> ifFalseSpec;

public Builder() {
}

public Builder<T> hvis(Specification<T> ifSpec, Specification<T> thenSpec) {
public Builder(Specification<T> ifSpec) {
this.testSpec = ifSpec;
this.ifTrueSpec = thenSpec;
return this;
}

public ComputationalIfSpecification<T> hvisEllersJa(Specification<T> ifSpec, Specification<T> thenSpec) {
this.testSpec = ifSpec;
public Builder<T> hvisja(Specification<T> thenSpec) {
this.ifTrueSpec = thenSpec;
return this.build();
return this;
}

public ComputationalIfSpecification<T> ellers(Specification<T> specification) {
Expand All @@ -58,7 +52,7 @@ public ComputationalIfSpecification<T> build() {
private Specification<T> ifTrueSpec;
private Specification<T> ifFalseSpec;
private Evaluation testEvaluation;
private ServiceArgument scope;
private ServiceArgument property;


public ComputationalIfSpecification(final Specification<T> testSpec, final Specification<T> doSpec) {
Expand Down Expand Up @@ -90,22 +84,22 @@ public Evaluation evaluate(final T t) {
testEvaluation = testSpec.evaluate(t);
var conditionalEvaluation = Resultat.JA.equals(testEvaluation.result()) ? ifTrueSpec.evaluate(t) : doEvaluateIfFalse(t, null);
var evaluation = new ComputationalIfEvaluation(identifikator(), beskrivelse(), testEvaluation, conditionalEvaluation);
if (scope != null) {
evaluation.setEvaluationProperty(scope.getBeskrivelse(), scope.getVerdi().toString());
if (property != null) {
evaluation.setEvaluationProperty(property.getBeskrivelse(), property.getVerdi().toString());
}
return evaluation;
}

@Override
public Evaluation evaluate(final T t, ServiceArgument serviceArgument) {
if (serviceArgument == null) {
throw new IllegalArgumentException("Utviklerfeil: Førsøker evaluere ComputationalIf med argument null");
throw new IllegalArgumentException("Utviklerfeil: Forsøker evaluere ComputationalIf med argument null");
}
testEvaluation = testSpec.evaluate(t, serviceArgument);
var conditionalEvaluation = Resultat.JA.equals(testEvaluation.result()) ? ifTrueSpec.evaluate(t, serviceArgument) : doEvaluateIfFalse(t, serviceArgument);
var evaluation = new ComputationalIfEvaluation(identifikator(), beskrivelse(), testEvaluation, conditionalEvaluation);
if (scope != null) {
evaluation.setEvaluationProperty(scope.getBeskrivelse(), scope.getVerdi().toString());
if (property != null) {
evaluation.setEvaluationProperty(property.getBeskrivelse(), property.getVerdi().toString());
}
evaluation.setEvaluationProperty(serviceArgument.getBeskrivelse(), serviceArgument.getVerdi().toString());
return evaluation;
Expand Down Expand Up @@ -135,7 +129,13 @@ public RuleDescription ruleDescription() {

@Override
public Specification<T> medScope(ServiceArgument scope) {
this.scope = scope;
this.property = scope;
return this;
}

@Override
public Specification<T> medEvaluationProperty(ServiceArgument property) {
this.property = property;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public SequenceSpecification<T> build() {
}
}

private ServiceArgument scope;
private ServiceArgument property;

private final List<Specification<T>> specs = new ArrayList<>();

Expand Down Expand Up @@ -118,8 +118,8 @@ public String beskrivelse() {
@Override
public Evaluation evaluate(final T t) {
var evaluation = doEvaluate(t, null);
if (scope != null) {
evaluation.setEvaluationProperty(scope.getBeskrivelse(), scope.getVerdi().toString());
if (property != null) {
evaluation.setEvaluationProperty(property.getBeskrivelse(), property.getVerdi().toString());
}
return evaluation;
}
Expand All @@ -130,8 +130,8 @@ public Evaluation evaluate(final T t, ServiceArgument serviceArgument) {
throw new IllegalArgumentException("Utviklerfeil: Førsøker evaluere Sequence med argument null");
}
var evaluation = doEvaluate(t, serviceArgument);
if (scope != null) {
evaluation.setEvaluationProperty(scope.getBeskrivelse(), scope.getVerdi().toString());
if (property != null) {
evaluation.setEvaluationProperty(property.getBeskrivelse(), property.getVerdi().toString());
}
evaluation.setEvaluationProperty(serviceArgument.getBeskrivelse(), serviceArgument.getVerdi().toString());
return evaluation;
Expand Down Expand Up @@ -163,7 +163,13 @@ public RuleDescription ruleDescription() {

@Override
public Specification<T> medScope(ServiceArgument scope) {
this.scope = scope;
this.property = scope;
return this;
}

@Override
public Specification<T> medEvaluationProperty(ServiceArgument property) {
this.property = property;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ default Evaluation evaluate(T t, ServiceArgument serviceArgument) {
Specification<T> medID(String id);

@SuppressWarnings("unused")
@Deprecated // Bruk beregningForeach eller beregningForeachThen
@Deprecated // Bruk medEvaluationProperty - evt beregningForeach eller beregningForeachThen
default Specification<T> medScope(ServiceArgument scope) {
return this;
}

/**
* Setter en evaluationproperty basert på beskrivelse og objekt i ServiceArgument.
* Er implementert av enkelte aggregate-specifications for å sette en property for samleevalueringen.
*/
default Specification<T> medEvaluationProperty(ServiceArgument property) {
return this;
}

/**
* Create a new specification that is the AND operation of {@code this} specification of another specification.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ public Specification<MellomregnInt> getSpecification() {
var regel = rs.sekvensRegel()
.neste(new AddOneLeaf())
.forAlle("bokstav", List.of("A", "B", "C"), new AddOneLeaf())
//.hvisEllers(new EvenLeaf(), new MultiplyByTwoLeaf(), new AddOneLeaf())
.neste(rs.hvisRegel().hvis(new EvenLeaf(), new MultiplyByTwoLeaf()).ellers(new AddOneLeaf()))
.hvisEllers(new EvenLeaf(), new MultiplyByTwoLeaf(), new AddOneLeaf())
// Alternative måter med hhv condOr og compIf
//.neste(rs.hvisRegel().hvis(new EvenLeaf(), new MultiplyByTwoLeaf()).ellers(new AddOneLeaf()))
//.neste(rs.dersomRegel(new EvenLeaf()).hvisja(new MultiplyByTwoLeaf()).ellers(new AddOneLeaf()))
.neste(new MultiplyByTwoLeaf())
.siste(new SinkLeaf());
return regel;//rs.regel("Seq", "Seq", regel);
Expand Down

0 comments on commit 538118c

Please sign in to comment.