Skip to content

Boolean

For all boolean operators, the collections passed as operands are first evaluated as Booleans. The operators then use three-valued logic to propagate empty operands.

And

Bases: FHIRPath

A representation of the FHIRPath and boolean logic operator.

Attributes:

Name Type Description
left FHIRPath | FHIRPathCollection

Left operand.

right FHIRPath | FHIRPathCollection

Right operand.

Source code in fhircraft/fhir/path/engine/boolean.py
class And(FHIRPath):
    """
    A representation of the FHIRPath [`and`](https://hl7.org/fhirpath/N1/#and) boolean logic operator.

    Attributes:
        left (FHIRPath | FHIRPathCollection): Left operand.
        right (FHIRPath | FHIRPathCollection): Right operand.
    """

    def __init__(
        self, left: FHIRPath | FHIRPathCollection, right: FHIRPath | FHIRPathCollection
    ):
        self.left = left
        self.right = right

    def evaluate(
        self, collection: FHIRPathCollection, create=False
    ) -> FHIRPathCollection:
        """
        Returns `True` if both operands evaluate to `True`, `False` if either operand evaluates to `False`, and the empty collection (`[]`) otherwise.

        Args:
            collection (FHIRPathCollection): The input collection.

        Returns:
            FHIRPathCollection: The output collection
        """
        left_boolean, right_boolean = _evaluate_boolean_expressions(
            self.left, self.right, collection, create=create
        )
        if left_boolean is None:
            if right_boolean is True:
                return []
            elif right_boolean is False:
                return [FHIRPathCollectionItem.wrap(False)]
            elif right_boolean is None:
                return []
        elif right_boolean is None:
            if left_boolean is True:
                return []
            elif left_boolean is False:
                return [FHIRPathCollectionItem.wrap(False)]
            elif left_boolean is None:
                return []
        return [FHIRPathCollectionItem.wrap(left_boolean and right_boolean)]

    def __str__(self):
        return f"{self.__class__.__name__.lower()}({self.left.__str__(), self.right.__str__()})"

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.left.__repr__(), self.right.__repr__()})"
        )

    def __eq__(self, other):
        return (
            isinstance(other, And)
            and other.left == self.left
            and other.right == self.right
        )

    def __hash__(self):
        return hash((self.left, self.right))

evaluate(collection, create=False)

Returns True if both operands evaluate to True, False if either operand evaluates to False, and the empty collection ([]) otherwise.

Parameters:

Name Type Description Default
collection FHIRPathCollection

The input collection.

required

Returns:

Name Type Description
FHIRPathCollection FHIRPathCollection

The output collection

Source code in fhircraft/fhir/path/engine/boolean.py
def evaluate(
    self, collection: FHIRPathCollection, create=False
) -> FHIRPathCollection:
    """
    Returns `True` if both operands evaluate to `True`, `False` if either operand evaluates to `False`, and the empty collection (`[]`) otherwise.

    Args:
        collection (FHIRPathCollection): The input collection.

    Returns:
        FHIRPathCollection: The output collection
    """
    left_boolean, right_boolean = _evaluate_boolean_expressions(
        self.left, self.right, collection, create=create
    )
    if left_boolean is None:
        if right_boolean is True:
            return []
        elif right_boolean is False:
            return [FHIRPathCollectionItem.wrap(False)]
        elif right_boolean is None:
            return []
    elif right_boolean is None:
        if left_boolean is True:
            return []
        elif left_boolean is False:
            return [FHIRPathCollectionItem.wrap(False)]
        elif left_boolean is None:
            return []
    return [FHIRPathCollectionItem.wrap(left_boolean and right_boolean)]

Implies

Bases: FHIRPath

A representation of the FHIRPath implies boolean logic operator.

Attributes:

Name Type Description
left FHIRPath | FHIRPathCollection

Left operand.

right FHIRPath | FHIRPathCollection

Right operand.

Source code in fhircraft/fhir/path/engine/boolean.py
class Implies(FHIRPath):
    """
    A representation of the FHIRPath [`implies`](https://hl7.org/fhirpath/N1/#implies) boolean logic operator.

    Attributes:
        left (FHIRPath | FHIRPathCollection): Left operand.
        right (FHIRPath | FHIRPathCollection): Right operand.
    """

    def __init__(
        self, left: FHIRPath | FHIRPathCollection, right: FHIRPath | FHIRPathCollection
    ):
        self.left = left
        self.right = right

    def evaluate(
        self, collection: FHIRPathCollection, create=False
    ) -> FHIRPathCollection:
        """
        If the left operand evaluates to `True`, this operator returns the boolean evaluation of the right operand. If the
        left operand evaluates to `False`, this operator returns `True`. Otherwise, this operator returns `True` if the right
        operand evaluates to `True`, and the empty collection (`[]`) otherwise.

        Args:
            collection (FHIRPathCollection): The input collection.

        Returns:
            FHIRPathCollection: The output collection
        """
        left_boolean, right_boolean = _evaluate_boolean_expressions(
            self.left, self.right, collection, create=create
        )
        if left_boolean is None:
            if right_boolean is True:
                return [FHIRPathCollectionItem.wrap(True)]
            elif right_boolean is False:
                return []
            elif right_boolean is None:
                return []
        elif right_boolean is None:
            if left_boolean is True:
                return []
            elif left_boolean is False:
                return [FHIRPathCollectionItem.wrap(True)]
            elif left_boolean is None:
                return []
        elif left_boolean is True:
            if right_boolean is True:
                return [FHIRPathCollectionItem.wrap(True)]
            elif right_boolean is False:
                return [FHIRPathCollectionItem.wrap(False)]
        elif right_boolean is True:
            if left_boolean is True:
                return [FHIRPathCollectionItem.wrap(True)]
            elif left_boolean is False:
                return [FHIRPathCollectionItem.wrap(True)]
        elif right_boolean is False and left_boolean is False:
            return [FHIRPathCollectionItem.wrap(True)]
        return []

    def __str__(self):
        return f"{self.__class__.__name__.lower()}({self.left.__str__(), self.right.__str__()})"

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.left.__repr__(), self.right.__repr__()})"
        )

    def __eq__(self, other):
        return (
            isinstance(other, Implies)
            and other.left == self.left
            and other.right == self.right
        )

    def __hash__(self):
        return hash((self.left, self.right))

evaluate(collection, create=False)

If the left operand evaluates to True, this operator returns the boolean evaluation of the right operand. If the left operand evaluates to False, this operator returns True. Otherwise, this operator returns True if the right operand evaluates to True, and the empty collection ([]) otherwise.

Parameters:

Name Type Description Default
collection FHIRPathCollection

The input collection.

required

Returns:

Name Type Description
FHIRPathCollection FHIRPathCollection

The output collection

Source code in fhircraft/fhir/path/engine/boolean.py
def evaluate(
    self, collection: FHIRPathCollection, create=False
) -> FHIRPathCollection:
    """
    If the left operand evaluates to `True`, this operator returns the boolean evaluation of the right operand. If the
    left operand evaluates to `False`, this operator returns `True`. Otherwise, this operator returns `True` if the right
    operand evaluates to `True`, and the empty collection (`[]`) otherwise.

    Args:
        collection (FHIRPathCollection): The input collection.

    Returns:
        FHIRPathCollection: The output collection
    """
    left_boolean, right_boolean = _evaluate_boolean_expressions(
        self.left, self.right, collection, create=create
    )
    if left_boolean is None:
        if right_boolean is True:
            return [FHIRPathCollectionItem.wrap(True)]
        elif right_boolean is False:
            return []
        elif right_boolean is None:
            return []
    elif right_boolean is None:
        if left_boolean is True:
            return []
        elif left_boolean is False:
            return [FHIRPathCollectionItem.wrap(True)]
        elif left_boolean is None:
            return []
    elif left_boolean is True:
        if right_boolean is True:
            return [FHIRPathCollectionItem.wrap(True)]
        elif right_boolean is False:
            return [FHIRPathCollectionItem.wrap(False)]
    elif right_boolean is True:
        if left_boolean is True:
            return [FHIRPathCollectionItem.wrap(True)]
        elif left_boolean is False:
            return [FHIRPathCollectionItem.wrap(True)]
    elif right_boolean is False and left_boolean is False:
        return [FHIRPathCollectionItem.wrap(True)]
    return []

Not

Bases: FHIRPathFunction

A representation of the FHIRPath not boolean logic function.

Source code in fhircraft/fhir/path/engine/boolean.py
class Not(FHIRPathFunction):
    """
    A representation of the FHIRPath [`not`](https://hl7.org/fhirpath/N1/#not) boolean logic function.
    """

    def evaluate(
        self, collection: FHIRPathCollection, create=False
    ) -> FHIRPathCollection:
        """
        Returns `True` if the input collection evaluates to `False`, and `False` if it evaluates to `True`. Otherwise, the result is empty (`[]`):


        Args:
            collection (FHIRPathCollection): The input collection.

        Returns:
            FHIRPathCollection: The output collection
        """
        if len(collection) > 1:
            raise FHIRPathRuntimeError(
                "Cannot assert Not() for a collection that has more than one item."
            )
        elif len(collection) == 0:
            return []
        else:
            boolean = bool(collection[0].value)
            return [FHIRPathCollectionItem.wrap(not boolean)]

evaluate(collection, create=False)

Returns True if the input collection evaluates to False, and False if it evaluates to True. Otherwise, the result is empty ([]):

Parameters:

Name Type Description Default
collection FHIRPathCollection

The input collection.

required

Returns:

Name Type Description
FHIRPathCollection FHIRPathCollection

The output collection

Source code in fhircraft/fhir/path/engine/boolean.py
def evaluate(
    self, collection: FHIRPathCollection, create=False
) -> FHIRPathCollection:
    """
    Returns `True` if the input collection evaluates to `False`, and `False` if it evaluates to `True`. Otherwise, the result is empty (`[]`):


    Args:
        collection (FHIRPathCollection): The input collection.

    Returns:
        FHIRPathCollection: The output collection
    """
    if len(collection) > 1:
        raise FHIRPathRuntimeError(
            "Cannot assert Not() for a collection that has more than one item."
        )
    elif len(collection) == 0:
        return []
    else:
        boolean = bool(collection[0].value)
        return [FHIRPathCollectionItem.wrap(not boolean)]

Or

Bases: FHIRPath

A representation of the FHIRPath or boolean logic operator.

Attributes:

Name Type Description
left FHIRPath | FHIRPathCollection

Left operand.

right FHIRPath | FHIRPathCollection

Right operand.

Source code in fhircraft/fhir/path/engine/boolean.py
class Or(FHIRPath):
    """
    A representation of the FHIRPath [`or`](https://hl7.org/fhirpath/N1/#or) boolean logic operator.

    Attributes:
        left (FHIRPath | FHIRPathCollection): Left operand.
        right (FHIRPath | FHIRPathCollection): Right operand.
    """

    def __init__(
        self, left: FHIRPath | FHIRPathCollection, right: FHIRPath | FHIRPathCollection
    ):
        self.left = left
        self.right = right

    def evaluate(
        self, collection: FHIRPathCollection, create=False
    ) -> FHIRPathCollection:
        """
        Returns `False` if both operands evaluate to `False`, `True` if either operand evaluates to `True`, and empty (`[]`) otherwise.

        Args:
            collection (FHIRPathCollection): The input collection.

        Returns:
            FHIRPathCollection: The output collection
        """
        left_boolean, right_boolean = _evaluate_boolean_expressions(
            self.left, self.right, collection, create=create
        )
        if left_boolean is None:
            if right_boolean is True:
                return [FHIRPathCollectionItem.wrap(True)]
            elif right_boolean is False:
                return []
            elif right_boolean is None:
                return []
        elif right_boolean is None:
            if left_boolean is True:
                return [FHIRPathCollectionItem.wrap(True)]
            elif left_boolean is False:
                return []
            elif left_boolean is None:
                return []
        return [FHIRPathCollectionItem.wrap(left_boolean or right_boolean)]

    def __str__(self):
        return f"{self.__class__.__name__.lower()}({self.left.__str__(), self.right.__str__()})"

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.left.__repr__(), self.right.__repr__()})"
        )

    def __eq__(self, other):
        return (
            isinstance(other, Or)
            and other.left == self.left
            and other.right == self.right
        )

    def __hash__(self):
        return hash((self.left, self.right))

evaluate(collection, create=False)

Returns False if both operands evaluate to False, True if either operand evaluates to True, and empty ([]) otherwise.

Parameters:

Name Type Description Default
collection FHIRPathCollection

The input collection.

required

Returns:

Name Type Description
FHIRPathCollection FHIRPathCollection

The output collection

Source code in fhircraft/fhir/path/engine/boolean.py
def evaluate(
    self, collection: FHIRPathCollection, create=False
) -> FHIRPathCollection:
    """
    Returns `False` if both operands evaluate to `False`, `True` if either operand evaluates to `True`, and empty (`[]`) otherwise.

    Args:
        collection (FHIRPathCollection): The input collection.

    Returns:
        FHIRPathCollection: The output collection
    """
    left_boolean, right_boolean = _evaluate_boolean_expressions(
        self.left, self.right, collection, create=create
    )
    if left_boolean is None:
        if right_boolean is True:
            return [FHIRPathCollectionItem.wrap(True)]
        elif right_boolean is False:
            return []
        elif right_boolean is None:
            return []
    elif right_boolean is None:
        if left_boolean is True:
            return [FHIRPathCollectionItem.wrap(True)]
        elif left_boolean is False:
            return []
        elif left_boolean is None:
            return []
    return [FHIRPathCollectionItem.wrap(left_boolean or right_boolean)]

Xor

Bases: FHIRPath

A representation of the FHIRPath xor boolean logic operator.

Attributes:

Name Type Description
left FHIRPath | FHIRPathCollection

Left operand.

right FHIRPath | FHIRPathCollection

Right operand.

Source code in fhircraft/fhir/path/engine/boolean.py
class Xor(FHIRPath):
    """
    A representation of the FHIRPath [`xor`](https://hl7.org/fhirpath/N1/#xor) boolean logic operator.

    Attributes:
        left (FHIRPath | FHIRPathCollection): Left operand.
        right (FHIRPath | FHIRPathCollection): Right operand.
    """

    def __init__(
        self, left: FHIRPath | FHIRPathCollection, right: FHIRPath | FHIRPathCollection
    ):
        self.left = left
        self.right = right

    def evaluate(
        self, collection: FHIRPathCollection, create=False
    ) -> FHIRPathCollection:
        """
        Returns `True` if exactly one of the operands evaluates to `True`, `False` if either both operands evaluate to `True` or both operands evaluate to `False`, and the empty collection (`[]`) otherwise.

        Args:
            collection (FHIRPathCollection): The input collection.

        Returns:
            FHIRPathCollection: The output collection
        """
        left_boolean, right_boolean = _evaluate_boolean_expressions(
            self.left, self.right, collection, create=create
        )
        if left_boolean is None or right_boolean is None:
            return []
        return [FHIRPathCollectionItem.wrap(left_boolean ^ right_boolean)]

    def __str__(self):
        return f"{self.__class__.__name__.lower()}({self.left.__str__(), self.right.__str__()})"

    def __repr__(self):
        return (
            f"{self.__class__.__name__}({self.left.__repr__(), self.right.__repr__()})"
        )

    def __eq__(self, other):
        return (
            isinstance(other, Xor)
            and other.left == self.left
            and other.right == self.right
        )

    def __hash__(self):
        return hash((self.left, self.right))

evaluate(collection, create=False)

Returns True if exactly one of the operands evaluates to True, False if either both operands evaluate to True or both operands evaluate to False, and the empty collection ([]) otherwise.

Parameters:

Name Type Description Default
collection FHIRPathCollection

The input collection.

required

Returns:

Name Type Description
FHIRPathCollection FHIRPathCollection

The output collection

Source code in fhircraft/fhir/path/engine/boolean.py
def evaluate(
    self, collection: FHIRPathCollection, create=False
) -> FHIRPathCollection:
    """
    Returns `True` if exactly one of the operands evaluates to `True`, `False` if either both operands evaluate to `True` or both operands evaluate to `False`, and the empty collection (`[]`) otherwise.

    Args:
        collection (FHIRPathCollection): The input collection.

    Returns:
        FHIRPathCollection: The output collection
    """
    left_boolean, right_boolean = _evaluate_boolean_expressions(
        self.left, self.right, collection, create=create
    )
    if left_boolean is None or right_boolean is None:
        return []
    return [FHIRPathCollectionItem.wrap(left_boolean ^ right_boolean)]