Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format: 代码格式化 #160

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions web/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ declare module 'vue' {
ElCheckbox: typeof import('element-plus/es')['ElCheckbox']
ElCollapse: typeof import('element-plus/es')['ElCollapse']
ElCollapseItem: typeof import('element-plus/es')['ElCollapseItem']
ElColorPicker: typeof import('element-plus/es')['ElColorPicker']
ElConfigProvider: typeof import('element-plus/es')['ElConfigProvider']
ElDatePicker: typeof import('element-plus/es')['ElDatePicker']
ElDialog: typeof import('element-plus/es')['ElDialog']
ElForm: typeof import('element-plus/es')['ElForm']
ElFormItem: typeof import('element-plus/es')['ElFormItem']
ElInput: typeof import('element-plus/es')['ElInput']
ElInputNumber: typeof import('element-plus/es')['ElInputNumber']
ElMenu: typeof import('element-plus/es')['ElMenu']
ElMenuItem: typeof import('element-plus/es')['ElMenuItem']
ElOption: typeof import('element-plus/es')['ElOption']
ElPagination: typeof import('element-plus/es')['ElPagination']
ElPopover: typeof import('element-plus/es')['ElPopover']
ElRadio: typeof import('element-plus/es')['ElRadio']
ElRadioButton: typeof import('element-plus/es')['ElRadioButton']
ElRadioGroup: typeof import('element-plus/es')['ElRadioGroup']
ElRow: typeof import('element-plus/es')['ElRow']
ElSelect: typeof import('element-plus/es')['ElSelect']
ElSubMenu: typeof import('element-plus/es')['ElSubMenu']
ElSlider: typeof import('element-plus/es')['ElSlider']
ElSwitch: typeof import('element-plus/es')['ElSwitch']
ElTable: typeof import('element-plus/es')['ElTable']
ElTableColumn: typeof import('element-plus/es')['ElTableColumn']
ElTabPane: typeof import('element-plus/es')['ElTabPane']
ElTabs: typeof import('element-plus/es')['ElTabs']
ElTag: typeof import('element-plus/es')['ElTag']
ElTimePicker: typeof import('element-plus/es')['ElTimePicker']
ElTooltip: typeof import('element-plus/es')['ElTooltip']
IEpBottom: typeof import('~icons/ep/bottom')['default']
IEpCheck: typeof import('~icons/ep/check')['default']
Expand Down
13 changes: 5 additions & 8 deletions web/src/common/logicEngine/BasicType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@ export enum Operator {
Include = 'in',
Equal = 'eq',
NotEqual = 'neq',
NotInclude = 'nin',
NotInclude = 'nin'
}


export enum PrefixID {
Rule = 'r',
Condition = 'c'
}

export enum Scope {
Question = 'question',
Option = 'option'
}


export type FieldTypes = string | string[];
export type FieldTypes = string | string[]

// 定义事实对象类型
export type Fact = {
[key: string]: any;
};

[key: string]: any
}
71 changes: 35 additions & 36 deletions web/src/common/logicEngine/RuleBuild.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nanoid } from 'nanoid';
import { nanoid } from 'nanoid'
import * as yup from 'yup'
import { type FieldTypes, PrefixID, Operator, Scope } from './BasicType'

Expand All @@ -7,33 +7,33 @@ export function generateID(prefix = PrefixID.Rule) {
}
// 定义条件规则类
export class ConditionNode {
id: string = '';
public field: string = '';
public operator: Operator = Operator.Include;
id: string = ''
public field: string = ''
public operator: Operator = Operator.Include
public value: FieldTypes = []
constructor(field: string = '', operator: Operator = Operator.Include, value: FieldTypes = []) {
this.field = field;
this.operator = operator;
this.value = value;
this.field = field
this.operator = operator
this.value = value
this.id = generateID(PrefixID.Condition)
}
setField(field: string) {
this.field = field;
this.field = field
}
setOperator(operator: Operator) {
this.operator = operator;
this.operator = operator
}
setValue(value: FieldTypes) {
this.value = value;
this.value = value
}
}

export class RuleNode {
id: string = '';
id: string = ''
conditions: ConditionNode[] = []
scope: string = Scope.Question
target: string = ''
constructor(scope:string = Scope.Question, target: string = '') {
constructor(scope: string = Scope.Question, target: string = '') {
this.id = generateID(PrefixID.Rule)
this.scope = scope
this.target = target
Expand All @@ -42,44 +42,44 @@ export class RuleNode {
this.target = value
}
addCondition(condition: ConditionNode) {
this.conditions.push(condition);
this.conditions.push(condition)
}
removeCondition(id: string) {
this.conditions = this.conditions.filter(v => v.id !== id);
this.conditions = this.conditions.filter((v) => v.id !== id)
}
findCondition(conditionId: string) {
return this.conditions.find(condition => condition.id === conditionId);
return this.conditions.find((condition) => condition.id === conditionId)
}
}

export class RuleBuild {
rules: RuleNode[] = [];
static instance: RuleBuild;
rules: RuleNode[] = []
static instance: RuleBuild
constructor() {
this.rules = [];
this.rules = []
if (!RuleBuild.instance) {
RuleBuild.instance = this;
RuleBuild.instance = this
}
return RuleBuild.instance;

return RuleBuild.instance
}

// 添加条件规则到规则引擎中
addRule(rule: RuleNode) {
this.rules.push(rule);
this.rules.push(rule)
}
removeRule(ruleId: string) {
this.rules = this.rules.filter(rule => rule.id !== ruleId);
this.rules = this.rules.filter((rule) => rule.id !== ruleId)
}
findRule(ruleId: string) {
return this.rules.find(rule => rule.id === ruleId);
return this.rules.find((rule) => rule.id === ruleId)
}
toJson() {
return this.rules.map(rule => {
return this.rules.map((rule) => {
return {
target: rule.target,
scope: rule.scope,
conditions: rule.conditions.map(condition => {
conditions: rule.conditions.map((condition) => {
return {
field: condition.field,
operator: condition.operator,
Expand All @@ -91,13 +91,13 @@ export class RuleBuild {
}
fromJson(ruleConf: any) {
this.rules = []
if(ruleConf instanceof Array) {
if (ruleConf instanceof Array) {
ruleConf.forEach((rule: any) => {
const { scope, target } = rule
const ruleNode = new RuleNode(scope, target);
const ruleNode = new RuleNode(scope, target)
rule.conditions.forEach((condition: any) => {
const { field, operator, value } = condition
const conditionNode = new ConditionNode(field, operator, value);
const conditionNode = new ConditionNode(field, operator, value)
ruleNode.addCondition(conditionNode)
})
this.addRule(ruleNode)
Expand All @@ -109,28 +109,27 @@ export class RuleBuild {
return ruleSchema.validateSync(this.toJson())
}
// 实现目标选择了下拉框置灰效果
findTargetsByScope(scope: string){
return this.rules.filter(rule => rule.scope === scope).map(rule => rule.target)
findTargetsByScope(scope: string) {
return this.rules.filter((rule) => rule.scope === scope).map((rule) => rule.target)
}
// 实现前置题删除校验
findTargetsByFields(field: string) {
const nodes = this.rules.filter((rule: RuleNode) => {
const conditions = rule.conditions.filter((item: any) => {
const conditions = rule.conditions.filter((item: any) => {
return item.field === field
})
return conditions.length > 0
return conditions.length > 0
})
return nodes.map((item: any) => {
return item.target
})
}
// 根据目标题获取显示逻辑
findConditionByTarget(target: string) {
return this.rules.filter(rule=> rule.target === target).map(item => item.conditions)
return this.rules.filter((rule) => rule.target === target).map((item) => item.conditions)
}
}


export const ruleSchema = yup.array().of(
yup.object({
target: yup.string().required(),
Expand All @@ -143,4 +142,4 @@ export const ruleSchema = yup.array().of(
})
)
})
)
)
Loading