예제

다음은 예제 규칙 세트입니다.

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

    <!-- 이러한 속성은 작성 시 지정해야 합니다. -->
    <Initialization>
      <Attribute name="firstName">
        <type>
          <javaclass name="String"/>
        </type>
      </Attribute>

      <Attribute name="lastName">
        <type>
          <javaclass name="String"/>
        </type>
      </Attribute>
    </Initialization>

    <Attribute name="incomes">
      <type>
        <javaclass name="List">
          <ruleclass name="Income"/>
        </javaclass>
      </type>
      <derivation>
        <!-- "Income" 유형의 모든
              규칙 오브젝트를 읽습니다. -->
        <readall ruleclass="Income"/>
      </derivation>
    </Attribute>

  </Class>

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

  </Class>

</RuleSet>

위의 규칙 세트에서 readall 표현식을 사용하여 Income 규칙 클래스의 모든 인스턴스를 검색합니다.

외부 규칙 오브젝트를 작성하려면 규칙 오브젝트를 작성할 때 클라이언트 코드나 테스트에 세션이 지정되어야 합니다.

package curam.creole.example;

import junit.framework.TestCase;
import curam.creole.calculator.CREOLETestHelper;
import curam.creole.execution.RuleObject;
import curam.creole.execution.session.InterpretedRuleObjectFactory;
import curam.creole.execution.session.RecalculationsProhibited;
import curam.creole.execution.session.Session;
import curam.creole.execution.session.Session_Factory;
import
 curam.creole.execution.session.StronglyTypedRuleObjectFactory;
import curam.creole.parser.RuleSetXmlReader;
import
 curam.creole.ruleclass.Example_externalRuleObjects.impl.Income;
import
 curam.creole.ruleclass.Example_externalRuleObjects.impl.Income_Factory;
import
 curam.creole.ruleclass.Example_externalRuleObjects.impl.Person;
import
 curam.creole.ruleclass.Example_externalRuleObjects.impl.Person_Factory;
import curam.creole.ruleitem.RuleSet;
import curam.creole.storage.inmemory.InMemoryDataStorage;

/**
 * 클라이언트 코드가 직접 작성한 외부 규칙 오브젝트를 테스트합니다.
 */
public class TestCreateExternalRuleObjects extends TestCase {

  /**
   * 생성된 코드를 사용한 외부 규칙 오브젝트의 작성을 보여주는
   * 예제입니다.
   */
  public void testUsingGeneratedTestClasses() {

    final Session session =
        Session_Factory.getFactory().newInstance(
            new RecalculationsProhibited(),
            new InMemoryDataStorage(
                new StronglyTypedRuleObjectFactory()));

    /**
     * 컴파일러가 올바른 유형의 초기화 인수를
     * 제공하도록 합니다.
     */
    final Person person =
        Person_Factory.getFactory().newInstance(session, "John",
            "Smith");
    CREOLETestHelper.assertEquals("John", person.firstName()
        .getValue());

    /**
     * 이러한 오브젝트는 규칙 세트의
     *
     * <readall ruleclass="Income"/>
     *
     * 표현식을 통해 검색합니다.
     */
    final Income income1 =
        Income_Factory.getFactory().newInstance(session);
    income1.amount().specifyValue(123);

    final Income income2 =
        Income_Factory.getFactory().newInstance(session);
    income2.amount().specifyValue(345);

  }

  /**
   * CER 규칙 세트 해석기를 사용한 외부 규칙 오브젝트의
   * 작성을 보여주는 예제입니다.
   */
  public void testUsingInterpreter() {

    /* 규칙 세트에서 읽기 */
    final RuleSet ruleSet = getRuleSet();

    /* 해석된 세션 시작 */
    final Session session =
        Session_Factory.getFactory().newInstance(
            new RecalculationsProhibited(),
            new InMemoryDataStorage(
                new InterpretedRuleObjectFactory()));

    /**
     * 컴파일러가 올바른 유형의 초기화 인수를
     * 제공하도록 할 수 없습니다. 인수가 잘못된 경우
     * CER이 런타임 오류를 보고합니다.
     */
    final RuleObject person =
        session.createRuleObject(ruleSet.findClass("Person"),
            "John", "Smith");
    CREOLETestHelper.assertEquals("John", person
        .getAttributeValue("firstName").getValue());

    /**
     * 이러한 오브젝트는 규칙 세트의
     *
     * <readall ruleclass="Income"/>
     *
     * 표현식을 통해 검색합니다.
     */
    final RuleObject income1 =
        session.createRuleObject(ruleSet.findClass("Income"));
    income1.getAttributeValue("amount").specifyValue(123);

    final RuleObject income2 =
        session.createRuleObject(ruleSet.findClass("Income"));
    income2.getAttributeValue("amount").specifyValue(345);
  }

  /**
   * XML 소스 파일에서 Example_externalRuleObjects를
   * 읽습니다.
   */
  private RuleSet getRuleSet() {

    /* 규칙 세트 소스 파일의 상대 경로 */
    final String ruleSetRelativePath =
        "./rules/Example_externalRuleObjects.xml";

    /* 규칙 세트 소스에서 읽기 */
    final RuleSetXmlReader ruleSetXmlReader =
        new RuleSetXmlReader(ruleSetRelativePath);

    /* 문제점 덤프 */
    ruleSetXmlReader.validationProblemCollection().printProblems(
        System.err);

    /* 규칙 세트에 오류가 있는 경우 실패 */
    assertTrue(!ruleSetXmlReader.validationProblemCollection()
        .containsErrors());

    /* 리더에서 규칙 세트 리턴 */
    return ruleSetXmlReader.ruleSet();
  }

}