All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hisp.dhis.rules.RuleEngineContext Maven / Gradle / Ivy

package org.hisp.dhis.rules;

import org.hisp.dhis.rules.models.Rule;
import org.hisp.dhis.rules.models.RuleVariable;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Collections.unmodifiableList;

public final class RuleEngineContext
{
    private final RuleEngineIntent ruleEngineIntent;

    @Nonnull
    private final List rules;

    @Nonnull
    private final List ruleVariables;

    @Nonnull
    private final Map> supplementaryData;

    @Nonnull
    private final Map constantsValues;

    @Nullable
    private final Map dataItemStore;


    RuleEngineContext( @Nonnull List rules, @Nonnull List ruleVariables,
        Map> supplementaryData, Map constantsValues, RuleEngineIntent intent,
        Map itemStore )
    {
        this.rules = rules;
        this.ruleVariables = ruleVariables;
        this.supplementaryData = supplementaryData;
        this.constantsValues = constantsValues;
        this.ruleEngineIntent = intent;
        this.dataItemStore = itemStore;
    }

    RuleEngineContext( @Nonnull List rules, @Nonnull List ruleVariables,
       Map> supplementaryData, Map constantsValues )
    {
        this.rules = rules;
        this.ruleVariables = ruleVariables;
        this.supplementaryData = supplementaryData;
        this.constantsValues = constantsValues;
        this.ruleEngineIntent = RuleEngineIntent.EVALUATION;
        this.dataItemStore = new HashMap<>();
    }

    @Nonnull
    public static Builder builder()
    {
        return new Builder();
    }

    @Nonnull
    @Deprecated
    public static Builder builder( @Nonnull RuleExpressionEvaluator evaluator )
    {
        return new Builder( evaluator );
    }

    @Nonnull
    public List rules()
    {
        return rules;
    }

    @Nonnull
    public List ruleVariables()
    {
        return ruleVariables;
    }

    @Nonnull
    public Map> supplementaryData()
    {
        return supplementaryData;
    }

    @Nonnull
    public Map constantsValues()
    {
        return constantsValues;
    }

    @Nullable
    public Map getDataItemStore()
    {
        return dataItemStore;
    }

    @Nullable
    public RuleEngineIntent getRuleEngineIntent()
    {
        return ruleEngineIntent;
    }

    @Nonnull
    public RuleEngine.Builder toEngineBuilder()
    {
        return new RuleEngine.Builder( this );
    }

    public static class Builder
    {
        private RuleEngineIntent intent;

        @Nullable
        private List rules;

        @Nullable
        private List ruleVariables;

        @Nullable
        private Map> supplementaryData;

        @Nullable
        private Map constantsValues;

        @Nullable
        private Map itemStore;

        Builder( @Nonnull RuleExpressionEvaluator evaluator )
        {
        }

        Builder()
        {
        }

        @Nonnull
        public Builder rules( @Nonnull List rules )
        {
            if ( rules == null )
            {
                throw new IllegalArgumentException( "rules == null" );
            }

            this.rules = unmodifiableList( new ArrayList<>( rules ) );
            return this;
        }

        @Nonnull
        public Builder ruleVariables( @Nonnull List ruleVariables )
        {
            if ( ruleVariables == null )
            {
                throw new IllegalArgumentException( "ruleVariables == null" );
            }

            this.ruleVariables = unmodifiableList( new ArrayList<>( ruleVariables ) );
            return this;
        }

        @Nonnull
        public Builder ruleEngineItent( @Nullable RuleEngineIntent ruleEngineIntent )
        {
            this.intent = ruleEngineIntent;
            return this;
        }

        @Nonnull
        public Builder itemStore( @Nullable Map itemStore )
        {
            this.itemStore = itemStore;
            return this;
        }


        @Nonnull
        public Builder supplementaryData( Map> supplementaryData )
        {
            if ( supplementaryData == null )
            {
                throw new IllegalArgumentException( "supplementaryData == null" );
            }
            this.supplementaryData = supplementaryData;
            return this;
        }

        @Deprecated
        public Builder calculatedValueMap( Map> calculatedValueMap )
        {
            return this;
        }

        @Nonnull
        public Builder constantsValue( Map constantsValues )
        {
            if ( constantsValues == null )
            {
                throw new IllegalArgumentException( "constantsValue == null" );
            }
            this.constantsValues = constantsValues;
            return this;
        }

        @Nonnull
        public RuleEngineContext build()
        {
            if ( rules == null )
            {
                rules = unmodifiableList( new ArrayList() );
            }

            if ( ruleVariables == null )
            {
                ruleVariables = unmodifiableList( new ArrayList() );
            }

            if ( intent == null )
            {
                // For evaluation
                return new RuleEngineContext( rules, ruleVariables, supplementaryData, constantsValues );
            }
            else
            {
                // for description
                return new RuleEngineContext( rules, ruleVariables, supplementaryData, constantsValues,
                    intent, itemStore );
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy