AABB
AlignmentBehavior
ArriveBehavior
AStar
BFS
BoundingSphere
BVH
BVHNode
Cell
CellSpacePartitioning
CohesionBehavior
CompositeGoal
ConvexHull
Corridor
CostTable
DFS
Dijkstra
Edge
EntityManager
EvadeBehavior
EventDispatcher
Behavior
FollowPathBehavior
FuzzyAND
FuzzyCompositeTerm
FuzzyFAIRLY
FuzzyModule
FuzzyOR
FuzzyRule
FuzzySet
FuzzyTerm
FuzzyVariable
FuzzyVERY
GameEntity
Goal
GoalEvaluator
Graph
GraphUtils
HalfEdge
HeuristicPolicyDijkstra
HeuristicPolicyEuclid
HeuristicPolicyEuclidSquared
HeuristicPolicyManhattan
InterposeBehavior
LeftSCurveFuzzySet
LeftShoulderFuzzySet
LineSegment
Logger
MathUtils
Matrix3
Matrix4
MemoryRecord
MemorySystem
MeshGeometry
MessageDispatcher
MovingEntity
NavEdge
NavMesh
NavMeshLoader
NavNode
Node
NormalDistFuzzySet
OBB
ObstacleAvoidanceBehavior
OffsetPursuitBehavior
OnPathBehavior
Path
Plane
Polygon
Polyhedron
PriorityQueue
PursuitBehavior
Quaternion
Ray
RectangleTriggerRegion
Regular
RightSCurveFuzzySet
RightShoulderFuzzySet
SAT
SeekBehavior
SeparationBehavior
SingletonFuzzySet
Smoother
SphericalTriggerRegion
State
StateMachine
SteeringBehavior
SteeringManager
Task
TaskQueue
Telegram
Think
Time
TriangularFuzzySet
Trigger
TriggerRegion
Vector3
Vehicle
Version
WanderBehavior

toJSON

toJSON方法是FuzzyRule类的一个成员方法,用于将当前规则对象转换为JSON格式的字符串。

语法

rule.toJSON()

返回值

返回值是一个JSON格式的字符串,表示当前规则对象。

示例

const rule = new FuzzyRule('distance', 'far', 'speed', 'slow');

const jsonString = rule.toJSON();

console.log(jsonString);
// 输出结果:
// '{"if":"distance","is":"far","then":"speed","is":"slow"}'

参数说明

toJSON方法不接受任何参数。

异常说明

如果调用toJSON方法时发生错误,将会抛出异常。

实现原理

toJSON方法内部通过将当前规则对象的属性逐一填入一个对象中,然后使用JSON.stringify将该对象转换为JSON格式的字符串。具体实现如下:

FuzzyRule.prototype.toJSON = function () {
  const obj = {
    if: this.antecedent.variable().name(),
    is: this.antecedent.name(),
    then: this.consequent.variable().name(),
    is: this.consequent.name()
  };

  return JSON.stringify(obj);
}

注意事项

  • toJSON方法只能用于FuzzyRule类的实例对象,不能用于类本身或其它类型的对象。
  • toJSON方法返回的JSON格式的字符串可以用于传递给其它系统或JavaScript代码中的JSON解析器进行解析。