-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KOGITO-7671: [yard Editor] Deserialize YAML/JSON file to populate UI (#…
…1288) * KOGITO-7671: First draft * KOGITO-7671: JSON / YAML deserialization added. Populated data in General and Input data * KOGITO-7671: JSON / YAML deserialization added. Populated data in General and Input data * KOGITO-7671: JSON / YAML deserialization added. Populated data in General and Input data * KOGITO-7671: JSON / YAML deserialization added. Populated data in General and Input data and Element data * KOGITO-7671: JSON / YAML deserialization added. Populated data in General and Input data * Updated according to last repo changes * Revert temporary pnpm version check * Updated `pnpm-lock.yaml` file * Revert wrong files
- Loading branch information
Showing
18 changed files
with
416 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2022 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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 { Input } from "../model"; | ||
|
||
export const dataTypes = [ | ||
{ typeRef: "Undefined", name: "<Undefined>", isCustom: false }, | ||
{ typeRef: "Any", name: "Any", isCustom: false }, | ||
{ typeRef: "Boolean", name: "boolean", isCustom: false }, | ||
{ typeRef: "Context", name: "context", isCustom: false }, | ||
{ typeRef: "Date", name: "date", isCustom: false }, | ||
{ typeRef: "DateTime", name: "date and time", isCustom: false }, | ||
{ typeRef: "DateTimeDuration", name: "days and time duration", isCustom: false }, | ||
{ typeRef: "Number", name: "number", isCustom: false }, | ||
{ typeRef: "String", name: "string", isCustom: false }, | ||
{ typeRef: "Time", name: "time", isCustom: false }, | ||
{ typeRef: "YearsMonthsDuration", name: "years and months duration", isCustom: false }, | ||
]; | ||
|
||
export function generateDecisionTypes(input: Input[]) { | ||
const types: { typeRef: string; name: string; isCustom: boolean }[] = []; | ||
dataTypes.forEach((val) => types.push(Object.assign({}, val))); | ||
input.forEach((input) => { | ||
types.push({ typeRef: input.name, name: input.name, isCustom: true }); | ||
}); | ||
|
||
return types; | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/yard-editor/src/decision/DecisionExpressionDefinitions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2022 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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 { Element } from "../model"; | ||
import { DecisionTableProps, LogicType } from "@kie-tools/boxed-expression-component/dist/api"; | ||
import { | ||
Clause, | ||
DataType, | ||
DecisionTableRule, | ||
ExpressionProps, | ||
LiteralExpressionProps, | ||
} from "@kie-tools/boxed-expression-component/dist/api"; | ||
|
||
export function generateDecisionExpressionDefinition(element: Element): ExpressionProps { | ||
const decisionType = element.logic.type; | ||
|
||
switch (decisionType) { | ||
case "DecisionTable": | ||
const inputClauses = element.logic.inputs?.map((input) => generateClause(input)); | ||
const outputClauses = element.logic.outputComponents?.map((output) => generateClause(output)); | ||
return { | ||
annotations: [{ name: "annotation-1a", id: "111" }], | ||
dataType: DataType.Any, | ||
input: inputClauses, | ||
logicType: LogicType.DecisionTable, | ||
name: element.name, | ||
output: outputClauses, | ||
rules: generateDecisionTableRule(element.logic.rules!, element.logic.inputs?.length), | ||
} as DecisionTableProps; | ||
case "LiteralExpression": | ||
return { | ||
dataType: DataType.Any, | ||
name: element.name, | ||
logicType: LogicType.LiteralExpression, | ||
content: element.logic.expression, | ||
} as LiteralExpressionProps; | ||
default: | ||
return {}; | ||
} | ||
} | ||
|
||
function generateClause(clauseName: string): Clause { | ||
return { | ||
id: clauseName + Math.floor(Math.random() * 6) + 1, | ||
name: clauseName, | ||
dataType: DataType.Any, | ||
} as Clause; | ||
} | ||
|
||
function generateDecisionTableRule(rules: string[][], inputLength: number | undefined): DecisionTableRule[] { | ||
const l = inputLength ? inputLength : 0; | ||
const decisionTableRules: DecisionTableRule[] = []; | ||
let index = 0; | ||
rules.map((rule) => { | ||
decisionTableRules.push({ | ||
annotationEntries: [""], | ||
id: index.toString(), | ||
inputEntries: rule.slice(0, l), | ||
outputEntries: rule.slice(l), | ||
}); | ||
index++; | ||
}); | ||
|
||
return decisionTableRules; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2022 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
export * from "./DecisionDataTypes"; | ||
export * from "./DecisionExpressionDefinitions"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2022 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
export class YardModel { | ||
expressionLang: string; | ||
kind: string; | ||
inputs: Input[]; | ||
elements: Element[]; | ||
name: string; | ||
specVersion: string; | ||
|
||
constructor(model: any) { | ||
Object.assign(this, model); | ||
} | ||
} | ||
|
||
export class Element { | ||
logic: Logic; | ||
name: string; | ||
requirements: string[]; | ||
type: string; | ||
} | ||
|
||
export class Input { | ||
name: string; | ||
type: string; | ||
} | ||
|
||
export class Logic { | ||
inputs?: string[]; | ||
expression?: string; | ||
outputComponents?: string[]; | ||
rules?: string[][]; | ||
type: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2022 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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 * as yaml from "js-yaml"; | ||
import { YardModel } from "./YardModel"; | ||
|
||
/** | ||
* It deserializes content string as YardModel object | ||
* @param {string} content The JSON or YAML yard source to deserialize | ||
* @returns {YardModel} Resulting object representation of yard model | ||
*/ | ||
export function deserialize(content: string): YardModel { | ||
try { | ||
const model = yaml.load(content); | ||
return new YardModel(model); | ||
} catch (e) { | ||
throw new Error("Error during deserialize phase of yard model" + e.toString()); | ||
} | ||
} |
Oops, something went wrong.