Yuka js库的FuzzyModule类的fromJSON方法,用于将JSON格式的模糊控制系统描述转换为FuzzyModule实例。
FuzzyModule.fromJSON(stringifiedJsonDescriptor)
stringifiedJsonDescriptor
:类型为String,是一个JSON格式的模糊控制系统描述字符串。返回一个FuzzyModule实例,代表被转换的模糊控制系统。
该方法是FuzzyModule类中的静态方法,用于从JSON字符串中解析描述模糊控制系统的数据,并返回一个相应的FuzzyModule实例。
JSON格式的模糊控制系统描述应包含以下属性:
variables
:所有变量的数组,每个变量应该包含以下属性:
name
:变量的名称minValue
:变量的最小值maxValue
:变量的最大值terms
:变量的词项,是一个对象,其键为词项名称,值为Term实例的JSON描述rules
:模糊规则的数组,每个规则应包含以下属性:
antecedent
:前提条件,是一个对象,由变量名称和一个字符串表示的词项组成,例如:{"humidity": "high", "temperature": "low"}
consequent
:结论,是由一个变量名称和一个字符串表示的词项组成的对象,例如:{"flow": "medium"}
weight
:权重,是一个数字,用于调整规则的优先级JSON格式的Term描述应包含以下属性:
type
:Term的类型,例如:Triangle、LeftShoulder、RightShoulder、Trapezoid、Singletonparams
:描述Term参数的对象,其属性根据Term类型而定以下是一个示例JSON字符串:
{
"variables": [
{
"name": "humidity",
"minValue": 0,
"maxValue": 100,
"terms": {
"low": {
"type": "Triangle",
"params": {
"left": 0,
"top": 25,
"right": 50
}
},
"medium": {
"type": "Triangle",
"params": {
"left": 25,
"top": 50,
"right": 75
}
},
"high": {
"type": "Triangle",
"params": {
"left": 50,
"top": 75,
"right": 100
}
}
}
},
{
"name": "temperature",
"minValue": 10,
"maxValue": 40,
"terms": {
"low": {
"type": "Triangle",
"params": {
"left": 10,
"top": 15,
"right": 20
}
},
"medium": {
"type": "Triangle",
"params": {
"left": 15,
"top": 25,
"right": 35
}
},
"high": {
"type": "Triangle",
"params": {
"left": 30,
"top": 40,
"right": 40
}
}
}
},
{
"name": "flow",
"minValue": 0,
"maxValue": 10,
"terms": {
"low": {
"type": "Triangle",
"params": {
"left": 0,
"top": 3,
"right": 6
}
},
"medium": {
"type": "Triangle",
"params": {
"left": 3,
"top": 5,
"right": 8
}
},
"high": {
"type": "Triangle",
"params": {
"left": 6,
"top": 10,
"right": 10
}
}
}
}
],
"rules": [
{
"antecedent": {
"humidity": "low",
"temperature": "low"
},
"consequent": {
"flow": "low"
},
"weight": 1
},
{
"antecedent": {
"humidity": "medium",
"temperature": "low"
},
"consequent": {
"flow": "medium"
},
"weight": 1
},
{
"antecedent": {
"humidity": "high",
"temperature": "low"
},
"consequent": {
"flow": "high"
},
"weight": 1
},
{
"antecedent": {
"humidity": "low",
"temperature": "medium"
},
"consequent": {
"flow": "medium"
},
"weight": 1
},
{
"antecedent": {
"humidity": "medium",
"temperature": "medium"
},
"consequent": {
"flow": "high"
},
"weight": 1
},
{
"antecedent": {
"humidity": "high",
"temperature": "medium"
},
"consequent": {
"flow": "high"
},
"weight": 1
},
{
"antecedent": {
"humidity": "low",
"temperature": "high"
},
"consequent": {
"flow": "low"
},
"weight": 1
},
{
"antecedent": {
"humidity": "medium",
"temperature": "high"
},
"consequent": {
"flow": "low"
},
"weight": 1
},
{
"antecedent": {
"humidity": "high",
"temperature": "high"
},
"consequent": {
"flow": "medium"
},
"weight": 1
}
]
}
const jsonDescriptor = `{
"variables": [
{
"name": "humidity",
"minValue": 0,
"maxValue": 100,
"terms": {
"low": {
"type": "Triangle",
"params": {
"left": 0,
"top": 25,
"right": 50
}
},
"medium": {
"type": "Triangle",
"params": {
"left": 25,
"top": 50,
"right": 75
}
},
"high": {
"type": "Triangle",
"params": {
"left": 50,
"top": 75,
"right": 100
}
}
}
},
{
"name": "temperature",
"minValue": 10,
"maxValue": 40,
"terms": {
"low": {
"type": "Triangle",
"params": {
"left": 10,
"top": 15,
"right": 20
}
},
"medium": {
"type": "Triangle",
"params": {
"left": 15,
"top": 25,
"right": 35
}
},
"high": {
"type": "Triangle",
"params": {
"left": 30,
"top": 40,
"right": 40
}
}
}
},
{
"name": "flow",
"minValue": 0,
"maxValue": 10,
"terms": {
"low": {
"type": "Triangle",
"params": {
"left": 0,
"top": 3,
"right": 6
}
},
"medium": {
"type": "Triangle",
"params": {
"left": 3,
"top": 5,
"right": 8
}
},
"high": {
"type": "Triangle",
"params": {
"left": 6,
"top": 10,
"right": 10
}
}
}
}
],
"rules": [
{
"antecedent": {
"humidity": "low",
"temperature": "low"
},
"consequent": {
"flow": "low"
},
"weight": 1
},
{
"antecedent": {
"humidity": "medium",
"temperature": "low"
},
"consequent": {
"flow": "medium"
},
"weight": 1
},
{
"antecedent": {
"humidity": "high",
"temperature": "low"
},
"consequent": {
"flow": "high"
},
"weight": 1
},
{
"antecedent": {
"humidity": "low",
"temperature": "medium"
},
"consequent": {
"flow": "medium"
},
"weight": 1
},
{
"antecedent": {
"humidity": "medium",
"temperature": "medium"
},
"consequent": {
"flow": "high"
},
"weight": 1
},
{
"antecedent": {
"humidity": "high",
"temperature": "medium"
},
"consequent": {
"flow": "high"
},
"weight": 1
},
{
"antecedent": {
"humidity": "low",
"temperature": "high"
},
"consequent": {
"flow": "low"
},
"weight": 1
},
{
"antecedent": {
"humidity": "medium",
"temperature": "high"
},
"consequent": {
"flow": "low"
},
"weight": 1
},
{
"antecedent": {
"humidity": "high",
"temperature": "high"
},
"consequent": {
"flow": "medium"
},
"weight": 1
}
]
}`
const fuzzyModule = FuzzyModule.fromJSON(jsonDescriptor)
如果给定的JSON字符串无效或描述不完整的模糊控制系统,则可能抛出异常。