이 콘텐츠는 선택한 언어로 제공되지 않습니다.

4.3. Declarative Model


4.3.1. Declarative Model

Using a declarative model has the following benefits:
  • It reinforces that the model belongs to the knowledge base, not the application.
  • The model can have a lifecycle that is separate from the applications.
  • Java types can be enriched with rule specific annotations.
  • JAR files must be kept synchronized between the rules and the applications that use them; however, a declarative model does not need to be kept synchronized.
Declarative models can be either of the following:
  • A standalone definition of the entire Fact model used within your rules.
  • Supplementary Fact definitions to support a Java POJO Model.

4.3.2. Creating a Declarative Model

Procedure 4.2. Creating a Declarative Model

  1. Open the New Declarative Model menu

    From the navigation panel, select Knowledge Bases Create New New Declarative Model.
  2. Specify a name for the new model. Select which package to create the model in or specify that it should be added to the Global Area. Click OK when all the details have been entered.
  3. Click Add new fact type and enter the fact name in the name field of the popup menu.
  4. Create Fact Fields by selecting the Add field button and entering the information in the popup menu.
  5. Create Fact annotations by selecting the Add annotation button. The annotation Name and Value fields are mandatory, but the Key field is optional. If a Key value is not specified, a default value of value will be assigned.
  6. Select Type Extends to extend an existing Java class uploaded as part of JAR (POJO) model or other declared types in the same package.

4.3.3. Extending a Java Class

To extend a Java class, the following steps need to be completed:
  • Import the applicable Java JAR into BRMS.
  • If the Java package name in which the class belongs to is different to the package name in to which the JAR has been imported, ensure the BRMS package imports the class from which you want to extend. This is normally completed for you automatically when you upload a JAR model; however, if you have multiple classes with the same name in the JAR, you should check the appropriate one has been imported.
  • Within the Declarative Modeling screen define an empty type (i.e. with no fields) of the same name as that you want to extend.
  • Create a new declarative type as appropriate, extending the empty declaration created in the preceding step.

Note

Only properties on Java classes that have both a "getter" and "setter" following standard Java Bean conventions are available on the declared sub-type. This occurs because the declarative semantics imply all properties on declared types have both accessors and mutators.

4.3.4. Consuming a Declarative Model with Java

Declared types are generated at knowledge base compilation time, and as such the application will only have access to them at application run time. Therefore, these classes are not available for direct reference from the application.
Declarative types can be used like normal fact objects, but the way you create them is different (as they are not on your applications classpath). To create these objects, they are available from the KnowledgeBase instance.

Example 4.1. Handling Declared Fact Types Through the API

// get a reference to a knowledge base with a declared type:
KnowledgeBase kbase = ...

// get the declared FactType
FactType personType = kbase.getFactType( "org.drools.examples",
                                         "Person" );

// handle the type as necessary:
// create instances:
Object bob = personType.newInstance();

// set attributes values
personType.set( bob,
                "name",
                "Bob" );
personType.set( bob,
                "age",
                42 );

// insert fact into a session
StatefulKnowledgeSession ksession = ...
ksession.insert( bob );
ksession.fireAllRules();

// read attributes
String name = personType.get( bob, "name" );
int age = personType.get( bob, "age" );
Copy to Clipboard Toggle word wrap

Note

The namespace of the declared type is the package namespace where it was declared (i.e. org.drools.examples in the above example).

맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat