# Chapter 69. Rule based model configuration

Support for rule based configuration is currently incubating. Please be aware that the DSL, APIs and other configuration may change in later Gradle versions.

This chapter describes and documents what is essentially the foundation for Gradle 3.0 and the next generation of Gradle builds. It is being incrementally developed during the Gradle 2.x stream. Gradle's support for building native applications and Play Framework applications already uses this configuration model. Gradle also includes some initial support for building Java libraries using this configuration model.

All of the mechanisms, DSL, API, and techniques discussed here are incubating and are not considered stable and subject to change - see Appendix C, The Feature Lifecycle). Exposing new features early, during incubation, allows early testing and the incorporation of real world feedback ultimately resulting in a better Gradle.

The following build script is an example of a rule based build.

Example 69.1. an example of a simple rule based build

`build.gradle`

    @Managed
    interface Person {
      void setFirstName(String name)
      String getFirstName()

      void setLastName(String n)
      String getLastName()
    }

    class PersonRules extends RuleSource {
      @Model void person(Person p) {}

      @Mutate void setFirstName(Person p) {
        p.firstName = "John"
      }

      @Mutate void createHelloTask(ModelMap<Task> tasks, Person p) {
        tasks.create("hello") {
          doLast {
            println "Hello $p.firstName $p.lastName!"
          }
        }
      }
    }

    apply plugin: PersonRules

    model {
      person {
        lastName = "Smith"
      }
    }

Note: The code for this example can be found at `samples/modelRules/basicRuleSourcePlugin` in the ‘-all’ distribution of Gradle.

Output of `gradle hello`

    > gradle hello
    :hello
    Hello John Smith!

    BUILD SUCCESSFUL

    Total time: 1 secs

The rest of this chapter is dedicated to explaining what is going on in this build script, and why Gradle is moving in this direction.

## 69.1. Background

Gradle embraces domain modelling as a core tenet. Focusing on the domain model as opposed to the execution model (like prior generation build tools such as Apache Ant) has many advantages. A strong domain model communicates the intent (i.e. the what) over the mechanics (i.e. the how). This allows humans to understand builds at a level that is meaningful to them.

As well as helping humans, a strong domain model also helps the dutiful machines. Plugins can more effectively collaborate around a strong domain model (e.g. plugins can say something about Java applications, such as providing conventions). Very importantly, by having a model of the what instead of the how Gradle can make intelligent choices on just how to do the how.

The move towards “Rule based model configuration” can be summarised as improving Gradle's ability to model richer domains in a more effective way. It also makes expressing the kinds of models present in today's Gradle more robust and simpler.

## 69.2. Motivations for change

Domain modelling in Gradle is not new. The Java plugin's `SourceSet` concept is an example of domain modelling, as is the modelling of `NativeBinary` in the native plugin suite.

One distinguishing characteristic of Gradle compared to other build tools that also embrace modelling is that Gradle's model is open and collaborative. Gradle is fundamentally a tool for modelling software construction and then realizing the model, via tasks such as compilation etc.. Different domain plugins (e.g. Java, C++, Android) provide models that other plugins can collaborate with and build upon.

While Gradle has long employed sophisticated techniques when it comes to realizing the model (i.e. what we know as building things), the next generation of Gradle builds will employ some of the same techniques to building of the model itself. By defining build tasks as effectively a graph of dependent functions with explicit inputs and outputs, Gradle is able to order, cache, parallelize and apply other optimizations to the work. Using a “graph of tasks” for the production of software is a long established idea, and necessary given the complexity of software production. The task graph effectively defines the rules of execution that Gradle must follow. The term “Rule based model configuration” refers to applying the same concepts to building the model that builds the task graph.

Another key motivation is performance and scale. Aspects of the current approach that Gradle takes to modelling the build prevent pervasive parallelism and limit scalability. The new model is being designed with the requirements of modern software delivery in mind, where immediate responsiveness is critical for projects large and small.

## 69.3. Concepts

This section outlines the key concepts of rule based model configuration. Subsequent sections in this chapter will show the concepts in action.

### 69.3.1. The “model space”

The term “model space” is used to refer to the formal model, addressable by rules.

An analog with existing model is effectively the “project space”. The `Project` object is effectively the root of a graph of objects (e.g `project.repositories`, `project.tasks` etc.). A build script is effectively adding and configuring objects of this graph. For the most part, the “project space” is opaque to Gradle. It is an arbitrary graph of objects that Gradle only partially understands.

Each project also has its own model space, which is distinct from the project space. A key characteristic of the “model space” is that Gradle knows much more about it (which is knowledge that can be put to good use). The objects in the model space are “managed”, to a greater extent than objects in the project space. The origin, structure, state, collaborators and relationships of objects in the model space are first class constructs. This is effectively the characteristic that functionally distinguishes the model space from the project space: the objects of the model space are defined in ways that Gradle can understand them intimately, as opposed to an object that is the result of running relatively opaque code. A “rule” is effectively a building block of this definition.

The model space will eventually replace the project space, in so far as it will be the only “space”. However, during the transition the distinction is helpful.

### 69.3.2. Model paths

A model path identifies a path through a model space, to an element. A common representation is a period-delimited set of names. The model path `"tasks"` is the path to the element that is the task container. Assuming a task who's name is `hello`, the path `"tasks.hello"` is the path to this task.

TBD - more needed here.

### 69.3.3. Rules

The model space is defined in terms of “rules”. A rule is just a function (in the abstract sense) that either produces a model element, or acts upon a model element. Every rule has a single subject and zero or more inputs. Only the subject can be changed by a rule, while the inputs are effectively immutable.

Gradle guarantees that all inputs are fully “realized“ before the rule executes. The process of “realizing” a model element is effectively executing all the rules for which it is the subject, transitioning it to its final state. There is a strong analogy here to Gradle's task graph and task execution model. Just as tasks depend on each other and Gradle ensures that dependencies are satisfied before executing a task, rules effectively depend on each other (i.e. a rule depends on all rules whose subject is one of the inputs) and Gradle ensures that all dependencies are satisfied before executing the rule.

Model elements are very often defined in terms of other model elements. For example, a compile task's configuration can be defined in terms of the configuration of the source set that it is compiling. In this scenario, the compile task would be the subject of a rule and the source set an input. Such a rule could configure the task subject based on the source set input without concern for how it was configured, who it was configured by or when the configuration was specified.

There are several ways to declare rules, and in several forms. An explanation of the different forms and mechanisms along with concrete examples is forthcoming in this chapter.

### 69.3.4. Managed model elements

Currently, any kind of Java object can be part of the model space. However, there is a difference between “managed” and “unmanaged” objects.

A “managed” object is transparent and enforces immutability once realized. Being transparent means that its structure is understood by the rule infrastructure and as such each of its properties are also individual elements in the model space.

An “unmanaged” object is opaque to the the model space and does not enforce immutability. Over time, more mechanisms will be available for defining managed model elements culminating in all model elements being managed in some way.

Managed models can be defined by attaching the `@Managed` annotation to an interface:

Example 69.2. a managed type

`build.gradle`

    @Managed
    interface Person {
      void setFirstName(String name)
      String getFirstName()

      void setLastName(String n)
      String getLastName()
    }

By defining a getter/setter pair, you are effectively declaring a managed property. A managed property is a property for which Gradle will enforce semantics such as immutability when a node of the model is not the subject of a rule. Therefore, this example declares a property named name on the managed type Person. This property will only be writable when the view is mutable, that is to say when the Person is the subject of a `Rule` (see below the explanation for rules).

Properties can be of any scalar type. A scalar type is either:

- a primitive type (e.g. `int`) or its boxed type (e.g `Integer`)
- a `BigInteger` or `BigDecimal`
- a `String`
- a `File`
- an enumeration type

In addition, properties can also be of any type which is itself managed:

Property type | Nullable | Example
`String` | Yes | Example 69.3. a String property `build.gradle`
    void setFirstName(String name)
    String getFirstName()
`File` | Yes | Example 69.4. a File property `build.gradle`
    void setHomeDirectory(File homeDir)
    File getHomeDirectory()
`Integer`, `Boolean`, `Byte`, `Short`, `Float`, `Long`, `Double` | Yes | Example 69.5. a Long property `build.gradle`
    void setId(Long id)
    Long getId()
`int`, `boolean`, `byte`, `short`, `float`, `long`, `double` | No | Example 69.6. a boolean property `build.gradle`
    void setEmployed(boolean isEmployed)
    boolean isEmployed()
Example 69.7. an int property `build.gradle`
    void setAge(int age)
    int getAge()
Another managed type. | Only if read/write | Example 69.8. a managed property `build.gradle`
    void setMother(Person mother)
    Person getMother()
An enumeration type. | Yes | Example 69.9. an enumeration type property `build.gradle`
    void setMaritalStatus(MaritalStatus status)
    MaritalStatus getMaritalStatus()
A `ManagedSet`. A managed set supports the creation of new named model elements, but not their removal. | Only if read/write | Example 69.10. a managed set `build.gradle`
    ManagedSet<Person> getChildren()
A `Set` or `List` of scalar types. All classic operations on collections are supported: add, remove, clear... | Only if read/write | Example 69.11. a managed set `build.gradle`
    void setUserGroups(List<String> groups)
    List<String> getUserGroups()

If the type of a property is itself a managed type, it is possible to declare only a getter, in which case you are declaring a read-only property. A read-only property must not be mistaken with an immutable property. A read-only property will be instantiated by Gradle, and cannot be replaced with another object of the same type (for example calling a setter). However, the properties of that property can potentially be changed, if, and only if, the property is the subject of a rule. If it's not the case, the property is immutable, like any classic read/write managed property.

Managed types can be defined out of interfaces or abstract classes and are usually defined in plugins, which are written either in Java or Groovy. Please see the `Managed` annotation for more information on creating managed model objects.

### 69.3.5. Model element types

There are particular types (language types) supported by the model space and can be generalised as follows:

Table 69.2. Type definitions

Type | Definition
Scalar | A scalar type is considered to be one of the following: a primitive type (e.g. `int`) or its boxed type (e.g `Integer`) a `BigInteger` or `BigDecimal` a `String` a `File` an enumeration type
Scalar Collection | A java.util.List or java.util.Set containing one of the scalar types
Managed type | Any class which is a valid managed model (i.e.annotated with @`Managed`)
Managed collection | A `ModelMap` or `ModelSet`

There are various contexts in which these types can be used:

Table 69.3. Model type support

Context | Supported types
Creating top level model elements | Any managed type `FunctionalSourceSet` (when the `LanguageBasePlugin` plugin has been applied)
Properties of managed model elements | The properties (attributes) of a managed model elements may be one or more of the following: A managed type A type which is annotated with @`Unmanaged` A Scalar Collection A Managed collection containing managed types A Managed collection containing `org.gradle.language.base.FunctionalSourceSet`s (when the `LanguageBasePlugin` plugin has been applied)

### 69.3.6. References, binding and scopes

As previously mentioned, a rule has a subject and zero or more inputs. The rule's subject and inputs are declared as “references” and are “bound” to model elements before execution by Gradle. Each rule must effectively forward declare the subject and inputs as references. Precisely how this is done depends on the form of the rule. For example, the rules provided by a `RuleSource` declare references as method parameters.

A reference is either “by-path” or “by-type”.

A “by-type” reference identifies a particular model element by its type. For example, a reference to the `TaskContainer` effectively identifies the `"tasks"` element in the project model space. The model space is not exhaustively searched for candidates for by-type binding. The search space for a by-type binding is determined by the “scope” of the rule (discussed later).

A “by-path” reference identifies a particular model element by its path in model space. By-path references are always relative to the rule scope; there is currently no way to path “out” of the scope. All by-path references also have an associated type, but this does not influence what the reference binds to. The element identified by the path must however by type compatible with the reference, or a fatal “binding failure” will occur.

#### 69.3.6.1. Binding scope

Rules are bound within a “scope”, which determines how references bind. Most rules are bound at the project scope (i.e. the root of the model graph for the project). However, rules can be scoped to a node within the graph. The `ModelMap.named()` method is an example, of a mechanism for applying scoped rules. Rules declared in the build script using the `model {}` block, or via a `RuleSource` applied as a plugin use the root of the model space as the scope. This can be considered the default scope.

By-path references are always relative to the rule scope. When the scope is the root, this effectively allows binding to any element in the graph. When it is not, the children of the scope can be referred to by-path.

When binding by-type references, the following elements are considered:

- The scope element itself.
- The immediate children of the scope element.
- The immediate children of the model space (i.e. project space) root.

For the common case, where the rule is effectively scoped to the root, only the immediate children of the root need to be considered.

## 69.4. Rule sources

One way to define rules, is via a `RuleSource` subclass. Such types can be applied in the same manner (to project objects) as `Plugin` implementations (i.e. via `Project.apply()`).

Example 69.12. applying a rule source plugin

`build.gradle`

    @Managed
    interface Person {
      void setFirstName(String name)
      String getFirstName()

      void setLastName(String n)
      String getLastName()
    }

    class PersonRules extends RuleSource {
      @Model void person(Person p) {}

      @Mutate void setFirstName(Person p) {
        p.firstName = "John"
      }

      @Mutate void createHelloTask(ModelMap<Task> tasks, Person p) {
        tasks.create("hello") {
          doLast {
            println "Hello $p.firstName $p.lastName!"
          }
        }
      }
    }

    apply plugin: PersonRules

Rule source plugins can be packaged and distributed in the same manner as other types of plugins (see Chapter 61, Writing Custom Plugins).

The different methods of the rule source are discrete, independent rules. Their order, or the fact that they belong to the same class, are irrelevant.

Example 69.13. a model creation rule

`build.gradle`

    @Model void person(Person p) {}

This rule declares that there is a model element at path `"person"` (defined by the method name), of type `Person`. This is the form of the `Model` type rule for `Managed` types. Here, the person object is the rule subject. The method could potentially have a body, that mutated the person instance. It could also potentially have more parameters, that would be the rule inputs.

Example 69.14. a model mutation rule

`build.gradle`

    @Mutate void setFirstName(Person p) {
      p.firstName = "John"
    }

This `Mutate` rule mutates the person object. The first parameter to the method is the subject. Here, a by-type reference is used as no `Path` annotation is present on the parameter. It could also potentially have more parameters, that would be the rule inputs.

Example 69.15. creating a task

`build.gradle`

    @Mutate void createHelloTask(ModelMap<Task> tasks, Person p) {
      tasks.create("hello") {
        doLast {
          println "Hello $p.firstName $p.lastName!"
        }
      }
    }

This `Mutate` rule effectively adds a task, by mutating the tasks collection. The subject here is the `"tasks"` node, which is available as a `ModelMap` of `Task`. The only input is our person element. As the person is being used as an input here, it will have been realised before executing this rule. That is, the task container effectively depends on the person element. If there are other configuration rules for the person element, potentially specified in a build script or other plugin, they will also be guaranteed to have been executed.

As `Person` is a `Managed` type in this example, any attempt to modify the person parameter in this method would result in an exception being thrown. Managed objects enforce immutability at the appropriate point in their lifecycle.

Please see the documentation for `RuleSource` for more information on constraints on how rule sources must be implemented and for more types of rules.

## 69.5. The “model DSL”

It is also possible to declare rules directly in the build script using the “model DSL”.

Example 69.16. the model dsl

`build.gradle`

    model {
      person {
        lastName = "Smith"
      }
    }

Continuing with the example so far of the model element `"person"` of type `Person` being present, the above DSL snippet effectively adds a mutation rule for the person that sets its `lastName` property.

The general form of the model DSL is:

    model {
      «model-path-to-subject» {
        «imperative code»
      }
    }

Where there may be multiple blocks.

It is also possible to create `Managed` type elements at the root level.

The general form of a creation rule is:

    model {
      «element-name»(«element-type») {
        «imperative code»
      }
    }

The following model rule is creating the person element:

Example 69.17. a DSL creation rule

`build.gradle`

    person(Person) {
      firstName = "John"
    }

Note: The code for this example can be found at `samples/modelRules/modelDsl` in the ‘-all’ distribution of Gradle.

The model DSL is currently quite limited. It is only possible to declare creation and general mutation rules. It is also only possible to refer to the subject by-path. These are all limitations that will be addressed in future Gradle versions.

### 69.5.1. Declaring input dependencies

Rules declared in the DSL may depend on other model elements through the use of a special syntax.

Example 69.18. a DSL rule using inputs

`build.gradle`

    tasks {
      create("helloFromDsl") {
        def p = $.person
        doLast {
          println "Hello $p.firstName $p.lastName!"
        }
      }
    }

Note: The code for this example can be found at `samples/modelRules/modelDsl` in the ‘-all’ distribution of Gradle.

In the above snippet, the `$.person` construct is an input reference. The construct returns the value of the model element at the specified path, as its default type (i.e. the type advertised by the Model Report). It may appear anywhere in the rule that a value may normally appear. It is not limited to the right hand side of variable assignments.

The general syntax is of the form:

    $.«path-to-model-element»

Paths are a period separated list of identifiers. To directly depend on the `firstName` of the person, the following could be used:

    $.person.firstName

The input element is guaranteed to be fully configured before the rule executes. That is, all of the rules that mutate the element are guaranteed to have been previously executed, leaving the target element in its final, immutable, state.

Most model elements enforce immutability when being used as inputs. Any attempt to mutate such an element will result in a runtime error. However, some legacy type objects do not currently implement such checks. Regardless, it is always invalid to attempt to mutate an input to a rule.

## 69.6. The Model Report

The built-in `ModelReport` task displays a hierarchical view of the elements in the model space. Each item prefixed with a `+` on the model report is a model element and the visual nesting of these elements correlates to the model path (e.g. `tasks.help`). The model report displays the following details about each model element:

Table 69.4. Model report - model element details

Detail | Description
Type | This is the underlying type of the model element and is typically a fully qualified class name.
Value | Is conditionally displayed on the report when a model element can be represented as a string.
Creator | Every model element has a creator. A creator signifies the origin of the model element (i.e. what created the model element).
Rules | Is a listing of the rules, excluding the creator rule, which are executed for a given model element. The order in which the rules are displayed reflects the order in which they are executed.

Example 69.19. model task output

Output of `gradle model`

    > gradle model
    :model

    ------------------------------------------------------------
    Root project
    ------------------------------------------------------------

    + person
          | Type:       Person
          | Creator:     PersonRules#person
          | Rules:
             ⤷ model.person @ build.gradle line 57, column 3
             ⤷ PersonRules#setFirstName
        + age
              | Type:       int
              | Creator:     PersonRules#person
        + children
              | Type:       org.gradle.model.collection.ManagedSet<Person>
              | Creator:     PersonRules#person
        + employed
              | Type:       boolean
              | Creator:     PersonRules#person
        + father
              | Type:       Person
              | Creator:     PersonRules#person
        + firstName
              | Type:       java.lang.String
              | Value:      John
              | Creator:     PersonRules#person
        + homeDirectory
              | Type:       java.io.File
              | Creator:     PersonRules#person
        + id
              | Type:       java.lang.Long
              | Creator:     PersonRules#person
        + lastName
              | Type:       java.lang.String
              | Value:      Smith
              | Creator:     PersonRules#person
        + maritalStatus
              | Type:       MaritalStatus
              | Creator:     PersonRules#person
        + mother
              | Type:       Person
              | Creator:     PersonRules#person
        + userGroups
              | Type:       java.util.List<java.lang.String>
              | Creator:     PersonRules#person
    + tasks
          | Type:       org.gradle.model.ModelMap<org.gradle.api.Task>
          | Creator:     Project.<init>.tasks()
          | Rules:
             ⤷ PersonRules#createHelloTask
        + components
              | Type:       org.gradle.api.reporting.components.ComponentReport
              | Value:      task ':components'
              | Creator:     tasks.addPlaceholderAction(components)
              | Rules:
                 ⤷ copyToTaskContainer
        + dependencies
              | Type:       org.gradle.api.tasks.diagnostics.DependencyReportTask
              | Value:      task ':dependencies'
              | Creator:     tasks.addPlaceholderAction(dependencies)
              | Rules:
                 ⤷ copyToTaskContainer
        + dependencyInsight
              | Type:       org.gradle.api.tasks.diagnostics.DependencyInsightReportTask
              | Value:      task ':dependencyInsight'
              | Creator:     tasks.addPlaceholderAction(dependencyInsight)
              | Rules:
                 ⤷ HelpTasksPlugin.Rules#addDefaultDependenciesReportConfiguration
                 ⤷ copyToTaskContainer
        + hello
              | Type:       org.gradle.api.Task
              | Value:      task ':hello'
              | Creator:     PersonRules#createHelloTask > create(hello)
              | Rules:
                 ⤷ copyToTaskContainer
        + help
              | Type:       org.gradle.configuration.Help
              | Value:      task ':help'
              | Creator:     tasks.addPlaceholderAction(help)
              | Rules:
                 ⤷ copyToTaskContainer
        + init
              | Type:       org.gradle.buildinit.tasks.InitBuild
              | Value:      task ':init'
              | Creator:     tasks.addPlaceholderAction(init)
              | Rules:
                 ⤷ copyToTaskContainer
        + model
              | Type:       org.gradle.api.reporting.model.ModelReport
              | Value:      task ':model'
              | Creator:     tasks.addPlaceholderAction(model)
              | Rules:
                 ⤷ copyToTaskContainer
        + projects
              | Type:       org.gradle.api.tasks.diagnostics.ProjectReportTask
              | Value:      task ':projects'
              | Creator:     tasks.addPlaceholderAction(projects)
              | Rules:
                 ⤷ copyToTaskContainer
        + properties
              | Type:       org.gradle.api.tasks.diagnostics.PropertyReportTask
              | Value:      task ':properties'
              | Creator:     tasks.addPlaceholderAction(properties)
              | Rules:
                 ⤷ copyToTaskContainer
        + tasks
              | Type:       org.gradle.api.tasks.diagnostics.TaskReportTask
              | Value:      task ':tasks'
              | Creator:     tasks.addPlaceholderAction(tasks)
              | Rules:
                 ⤷ copyToTaskContainer
        + wrapper
              | Type:       org.gradle.api.tasks.wrapper.Wrapper
              | Value:      task ':wrapper'
              | Creator:     tasks.addPlaceholderAction(wrapper)
              | Rules:
                 ⤷ copyToTaskContainer

## 69.7. Limitations and future direction

Rule based model configuration is the future of Gradle. This area is fledgling, but under very active development. Early experiments have demonstrated that this approach is more efficient, able to provide richer diagnostics and authoring assistance and is more extensible. However, there are currently many limitations.

The majority of the development to date has been focused on proving the efficacy of the approach, and building the internal rule execution engine and model graph mechanics. The user facing aspects (e.g the DSL, rule source classes) are yet to be optimized for conciseness and general usability. Likewise, many necessary configuration patterns and constructs are not yet able to be expressed via the API.

In conjunction with the addition of better syntax, a richer toolkit of configuration constructs and generally more expressive power, more tooling will be added that will enable build engineers and users alike to comprehend, modify and extend builds in new ways.

Due to the inherent nature of the rule based approach, it is more efficient at constructing the build model than today's Gradle. However, in the future Gradle will also leverage the parallelism that this approach enables both at configuration and execution time. Moreover, due to increased transparency of the model Gradle will be able to further reduce build times by caching and pre-computing the build model. Beyond improved general build performance, this will greatly improve the experience when using Gradle from tools such as IDEs.

As this area of Gradle is under active development, it will be changing rapidly. Please be sure to consult the documentation of Gradle corresponding to the version you are using and to watch for changes announced in the release notes for future versions.