/* * 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 _ from "lodash"; import * as React from "react"; import { useCallback, useMemo } from "react"; import { Action, BoxedFunction, BoxedFunctionKind, DmnBuiltInDataType, generateUuid, Normalized } from "../../api"; import { PopoverMenu } from "../../contextMenu/PopoverMenu"; import { boxedExpressionEditorDictionaries, BoxedExpressionEditorI18nContext, boxedExpressionEditorI18nDefaults, useBoxedExpressionEditorI18n, } from "../../i18n"; import { useBoxedExpressionEditor, useBoxedExpressionEditorDispatch } from "../../BoxedExpressionEditorContext"; import { BoxedFunctionFeel, FeelFunctionExpression } from "./FeelFunctionExpression"; import { FunctionKindSelector } from "./FunctionKindSelector"; import { BoxedFunctionJava, JavaFunctionExpression } from "./JavaFunctionExpression"; import { ParametersPopover } from "./ParametersPopover"; import { BoxedFunctionPmml, PmmlFunctionExpression } from "./PmmlFunctionExpression"; import { DMN_LATEST__tFunctionDefinition, DMN_LATEST__tFunctionKind } from "@kie-tools/dmn-marshaller"; import "./FunctionExpression.css"; import { I18nDictionariesProvider } from "@kie-tools-core/i18n/dist/react-components"; export function FunctionExpression({ isNested, parentElementId, expression: boxedFunction, }: { expression: Normalized; isNested: boolean; parentElementId: string; }) { switch (boxedFunction["@_kind"]) { case "Java": return ( } isNested={isNested} /> ); case "PMML": return ( } isNested={isNested} /> ); case "FEEL": default: return ( } isNested={isNested} parentElementId={parentElementId} /> ); } } export function useFunctionExpressionControllerCell(functionKind: Normalized) { const { setExpression } = useBoxedExpressionEditorDispatch(); const { isReadOnly } = useBoxedExpressionEditor(); const onFunctionKindSelect = useCallback( (kind: Normalized) => { setExpression({ setExpressionAction: (prev: Normalized) => { if (kind === BoxedFunctionKind.Feel) { // Do not inline this variable for type safety. See https://github.com/microsoft/TypeScript/issues/241 const retFeel: Normalized = { __$$element: "functionDefinition", "@_label": prev["@_label"], "@_id": generateUuid(), "@_kind": BoxedFunctionKind.Feel, "@_typeRef": undefined, expression: { __$$element: "literalExpression", "@_id": generateUuid(), "@_typeRef": undefined, }, formalParameter: [], }; return retFeel; } else if (kind === BoxedFunctionKind.Java) { const expressionId = generateUuid(); // Do not inline this variable for type safety. See https://github.com/microsoft/TypeScript/issues/241 const retJava: Normalized = { __$$element: "functionDefinition", "@_label": prev["@_label"], "@_id": expressionId, expression: { __$$element: "context", "@_id": generateUuid(), }, "@_kind": BoxedFunctionKind.Java, "@_typeRef": undefined, formalParameter: [], }; return retJava; } else if (kind === BoxedFunctionKind.Pmml) { // Do not inline this variable for type safety. See https://github.com/microsoft/TypeScript/issues/241 const retPmml: Normalized = { __$$element: "functionDefinition", "@_label": prev["@_label"], "@_id": generateUuid(), expression: { __$$element: "context", "@_id": generateUuid(), }, "@_kind": BoxedFunctionKind.Pmml, "@_typeRef": undefined, formalParameter: [], }; return retPmml; } else { throw new Error("Shouldn't ever reach this point."); } }, expressionChangedArgs: { action: Action.FunctionKindChanged, from: functionKind, to: kind }, }); }, [functionKind, setExpression] ); return useMemo( () => ( ), [functionKind, isReadOnly, onFunctionKindSelect] ); } export function useFunctionExpressionParametersColumnHeader( formalParameters: Normalized, isReadOnly: boolean ) { const { i18n } = useBoxedExpressionEditorI18n(); const { editorRef } = useBoxedExpressionEditor(); const expressionParametersContent = useMemo( () => (

{_.isEmpty(formalParameters) ? ( i18n.editParameters ) : ( <> {"("} {(formalParameters ?? []).map((parameter, i) => ( {parameter["@_name"]} {": "} ({parameter["@_typeRef"] ?? DmnBuiltInDataType.Undefined}) {i < (formalParameters ?? []).length - 1 && {", "}} ))} {")"} )}

), [formalParameters, i18n.editParameters] ); return useMemo(() => { return isReadOnly ? ( expressionParametersContent ) : ( editorRef.current!} className="parameters-editor-popover" minWidth="400px" body={} > {expressionParametersContent} ); }, [isReadOnly, expressionParametersContent, formalParameters, editorRef]); }