{/* Licensed to the Apache Software Foundation (ASF) under one */}
{/* or more contributor license agreements. See the NOTICE file */}
{/* distributed with this work for additional information */}
{/* regarding copyright ownership. The ASF licenses this file */}
{/* to you under the Apache License, Version 2.0 (the */}
{/* "License"); you may not use this file except in compliance */}
{/* with the License. You may obtain a copy of the License at */}
{/*  */}
{/* http://www.apache.org/licenses/LICENSE-2.0 */}
{/*  */}
{/* Unless required by applicable law or agreed to in writing, */}
{/* software distributed under the License is distributed on an */}
{/* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY */}
{/* KIND, either express or implied. See the License for the */}
{/* specific language governing permissions and limitations */}
{/* under the License. */}

import { Meta, Story, Canvas, Source } from "@storybook/blocks";
import * as FunctionExpression from "./Function.stories";

<Meta title="MDX/Function" of={FunctionExpression} />

# Boxed Function

The Boxed Function expression creates a parameterized boxed expression which can be
invoked by the Boxed Invocation or directly by the textual syntax. The function has
a **Kind**, which can be FEEL, JAVA, or PMML, a parameter list, and a body.
All Business Knowledge Model (BKM) nodes are defined as Boxed Functions, and their
not possible to create a Boxed Function on the root of a decision [1].

<div style={{ display: "flex", flexDirection: "row" }}>
  <Story of={FunctionExpression.InstallmentCalculation} />
  <small>
    <br />
    <br />
    <br />
    This is an example of an "Installment Calculation" Bussiness Knowledge Model with the return "number" type. It is a
    FEEL **Kind** Boxed Function, with the "Rate", "Term" and "Amount" parameters and a FEEL expression on its body.
  </small>
</div>

This Boxed Function expression can be invoked with the textual syntax by
a Boxed Literal as follows:

<Source code={`Installment Calculation(Rate, Term, Amount)`} />

## Kind

The Boxed Functions expression has **Kind**, which is one of the three: FEEL, JAVA, or PMML.
A Boxed Function without a **Kind** is of the FEEL **Kind** [1]. By right-clicking in the
**Kind** cell, which is the top left corner cell, it's possible to tweak the Boxed Function **Kind**.

### FEEL (F)

By default, the FEEL function starts with a Boxed Literal expression on the body function.
This expression can be changed for any other boxed expression.
Also, it's possible to have any number of parameters.

### JAVA (J)

This Kind of Boxed Function is used to call externally defined functions, and its body is
a Boxed Context. It's possible to have any number of parameters.

### PMML (P)

This Kind of Boxed Function is used to call externally defined functions, and its body is
a Boxed Context. It's possible to have any number of parameters.

## Header

If the Boxed Function is the root expression of a Business Knowledge Model (BKM) node, the header row will be split into two rows.
The first will represent the BKM name and type, and the second is the function parameters.
Now, if the Boxed Function is a nested expression, it will have only one row, which will be the function parameters.

> #### BKM header
>
> This row will be a single cell that has the decision name and its type, and by right-clicking on it a context menu will open enabling you to edit its values.
>
> #### Function parameters
>
> This row has a cell for the function parameters, and by right-clicking on it, it's possible to add, edit, or delete parameter names and types.

## Body

The function Body depends on the function Kind, but the Body is responsible for to return
the value for who invoked the function.

## Controls

### Parameters

Left-clicking on a parameter cell will open the "Rows" context menu.
It's possible to insert above, insert below, insert multiples, delete, and reset the parameter.

Another way to add rows is by hovering the mouse on a parameter cell, which will make appear a plus sign
on the top (insert above) or on the bottom (insert below) of the cell, the position of the plus sign will
depend on the mouse position. Right-clicking on it will add a new parameter above or below the actual parameter.

## Examples

<div style={{ display: "flex", flexDirection: "column" }}>
  <div style={{ display: "flex", flexDirection: "row" }}>
    <Story of={FunctionExpression.Base} />
    <small>
      <br />
      <br />
      An empty Boxed Function expression.
    </small>
  </div>

  <div style={{ display: "flex", flexDirection: "row" }}>
    <Story of={FunctionExpression.Nested} />
    <small>
      <br />
      <br />
      An empty Boxed Function expression nested on a Boxed Context expression.
    </small>
  </div>
</div>

## Reference

[1] [DMN 1.4 Spec](https://www.omg.org/spec/DMN/1.4/Beta1/PDF#page=103)
