diff --git a/src/main/java/no/nav/fpsak/nare/DynamicRuleService.java b/src/main/java/no/nav/fpsak/nare/DynamicRuleService.java deleted file mode 100644 index 39b71a8..0000000 --- a/src/main/java/no/nav/fpsak/nare/DynamicRuleService.java +++ /dev/null @@ -1,42 +0,0 @@ -package no.nav.fpsak.nare; - -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 implements RuleService { - - protected T regelmodell; - private ServiceArgument serviceArgument; - - protected DynamicRuleService() { - super(); - } - - public DynamicRuleService(T regelmodell) { - this(); - this.regelmodell = regelmodell; - } - - public DynamicRuleService medServiceArgument(ServiceArgument serviceArgument) { - this.serviceArgument = serviceArgument; - return this; - } - - public ServiceArgument getServiceArgument() { - return serviceArgument; - } - - void setRegelmodell(T regelmodell) { - this.regelmodell = regelmodell; - } - - @Override - public final Evaluation evaluer(T regelmodell) { - setRegelmodell(regelmodell); - Evaluation evaluation = getSpecification().evaluate(regelmodell); - if (serviceArgument != null) { - evaluation.setEvaluationProperty(serviceArgument.getBeskrivelse(), serviceArgument.getVerdi()); - } - return evaluation; - } -} diff --git a/src/main/java/no/nav/fpsak/nare/Ruleset.java b/src/main/java/no/nav/fpsak/nare/Ruleset.java index 3719dcf..d9f038b 100644 --- a/src/main/java/no/nav/fpsak/nare/Ruleset.java +++ b/src/main/java/no/nav/fpsak/nare/Ruleset.java @@ -1,14 +1,11 @@ package no.nav.fpsak.nare; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Objects; import no.nav.fpsak.nare.specification.ComputationalIfSpecification; import no.nav.fpsak.nare.specification.ConditionalOrSpecification; -import no.nav.fpsak.nare.specification.ForeachSpecification; import no.nav.fpsak.nare.specification.SequenceSpecification; import no.nav.fpsak.nare.specification.Specification; @@ -116,118 +113,4 @@ public Specification beregningsRegel(String id, String beskrivelse, List(id, beskrivelse, specs); } - - /** - * Realiserer sekvens der en regel utføres N ganger - 1 gang for hvert element i en collection - * - * @param id - * @param beskrivelse - * @param spec regel som utføres 1 gang for hvert element i argumentlisten, med argName + arg som "scope" - * @param argName - * @param args argumentlisten, inneholder N argumenter - * @return - */ - public Specification beregningsForeachRegel(String id, String beskrivelse, Specification spec, - String argName, List args) { - Objects.requireNonNull(spec, "spec1"); - Objects.requireNonNull(args, "args1"); - if (args.isEmpty()) { - throw new IllegalArgumentException("Argumentlisten kan ikke være tom"); - } - return new ForeachSpecification<>(id, beskrivelse, spec, args, argName); - } - - /** - * Realiserer sekvens der en regel utføres N ganger - 1 gang for hvert element i en collection - * - * @param id - * @param beskrivelse - * @param spec regel som utføres 1 gang for hvert element i argumentlisten, med argName + arg som "scope" - * @param argName - * @param args argumentlisten, inneholder N argumenter - * @return - */ - public Specification beregningsForeachRegel(Specification spec, String argName, List args) { - Objects.requireNonNull(spec, "spec1"); - Objects.requireNonNull(args, "args1"); - if (args.isEmpty()) { - throw new IllegalArgumentException("Argumentlisten kan ikke være tom"); - } - return new ForeachSpecification<>(spec, args, argName); - } - - /** - * Realiserer sekvens der en regel utføres N ganger - 1 gang for hvert element i en collection - fulgt av annen regel - * - * @param id - * @param beskrivelse - * @param spec regel som utføres 1 gang for hvert element i argumentlisten, med argName + arg som "scope" - * @param argName - * @param args argumentlisten, inneholder N argumenter - * @param specThen regel som utføres 1 gang etter at spec er utført N ganger - * @return - */ - public Specification beregningsForeachThenRegel(String id, String beskrivelse, Specification spec, - String argName, List args, Specification specThen) { - Objects.requireNonNull(spec, "spec"); - Objects.requireNonNull(args, "args1"); - Objects.requireNonNull(specThen, "specThen"); - if (args.isEmpty()) { - throw new IllegalArgumentException("Argumentlisten kan ikke være tom"); - } - return new SequenceSpecification<>(id, beskrivelse, new ForeachSpecification<>(id, beskrivelse, spec, args, argName), specThen); - } - - /** - * Realiserer sekvens der en regel utføres N ganger, etterfulgt av en - * spesifikasjon som har betydning for videre flyt - * - * @param id - * @param beskrivelse - * @param spec1 regel som utføres 1 gang for hvert element i - * argumentlisten, med argumentbeskrivelse + - * argumentverdi som serviceargument - * @param regelmodell - * @param argumentBeskrivelse - * @param args1 argumentlisten, inneholder N argumenter - * @param spec2 utføres 1 gang etter at spec1 er utført N ganger - * @return - */ - @Deprecated // Konsturer en liste selv og bruk beregningsRegel(spec1list, spec2) eller Bruk beregningForeach eller beregningForeachThen - public Specification beregningsRegel(String id, String beskrivelse, Class> spec1, - V regelmodell, String argumentBeskrivelse, List args1, Specification spec2) { - Objects.requireNonNull(spec1, "spec1"); - Objects.requireNonNull(args1, "args1"); - Objects.requireNonNull(spec2, "spec2"); - if (args1.isEmpty()) { - throw new IllegalArgumentException("Argumentlisten kan ikke være tom"); - } - List> specs = new ArrayList<>(); - args1.forEach(arg -> { - DynamicRuleService ny; - try { - ny = opprettSpesifikasjon(spec1, regelmodell); - ny.medServiceArgument(new ServiceArgument(argumentBeskrivelse, arg)); - specs.add(ny.getSpecification()); - } catch (InstantiationException | IllegalAccessException e) { - throw new IllegalStateException("Utviklerfeil: DynamicRuleService mangler public default constructor", - e); - } - }); - specs.add(spec2); - return new SequenceSpecification<>(id, beskrivelse, specs); - } - - private DynamicRuleService opprettSpesifikasjon(Class> spec1, V regelmodell) - throws InstantiationException, IllegalAccessException { - - try { - DynamicRuleService ny = spec1.getDeclaredConstructor().newInstance(); - ny.setRegelmodell(regelmodell); - return ny; - } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { - throw new IllegalStateException("Utviklerfeil: DynamicRuleService mangler public default constructor", e); - } - - } } diff --git a/src/main/java/no/nav/fpsak/nare/evaluation/Operator.java b/src/main/java/no/nav/fpsak/nare/evaluation/Operator.java index 180af81..5c49f14 100644 --- a/src/main/java/no/nav/fpsak/nare/evaluation/Operator.java +++ b/src/main/java/no/nav/fpsak/nare/evaluation/Operator.java @@ -2,5 +2,5 @@ public enum Operator { - AND, OR, SINGLE, NOT, COND_OR, SEQUENCE, COMPUTATIONAL_IF, FOREACH + AND, OR, SINGLE, NOT, COND_OR, SEQUENCE, COMPUTATIONAL_IF } diff --git a/src/main/java/no/nav/fpsak/nare/evaluation/node/ForeachEvaluation.java b/src/main/java/no/nav/fpsak/nare/evaluation/node/ForeachEvaluation.java deleted file mode 100644 index 7d4991d..0000000 --- a/src/main/java/no/nav/fpsak/nare/evaluation/node/ForeachEvaluation.java +++ /dev/null @@ -1,24 +0,0 @@ -package no.nav.fpsak.nare.evaluation.node; - -import no.nav.fpsak.nare.evaluation.AggregatedEvaluation; -import no.nav.fpsak.nare.evaluation.Evaluation; -import no.nav.fpsak.nare.evaluation.Operator; -import no.nav.fpsak.nare.evaluation.Resultat; - -public class ForeachEvaluation extends AggregatedEvaluation { - - public ForeachEvaluation(String id, String ruleDescription, Evaluation... children) { - super(Operator.FOREACH, id, ruleDescription, children); - } - - @Override - public String reason() { - return "Utført " + lastChild().ruleIdentification(); - } - - @Override - public Resultat result() { - // Endre hvis man tillater andre outcomes fra loop - return lastChild().result(); - } -} diff --git a/src/main/java/no/nav/fpsak/nare/evaluation/summary/EvaluationSummary.java b/src/main/java/no/nav/fpsak/nare/evaluation/summary/EvaluationSummary.java index 122fdeb..8b5d07f 100644 --- a/src/main/java/no/nav/fpsak/nare/evaluation/summary/EvaluationSummary.java +++ b/src/main/java/no/nav/fpsak/nare/evaluation/summary/EvaluationSummary.java @@ -37,7 +37,7 @@ public boolean check(Operator op, Evaluation parent, Evaluation child) { } // special case, kun prosessere siste SingleEvaluation barn av SequenceEvaluation siden alle andre returnerer ja. - if (Operator.SEQUENCE.equals(parent.getOperator()) || Operator.FOREACH.equals(parent.getOperator())) { + if (Operator.SEQUENCE.equals(parent.getOperator())) { String childRuleId = child.ruleIdentification(); String lastChildOfParentRuleId = ((AggregatedEvaluation) parent).lastChild().ruleIdentification(); return (childRuleId.equals(lastChildOfParentRuleId)); diff --git a/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java b/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java index 6bc08f7..8188912 100644 --- a/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java +++ b/src/main/java/no/nav/fpsak/nare/specification/AbstractSpecification.java @@ -65,6 +65,11 @@ public SingleEvaluation beregnet(Map mellomresultater) { return resultat; } + /** For beregningsregel: beregnet med sluttresultat i outcome. */ + public SingleEvaluation endeligBeregnet(RuleReasonRef reasonKey, Object... reasonArgs) { + return ja(reasonKey, reasonArgs); + } + @Override public Specification medBeskrivelse(String beskrivelse) { this.beskrivelse = beskrivelse; diff --git a/src/main/java/no/nav/fpsak/nare/specification/ComputationalIfSpecification.java b/src/main/java/no/nav/fpsak/nare/specification/ComputationalIfSpecification.java index 3459c7a..a1d9e46 100644 --- a/src/main/java/no/nav/fpsak/nare/specification/ComputationalIfSpecification.java +++ b/src/main/java/no/nav/fpsak/nare/specification/ComputationalIfSpecification.java @@ -82,7 +82,7 @@ public String beskrivelse() { @Override public Evaluation evaluate(final T t) { testEvaluation = testSpec.evaluate(t); - var conditionalEvaluation = Resultat.JA.equals(testEvaluation.result()) ? ifTrueSpec.evaluate(t) : doEvaluateIfFalse(t, null); + var conditionalEvaluation = Resultat.JA.equals(testEvaluation.result()) ? ifTrueSpec.evaluate(t) : doEvaluateIfFalse(t); var evaluation = new ComputationalIfEvaluation(identifikator(), beskrivelse(), testEvaluation, conditionalEvaluation); if (property != null) { evaluation.setEvaluationProperty(property.getBeskrivelse(), property.getVerdi().toString()); @@ -90,27 +90,8 @@ public Evaluation evaluate(final T t) { return evaluation; } - @Override - public Evaluation evaluate(final T t, ServiceArgument serviceArgument) { - if (serviceArgument == 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 (property != null) { - evaluation.setEvaluationProperty(property.getBeskrivelse(), property.getVerdi().toString()); - } - evaluation.setEvaluationProperty(serviceArgument.getBeskrivelse(), serviceArgument.getVerdi().toString()); - return evaluation; - } - - private Evaluation doEvaluateIfFalse(final T t, ServiceArgument serviceArgument) { - if (ifFalseSpec != null) { - return serviceArgument != null ? ifFalseSpec.evaluate(t, serviceArgument) : ifFalseSpec.evaluate(t); - } else { - return ja(); - } + private Evaluation doEvaluateIfFalse(final T t) { + return ifFalseSpec != null ? ifFalseSpec.evaluate(t) : ja(); } @Override @@ -127,12 +108,6 @@ public RuleDescription ruleDescription() { } } - @Override - public Specification medScope(ServiceArgument scope) { - this.property = scope; - return this; - } - @Override public Specification medEvaluationProperty(ServiceArgument property) { this.property = property; diff --git a/src/main/java/no/nav/fpsak/nare/specification/ForeachSpecification.java b/src/main/java/no/nav/fpsak/nare/specification/ForeachSpecification.java deleted file mode 100644 index 0985431..0000000 --- a/src/main/java/no/nav/fpsak/nare/specification/ForeachSpecification.java +++ /dev/null @@ -1,74 +0,0 @@ -package no.nav.fpsak.nare.specification; - -import java.util.List; -import java.util.Objects; - -import no.nav.fpsak.nare.ServiceArgument; -import no.nav.fpsak.nare.doc.RuleDescription; -import no.nav.fpsak.nare.evaluation.Evaluation; -import no.nav.fpsak.nare.evaluation.Operator; -import no.nav.fpsak.nare.evaluation.node.ForeachEvaluation; - -/** - * FOREACH specification, evaluate a specification for each member of a Collection, setting medScope to the current member - */ -public class ForeachSpecification extends AbstractSpecification { - - private Specification spec; - private List args; - private String argName; - - public ForeachSpecification(Specification spec, List args, String argName) { - super(); - Objects.requireNonNull(spec, "spec"); - Objects.requireNonNull(args, "args"); - Objects.requireNonNull(argName, "argName"); - this.spec = spec; - this.args = args; - this.argName = argName; - } - - public ForeachSpecification(String id, String beskrivelse, Specification spec, List args, String argName) { - this(spec, args, argName); - Objects.requireNonNull(id, "ID"); - Objects.requireNonNull(beskrivelse, "beskrivelse"); - medID(id); - medBeskrivelse(beskrivelse); - } - - @Override - public String beskrivelse() { - return beskrivelseIkkeTom().orElseGet(() -> "(FOREACH " + argName + ")"); - } - - @Override - public Evaluation evaluate(final T t) { - int ix = 0; - Evaluation[] evaluations = new Evaluation[args.size()]; - for (var arg : args) { - var serviceArgument = new ServiceArgument(argName, arg); - evaluations[ix] = spec.evaluate(t, serviceArgument); - ix++; - } - var resultingEval = new ForeachEvaluation(identifikator(), beskrivelse(), evaluations); - return resultingEval; - } - - @Override - public Evaluation evaluate(final T t, ServiceArgument serviceArgument) { - if (serviceArgument != null) { - throw new IllegalArgumentException("Utviklerfeil: Støtter ikke (nøstet) FOREACH med argumenter"); - } - return evaluate(t); - } - - @Override - public String identifikator() { - return identifikatorIkkeTom().orElseGet(() -> "(FOREACH " + argName + ": " + spec.identifikator() + ")"); - } - - @Override - public RuleDescription ruleDescription() { - return new SpecificationRuleDescription(Operator.FOREACH, identifikator(), beskrivelse(), spec.ruleDescription()); - } -} diff --git a/src/main/java/no/nav/fpsak/nare/specification/SequenceSpecification.java b/src/main/java/no/nav/fpsak/nare/specification/SequenceSpecification.java index 3d1bd29..e887026 100644 --- a/src/main/java/no/nav/fpsak/nare/specification/SequenceSpecification.java +++ b/src/main/java/no/nav/fpsak/nare/specification/SequenceSpecification.java @@ -53,11 +53,6 @@ public Builder hvisEllers(Specification test, Specification hvis, Speci return this; } - public Builder forAlle(String argName, List args, Specification spec) { - this.sekvens.add(new ForeachSpecification<>(spec, args, argName)); - return this; - } - public SequenceSpecification siste(Specification specification) { this.sekvens.add(specification); return build(); @@ -117,31 +112,18 @@ public String beskrivelse() { @Override public Evaluation evaluate(final T t) { - var evaluation = doEvaluate(t, null); + var evaluation = doEvaluate(t); 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 Sequence med argument null"); - } - var evaluation = doEvaluate(t, serviceArgument); - if (property != null) { - evaluation.setEvaluationProperty(property.getBeskrivelse(), property.getVerdi().toString()); - } - evaluation.setEvaluationProperty(serviceArgument.getBeskrivelse(), serviceArgument.getVerdi().toString()); - return evaluation; - } - - private SequenceEvaluation doEvaluate(final T t, ServiceArgument serviceArgument) { + private SequenceEvaluation doEvaluate(final T t) { var specSize = specs.size(); Evaluation[] evaluations = new Evaluation[specSize]; for (int ix = 0; ix < specSize; ix++) { - evaluations[ix] = serviceArgument != null ? specs.get(ix).evaluate(t, serviceArgument) : specs.get(ix).evaluate(t); + evaluations[ix] = specs.get(ix).evaluate(t); } return new SequenceEvaluation(identifikator(), beskrivelse(), evaluations); @@ -161,12 +143,6 @@ public RuleDescription ruleDescription() { return new SpecificationRuleDescription(Operator.SEQUENCE, identifikator(), beskrivelse(), ruleDescriptions); } - @Override - public Specification medScope(ServiceArgument scope) { - this.property = scope; - return this; - } - @Override public Specification medEvaluationProperty(ServiceArgument property) { this.property = property; diff --git a/src/main/java/no/nav/fpsak/nare/specification/Specification.java b/src/main/java/no/nav/fpsak/nare/specification/Specification.java index a223c28..412cc5c 100644 --- a/src/main/java/no/nav/fpsak/nare/specification/Specification.java +++ b/src/main/java/no/nav/fpsak/nare/specification/Specification.java @@ -26,10 +26,6 @@ public interface Specification { */ Evaluation evaluate(T t); - default Evaluation evaluate(T t, ServiceArgument serviceArgument) { - return evaluate(t); - } - /** * Specification Builder methods */ @@ -37,12 +33,6 @@ default Evaluation evaluate(T t, ServiceArgument serviceArgument) { Specification medID(String id); - @SuppressWarnings("unused") - @Deprecated // Bruk medEvaluationProperty - evt beregningForeach eller beregningForeachThen - default Specification 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. diff --git a/src/test/java/no/nav/fpsak/nare/specification/modrekvote/regler/CountingpecificationTest.java b/src/test/java/no/nav/fpsak/nare/specification/modrekvote/regler/CountingpecificationTest.java index 6768c92..c21ad21 100644 --- a/src/test/java/no/nav/fpsak/nare/specification/modrekvote/regler/CountingpecificationTest.java +++ b/src/test/java/no/nav/fpsak/nare/specification/modrekvote/regler/CountingpecificationTest.java @@ -2,19 +2,16 @@ import static org.assertj.core.api.Assertions.assertThat; -import java.util.List; import java.util.Optional; import org.junit.jupiter.api.Test; import no.nav.fpsak.nare.RuleService; import no.nav.fpsak.nare.Ruleset; -import no.nav.fpsak.nare.ServiceArgument; import no.nav.fpsak.nare.evaluation.Evaluation; import no.nav.fpsak.nare.evaluation.Resultat; import no.nav.fpsak.nare.evaluation.RuleOutcome; import no.nav.fpsak.nare.evaluation.node.SingleEvaluation; -import no.nav.fpsak.nare.evaluation.summary.EvaluationSerializer; import no.nav.fpsak.nare.evaluation.summary.EvaluationSummary; import no.nav.fpsak.nare.specification.LeafSpecification; import no.nav.fpsak.nare.specification.Specification; @@ -24,14 +21,14 @@ public class CountingpecificationTest { @Test public void skal_evaluere_regel_med_ellers_branch() { var singleSpecification = new SingleRule(); - var evaluation = singleSpecification.evaluer(new MellomregnInt(1)); + var evaluation = singleSpecification.evaluer(new MellomregnInt(3)); assertThat(evaluation.result()).isEqualTo(Resultat.JA); - System.out.println(EvaluationSerializer.asJson(evaluation)); - System.out.println(EvaluationSerializer.asJson(singleSpecification.getSpecification())); + //System.out.println(EvaluationSerializer.asJson(evaluation)); + //System.out.println(EvaluationSerializer.asJson(singleSpecification.getSpecification())); var evaluationSummary = new EvaluationSummary(evaluation); var outcome = hentOutcome(evaluationSummary); - assertThat(outcome).contains(12); + assertThat(outcome).contains(16); } @Test @@ -44,7 +41,7 @@ public void skal_evaluere_regel_med_hvis_branch() { //System.out.println(EvaluationSerializer.asJson(singleSpecification.getSpecification())); var evaluationSummary = new EvaluationSummary(evaluation); var outcome = hentOutcome(evaluationSummary); - assertThat(outcome).contains(24); + assertThat(outcome).contains(8); } private Optional hentOutcome(EvaluationSummary evaluationSummary) { @@ -81,7 +78,6 @@ public Specification getSpecification() { var rs = new Ruleset(); var regel = rs.sekvensRegel() .neste(new AddOneLeaf()) - .forAlle("bokstav", List.of("A", "B", "C"), 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())) @@ -104,13 +100,6 @@ public Evaluation evaluate(MellomregnInt input) { var outcome = new RuleOutcome<>(input.carryon); return ja(); } - - @Override - public Evaluation evaluate(MellomregnInt input, ServiceArgument args) { - var bokstav = Optional.ofNullable(args).filter(a -> "bokstav".equals(args.beskrivelse())).map(a -> (String)a.verdi()).orElse(null); - input.carryon = input.carryon + 1; - return ja(); - } } private static class MultiplyByTwoLeaf extends LeafSpecification {