Logic 逻辑
1. 结构声明
- TS Declaration
- JSON Schema
class Logic extends Annotatable {
name: string = '';
title?: string;
description?: string;
triggerType?: string;
cron?: string;
overridable?: boolean;
transactional?: Transactional;
typeParams?: Array<TypeParam>;
params: Array<Param>;
returns: Array<Return>;
variables: Array<Variable>;
body: Array<LogicItem>;
playground: Array<LogicItem>;
}
{
"type": "object",
"properties": {
"composedBy": {
"type": "array",
"items": {
"type": "string"
}
},
"changedTime": {
"$ref": "#/definitions/long"
},
"applyAnnotations": {
"type": "array",
"items": {
"$ref": "#/definitions/ApplyAnnotation"
}
},
"name": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"triggerType": {
"type": "string"
},
"cron": {
"type": "string"
},
"overridable": {
"type": "boolean"
},
"transactional": {
"$ref": "#/definitions/Transactional"
},
"typeParams": {
"type": "array",
"items": {
"$ref": "#/definitions/TypeParam"
}
},
"params": {
"type": "array",
"items": {
"$ref": "#/definitions/Param"
}
},
"returns": {
"type": "array",
"items": {
"$ref": "#/definitions/Return"
}
},
"variables": {
"type": "array",
"items": {
"$ref": "#/definitions/Variable"
}
},
"body": {
"type": "array",
"items": {
"$ref": "#/definitions/LogicItem"
}
},
"playground": {
"type": "array",
"items": {
"$ref": "#/definitions/LogicItem"
}
}
},
"required": [
"name",
"params",
"returns",
"variables",
"body",
"playground"
],
"additionalProperties": false
}
2. 节点示例
(1) 示例
AST 如下:
- JSON
- YAML
{
"concept": "Logic",
"name": "logic1",
"title": "标题",
"description": "",
"triggerType": "",
"cron": "",
"overridable": false,
"transactional": {
"concept": "Transactional",
"enabled": false,
"propagation": "REQUIRED",
"isolation": "DEFAULT"
},
"params": [
{
"concept": "Param",
"name": "list",
"description": "",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeNamespace": "nasl.collection",
"typeKind": "generic",
"typeName": "List",
"typeArguments": [
{
"concept": "TypeAnnotation",
"typeNamespace": "nasl.core",
"typeKind": "primitive",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
],
"inferred": false,
"ruleMap": {}
}
}
],
"returns": [
{
"concept": "Return",
"name": "result",
"description": ""
}
],
"variables": [],
"body": [
{
"concept": "Start"
},
{
"concept": "Assignment",
"label": "赋值",
"left": {
"concept": "Identifier",
"namespace": "",
"name": "result"
},
"right": {
"concept": "NullLiteral"
}
},
{
"concept": "IfStatement",
"test": {
"concept": "BinaryExpression",
"left": {
"concept": "MemberExpression",
"object": {
"concept": "Identifier",
"namespace": "",
"name": "list"
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "length"
}
},
"right": {
"concept": "NumericLiteral",
"value": "0",
"typeAnnotation": {
"concept": "TypeAnnotation",
"typeKind": "primitive",
"typeNamespace": "nasl.core",
"typeName": "Long",
"inferred": false,
"ruleMap": {}
}
},
"operator": "=="
},
"consequent": [
{
"concept": "Assignment",
"label": "赋值",
"left": {
"concept": "Identifier",
"namespace": "",
"name": "result"
},
"right": {
"concept": "NullLiteral"
}
}
],
"alternate": [
{
"concept": "Assignment",
"label": "赋值",
"left": {
"concept": "Identifier",
"namespace": "",
"name": "result"
},
"right": {
"concept": "BinaryExpression",
"left": {
"concept": "CallFunction",
"label": "内置函数",
"calleeNamespace": "nasl.util",
"calleeName": "ListSum",
"typeArguments": [],
"arguments": [
{
"concept": "Argument",
"keyword": "list",
"expression": {
"concept": "Identifier",
"namespace": "",
"name": "list"
}
}
]
},
"right": {
"concept": "MemberExpression",
"object": {
"concept": "Identifier",
"namespace": "",
"name": "list"
},
"property": {
"concept": "Identifier",
"namespace": "",
"name": "length"
}
},
"operator": "/"
}
}
]
},
{
"concept": "End"
}
],
"playground": []
}
concept: Logic
name: logic1
title: 标题
description: ""
triggerType: ""
cron: ""
overridable: false
transactional:
concept: Transactional
enabled: false
propagation: REQUIRED
isolation: DEFAULT
params:
- concept: Param
name: list
description: ""
typeAnnotation:
concept: TypeAnnotation
typeNamespace: nasl.collection
typeKind: generic
typeName: List
typeArguments:
- concept: TypeAnnotation
typeNamespace: nasl.core
typeKind: primitive
typeName: Long
inferred: false
ruleMap: {}
inferred: false
ruleMap: {}
returns:
- concept: Return
name: result
description: ""
variables: []
body:
- concept: Start
- concept: Assignment
label: 赋值
left:
concept: Identifier
namespace: ""
name: result
right:
concept: NullLiteral
- concept: IfStatement
test:
concept: BinaryExpression
left:
concept: MemberExpression
object:
concept: Identifier
namespace: ""
name: list
property:
concept: Identifier
namespace: ""
name: length
right:
concept: NumericLiteral
value: "0"
typeAnnotation:
concept: TypeAnnotation
typeKind: primitive
typeNamespace: nasl.core
typeName: Long
inferred: false
ruleMap: {}
operator: ==
consequent:
- concept: Assignment
label: 赋值
left:
concept: Identifier
namespace: ""
name: result
right:
concept: NullLiteral
alternate:
- concept: Assignment
label: 赋值
left:
concept: Identifier
namespace: ""
name: result
right:
concept: BinaryExpression
left:
concept: CallFunction
label: 内置函数
calleeNamespace: nasl.util
calleeName: ListSum
typeArguments: []
arguments:
- concept: Argument
keyword: list
expression:
concept: Identifier
namespace: ""
name: list
right:
concept: MemberExpression
object:
concept: Identifier
namespace: ""
name: list
property:
concept: Identifier
namespace: ""
name: length
operator: /
- concept: End
playground: []
对应的代码如下:
- 文本化 NASL
- Natural TS
@(
title = "标题",
description = "",
triggerType = "",
cron = "",
transactional = false,
overridable = false,
)
logic logic1(list: List<Integer>) => result {
result = null
if (list.length == 0) {
result = null
} else {
result = ListSum(list) / list.length
}
end
}
function logic1(list: List<Long>) {
let result;
result = null
if (list.length == 0) {
result = null
} else {
result = nasl.util.ListSum(list) / list.length
}
return result;
}