Statische Methoden entwickeln

CER unterstützt eine Vielzahl von Ausdrücken, mit denen wahrscheinlich die benötigten Berechnungen bereitgestellt werden können.

Für Fälle, in denen eine Geschäftsberechnung nicht unter Verwendung der CER-Ausdrücke implementiert werden kann, unterstützt CER den Ausdruck call, damit Sie aus Ihrem Regelwerk heraus eine statische Methode für eine angepasste Java-Klasse aufrufen können. Der CER-Editor enthält einige Regelelemente (z. B. "Aufrufen"), damit Benutzer eine statische Methode für eine angepasste Java-Klasse definieren können. Weitere Informationen enthält der Abschnitt Aufruf, für das Element "Aufruf".

Das folgende Beispiel zeigt ein Regelwerk, das eine Java-Methode aufruft:

<?xml version="1.0" encoding="UTF-8"?>
<RuleSet name="Example_StaticMethodDevelopment"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation=
"http://www.curamsoftware.com/CreoleRulesSchema.xsd">
  <Class name="Income">

    <Attribute name="paymentReceivedDate">
      <type>
        <javaclass name="curam.util.type.Date"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="amount">
      <type>
        <javaclass name="Number"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

  </Class>

  <Class name="Person">

    <Attribute name="incomes">
      <type>
        <javaclass name="List">
          <ruleclass name="Income"/>
        </javaclass>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="mostRecentIncome">
      <type>
        <ruleclass name="Income"/>
      </type>
      <derivation>
        <!-- In early development, the method
             identifyMostRecentIncome_StronglyTyped would be used.

             In practice, you would not maintain two versions of
             each method; you would simply weaken the argument
             and return types, and keep a single method.
          -->

        <call class="curam.creole.example.BestPracticeDevelopment"
          method="identifyMostRecentIncome_WeaklyTyped">
          <type>
            <ruleclass name="Income"/>
          </type>
          <arguments>
            <this/>
          </arguments>
        </call>
      </derivation>
    </Attribute>
  </Class>

</RuleSet>

Beim Entwickeln einer statischen Methode können Sie die von CER generierten Java-Klassen als Ausgangspunkt für die Argumenttypen und/oder Rückgabetypen der Methode verwenden:

package curam.creole.example;

import java.util.List;

import curam.creole.execution.session.Session;
import
 curam.creole.ruleclass.Example_StaticMethodDevelopment.impl.Income;
import
 curam.creole.ruleclass.Example_StaticMethodDevelopment.impl.Person;

public class BestPracticeDevelopment {

  /**
   * Identifies a person's most recent income.
   *
   * Note that this calculation can be performed using CER
   * expressions, but is shown here in Java just to illustrate the
   * use of generated types when developing static Java methods for
   * use with CER.
   *
   * This method is suitable only for use in development; for
   * production, see the
   * {@linkplain #identifyMostRecentIncome_WeaklyTyped} below.
   *
   * In practice, you would not maintain two versions of each
   * method; you would simply weaken the argument and return types,
   * and keep a single method.
   */
  public static Income identifyMostRecentIncome_StronglyTyped(
      final Session session, final Person person) {

    Income mostRecentIncome = null;
    final List<? extends Income> incomes =
        person.incomes().getValue();
    for (final Income current : incomes) {
      if (mostRecentIncome == null
          || mostRecentIncome.paymentReceivedDate().getValue()
              .before(current.paymentReceivedDate().getValue())) {
        mostRecentIncome = current;
      }
    }

    return mostRecentIncome;
  }
}

Sobald die Java-Methode wie gewünscht funktioniert, müssen Sie die Typen so abschwächen, dass anstelle der generierten Java-Klassen (die nicht in einer Produktionsumgebung eingesetzt werden sollten) dynamische Regelobjekte verwendet werden:

/**
   * Identifies a person's most recent income.
   *
   * Note that this calculation can be performed using CER
   * expressions, but is shown here in Java just to illustrate the
   * use of generated types when developing static Java methods for
   * use with CER.
   *
   * This method is suitable for use in production; for initial
   * development, see
   * {@linkplain #identifyMostRecentIncome_StronglyTyped} above.
   *
   * In practice, you would not maintain two versions of each
   * method; you would simply weaken the argument and return types,
   * and keep a single method.
   */
  public static RuleObject identifyMostRecentIncome_WeaklyTyped(
      final Session session, final RuleObject person) {

    RuleObject mostRecentIncome = null;
    final List<? extends RuleObject> incomes =
        (List<? extends RuleObject>) person.getAttributeValue(
            "incomes").getValue();
    for (final RuleObject current : incomes) {
      if (mostRecentIncome == null
          || ((Date) mostRecentIncome.getAttributeValue(
              "paymentReceivedDate").getValue())
              .before((Date) current.getAttributeValue(
                  "paymentReceivedDate").getValue())) {
        mostRecentIncome = current;
      }
    }

    return mostRecentIncome;

  }
Vorsicht:
Wenn Sie die Implementierung einer statischen Methode ändern, weiß CER nicht automatisch, dass Attributwerte neu berechnet werden müssen, die unter Verwendung der alten Version der statischen Methode berechnet wurden.

Sobald eine statische Methode in einer Produktionsumgebung für gespeicherte Attributwerte verwendet wurde, sollten Sie anstelle einer Änderung der Implementierung eine neue statische Methode erstellen (mit der erforderlichen neuen Implementierung) und Ihre Regelwerke so ändern, dass die neue statische Methode verwendet wird. Wenn Sie Ihre Regelwerkänderungen, die auf die neue statische Methode verweisen, veröffentlichen, berechnet CER automatisch alle Instanzen des betroffenen Attributwerts neu.

Für jede aus CER heraus aufgerufene statische Methode (oder Methode für Eigenschaften) gilt Folgendes: