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

com.blackbirdai.client.model.Expression Maven / Gradle / Ivy

package com.blackbirdai.client.model;

import com.blackbirdai.client.InvalidRequestException;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Boolean expression used for filter parameters.
 */
public abstract class Expression extends Param {

    /**
     * Returns simple boolean expression
     */
    public static Expression simple(Condition condition) {
        return new SimpleExpression(condition);
    }

    /**
     * Returns AND'd boolean expression (i.e. A and B and C and ...)
     */
    public static Expression and(List conditions) {
        return new DnfExpression(Collections.singletonList(conditions));
    }

    /**
     * Returns AND'd boolean expression (i.e. A and B and C and ...)
     */
    public static Expression and(Condition... conditions) {
        return and(Arrays.asList(conditions));
    }

    /**
     * Returns OR'd boolean expression (i.e. A or B or C or ...)
     */
    public static Expression or(List conditions) {
        return new CnfExpression(Collections.singletonList(conditions));
    }

    /**
     * Returns OR'd boolean expression (i.e. A or B or C or ...)
     */
    public static Expression or(Condition... conditions) {
        return or(Arrays.asList(conditions));
    }

    /**
     * Returns CNF (ANDs of ORs) boolean expression (i.e. (A1 or A2) and (B1 or B2) and ...)
     */
    public static Expression cnf(List> conditions) {
        return new CnfExpression(conditions);
    }

    /**
     * Returns DNF (ORs of ANDs) boolean expression (i.e. (A1 and A2) or (B1 and B2) or ...)
     */
    public static Expression dnf(List> conditions) {
        return new DnfExpression(conditions);
    }
}

class SimpleExpression extends Expression {
    private final Condition condition;

    /**
     * Internal use only. Build {@link Expression} using {@link Expression}'s static methods.
     */
    SimpleExpression(Condition condition) {
        this.condition = condition;
    }

    @Override
    StringBuilder appendQueryStr(StringBuilder builder) throws InvalidRequestException {
        return condition.appendQueryStr(builder);
    }
}

class CnfExpression extends Expression {
    private final List> conditions;

    /**
     * Internal use only. Build {@link Expression} using {@link Expression}'s static methods.
     */
    CnfExpression(List> conditions) {
        this.conditions = conditions;
    }

    @Override
    StringBuilder appendQueryStr(StringBuilder builder) throws InvalidRequestException {
        // NOTE: 'cnf' is default type, so 'type=cnf' can be skipped.
        builder.append("exp=");
        boolean comma = false;
        for (List innerConditions: conditions) {
            if (comma) builder.append(',');
            comma = true;
            boolean bar = false;
            for (Condition condition: innerConditions) {
                if (bar) builder.append('|');
                bar = true;
                condition.appendQueryStr(builder);
            }
        }
        return builder;
    }
}

class DnfExpression extends Expression {
    private final List> conditions;

    /**
     * Internal use only. Build {@link Expression} using {@link Expression}'s static methods.
     */
    DnfExpression(List> conditions) {
        this.conditions = conditions;
    }

    @Override
    StringBuilder appendQueryStr(StringBuilder builder) throws InvalidRequestException {
        builder.append("type=dnf/exp=");
        boolean bar = false;
        for (List innerConditions: conditions) {
            if (bar) builder.append('|');
            bar = true;
            boolean comma = false;
            for (Condition condition: innerConditions) {
                if (comma) builder.append(',');
                comma = true;
                condition.appendQueryStr(builder);
            }
        }
        return builder;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy