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).

Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2025 Red Hat