第175章 GoalService
175.1 提出背景
在ICAI认知体系中,Need(需求) 是认知活动的重要起点。
前面的认知链可以表示为:
State
↓
Need
↓
Goal
↓
Capability
↓
Method
↓
Decision
↓
Behavior
↓
Action
↓
Execution
↓
Result
其中,Need回答:
当前为什么需要采取行动?
Goal回答:
希望最终达到什么状态?
二者不能混淆。
例如:
Need:
Robot需要取得Cup。
Goal:
Cup进入Robot的Gripper。
再例如:
Need:
系统需要恢复运行。
Goal:
System State = Ready。
因此:
Need≠GoalNeed \neq Goal
Need提供驱动因素,Goal提供目标状态。
如果没有Goal,后续Capability、Method、Decision和Behavior就缺乏明确的判断终点。
因此建立:
GoalService(目标服务)
负责:
需求处理
↓
目标建立
↓
目标更新
↓
目标状态
GoalService的核心职责是把需求转化成明确、结构化、可计算、可验证的目标对象。
175.2 Goal定义
Goal(目标) 是系统针对某一Need,希望通过认知与执行过程最终达到的预期状态、结果或条件。
基础模型:
G=(ID,N,T,C,P,S)G=(ID,N,T,C,P,S)
其中:
- IDID:Goal Identity;
- NN:来源Need;
- TT:Target,目标对象;
- CC:Condition,目标条件;
- PP:Priority,目标优先级;
- SS:Goal State。
例如:
Goal:
Robot obtains Cup
可以进一步结构化:
Target Object = Cup
Target State = Held
Holder = Robot
Priority = High
因此:
Goal=Target+Condition+Priority+StateGoal = Target + Condition + Priority + State
Goal不是一句自然语言描述,而是一个能够参与后续认知计算的Domain Object。
175.3 GoalService定义
GoalService 是负责Goal生命周期和应用过程协调的Service。
基本模型:
GS=(N,G,U,S)GS=(N,G,U,S)
其中:
- NN:Need Processing;
- GG:Goal Creation;
- UU:Goal Update;
- SS:Goal State Management。
完整流程:
Need
↓
Need Processing
↓
Goal Definition
↓
Goal Creation
↓
Goal State
↓
Capability / Method / Decision
↓
Execution
↓
Result
↓
Goal Verification
↓
Goal State Update
因此GoalService不是执行Service。
它不直接:
执行Action
运行Behavior
执行Method
而是维护:
系统当前正在追求什么,以及这个目标目前处于什么状态。
175.4 Need与Goal的区别
Need是需求。
Goal是需求经过结构化之后形成的目标。
例如:
Need:
我要移动到Room-B。
转化为:
Goal:
Robot Location = Room-B
因此:
Need→GoalNeed \rightarrow Goal
但:
Need≠GoalNeed \neq Goal
Need可以具有模糊性:
需要移动
需要获取
需要恢复
需要处理
Goal必须逐步明确:
Target
Condition
Expected State
Priority
因此:
Goal=Operationalized NeedGoal = Operationalized\ Need
即:
Goal是经过操作化、结构化后的Need。
175.5 需求处理
175.5.1 需求处理定义
需求处理(Need Processing) 是对当前Need进行识别、分类、约束分析和目标化处理的过程。
基本流程:
Need
↓
Need Validation
↓
Need Classification
↓
Need Condition
↓
Goal Candidate
例如:
Need:
Robot需要充电
处理后:
Target = Robot
Required State = Charging
Condition = Battery < Threshold
形成Goal Candidate:
Robot Battery → Charging
175.6 需求与当前State
Need通常来自State。
例如:
Battery = 10%
系统规则:
Battery < 20%
→
Need Charging
于是:
State→Need→GoalState \rightarrow Need \rightarrow Goal
形成:
Battery Low
↓
Need Charging
↓
Goal Battery State = Charging
因此GoalService需要能够读取当前State。
但GoalService不应该自己负责State计算。
它可以依赖:
StateService
获得当前状态。
175.7 需求处理的基本结构
可以定义:
N=(I,T,C,P,S)N=(I,T,C,P,S)
其中:
- II:Need Identity;
- TT:Need Type;
- CC:Need Condition;
- PP:Need Priority;
- SS:Need State。
GoalService根据Need生成Goal:
N→ProcessGN \xrightarrow{Process} G
例如:
Need Type = Recovery
Condition = System Failed
Priority = High
产生:
Goal Type = Recovery
Target State = Ready
Priority = High
175.8 需求类型
Need可以具有不同Type,例如:
Acquire
Move
Maintain
Repair
Recover
Protect
Learn
Monitor
Communicate
Complete
例如:
Need = Acquire Object
目标可以是:
Object State = Obtained
又例如:
Need = Recover System
目标可以是:
System State = Ready
因此Need Type会影响Goal结构。
175.9 目标建立
175.9.1 Goal Creation
目标建立是将已经处理的Need转化为正式Goal对象。
基本模型:
CreateGoal(N,T,C,P)→GCreateGoal(N,T,C,P) \rightarrow G
流程:
Need
↓
Validate Need
↓
Define Target
↓
Define Condition
↓
Define Priority
↓
Define Expected State
↓
Create Goal
↓
Initial State
175.10 Goal建立条件
目标不能随意建立。
至少需要:
Need存在
Target明确
Condition可表达
Goal Type合法
Priority合法
目标状态合法
因此:
CanCreateGoal=Need∧Target∧Condition∧TypeCanCreateGoal = Need \land Target \land Condition \land Type
如果Need本身无法形成明确Target,则不能直接建立正式Goal。
可以先建立:
Goal Candidate
而不是伪造一个已经确定的Goal。
175.11 Goal Candidate
Goal Candidate是经过Need处理但尚未完全确认的目标候选。
例如:
Need:
System needs recovery
可能产生:
Goal Candidate A:
System State = Ready
Goal Candidate B:
Service State = Running
Goal Candidate C:
Database State = Available
此时:
Need
↓
Goal Candidates
↓
Decision
↓
Goal
如果只有一个明确目标:
Need
↓
Goal
因此GoalService可以产生Candidate,但最终是否选择某个Candidate,可以交给Decision体系。
175.12 Goal建立PHP结构
可以建立:
class Goal
{
protected $id;
protected $needId;
protected $targetId;
protected $type;
protected $condition;
protected $priority;
protected $state;
public function __construct(
$id,
$needId,
$targetId,
$type,
$condition,
$priority
) {
$this->id = $id;
$this->needId = $needId;
$this->targetId = $targetId;
$this->type = $type;
$this->condition = $condition;
$this->priority = $priority;
$this->state = 'created';
}
public function getId()
{
return $this->id;
}
public function getTargetId()
{
return $this->targetId;
}
public function getState()
{
return $this->state;
}
}
这里Goal是Domain Object,而不是Service。
175.13 目标更新
175.13.1 Goal Update定义
目标更新是根据新的Need、State、Feedback、Result、Environment或其他有效事实,对现有Goal进行合法修改。
基本模型:
Gt+ΔF→Gt+1G_t+\Delta F \rightarrow G_{t+1}
其中:
- GtG_t:当前Goal;
- ΔF\Delta F:新的事实变化;
- Gt+1G_{t+1}:更新后的Goal。
175.14 为什么Goal需要更新
Goal不是永远固定的。
例如:
Goal:
Robot到达Room-B
执行过程中发现:
Room-B unavailable
系统可能需要更新:
Target = Room-C
或者:
Goal State = Blocked
也可能因为Need发生变化:
Need Cancelled
导致:
Goal State = Cancelled
因此:
Goalt≠Goalt+1Goal_t \neq Goal_{t+1}
是合法情况。
175.15 Goal更新来源
Goal更新可以来自:
Need Update
State Change
Execution Result
Feedback
Environment Change
Risk
Conflict
Diagnosis
Verification
Decision
例如:
Goal
↓
Behavior
↓
Execution
↓
Failure
↓
Feedback
↓
Goal Update
这使Goal进入完整闭环。
175.16 Goal更新与Result
Goal是否完成不能根据“已经执行了Behavior”判断。
必须根据Result。
例如:
Goal:
Robot reaches Room-B
执行:
Behavior:
MoveTo(Room-B)
但Result:
Actual Location = Room-A
那么:
GoalStatus≠CompletedGoalStatus \neq Completed
必须继续:
Goal
↓
Failed / Incomplete
↓
Diagnosis / Decision
↓
New Method
因此:
Execution≠GoalCompletionExecution \neq GoalCompletion
而:
Result+GoalCondition→GoalStatusResult + GoalCondition \rightarrow GoalStatus
175.17 目标状态
175.17.1 Goal State定义
Goal State(目标状态) 描述目标当前处于什么生命周期阶段。
建议基础状态:
created
pending
active
paused
blocked
completed
failed
cancelled
expired
其基本生命周期:
Created
↓
Pending
↓
Active
↓
Completed
异常路径:
Active
├── Blocked
├── Failed
├── Cancelled
└── Expired
175.18 Goal State与Object State不同
Goal State不是Target Object的State。
例如:
Goal:
Robot reaches Room-B
Goal State:
Active
而Robot State可能:
Running
Room-B State可能:
Available
三个状态完全不同:
GoalState≠ObjectStateGoalState \neq ObjectState
因此不能直接使用Target Object的State代替Goal State。
175.19 Goal状态转换
可以定义:
Sgoal,t+Event+Condition→Sgoal,t+1S_{goal,t} + Event + Condition \rightarrow S_{goal,t+1}
例如:
created
↓
activate
↓
active
完成:
active
↓
Result satisfies Goal Condition
↓
completed
失败:
active
↓
Execution Failed
↓
failed
阻塞:
active
↓
Required Condition Unavailable
↓
blocked
取消:
active
↓
Need Cancelled
↓
cancelled
175.20 Goal完成判断
Goal完成必须具有明确条件。
定义:
Complete(G,R)=Match(R,GC)Complete(G,R) = Match(R,G_C)
其中:
- GCG_C:Goal Condition;
- RR:实际Result。
如果:
Match=TrueMatch=True
则:
GoalState=CompletedGoalState=Completed
例如:
Goal:
Robot Location = Room-B
Actual Result:
Robot Location = Room-B
因此:
Goal Condition
=
Actual Result
得到:
Completed
如果:
Actual Result
=
Room-A
则:
Completed = False
175.21 Goal失败判断
Goal失败也必须有依据。
例如:
Goal:
Move to Room-B
实际:
Path permanently unavailable
则:
Goal State = Failed
但是:
Temporary path unavailable
可能只是:
Goal State = Blocked
因此:
Failed≠BlockedFailed \neq Blocked
这是Goal状态模型中的重要区别。
175.22 Goal状态与StateService
第172章已经建立StateService。
因此GoalService不应该自己实现完整状态转换系统。
可以:
GoalService
↓
StateService
↓
StateEngine
GoalService负责:
Goal当前状态是什么
什么时候需要转换
为什么转换
StateService负责:
是否允许从State-A转换到State-B
因此:
GoalService=Goal LifecycleGoalService = Goal\ Lifecycle
而:
StateService=State TransitionStateService = State\ Transition
175.23 GoalService与Capability
Goal本身不负责执行。
Goal建立之后,需要知道:
是否存在实现这个Goal所需要的Capability?
例如:
Goal:
Robot reaches Room-B
系统查询:
Capability:
Move
Navigate
Locate
形成:
Goal
↓
Required Capability
↓
CapabilityService
因此:
Goal→CapabilityGoal \rightarrow Capability
但:
GoalService≠CapabilityServiceGoalService \neq CapabilityService
175.24 GoalService与Method
Goal明确:
需要达到什么结果
Method明确:
如何达到这个结果
因此:
Goal
↓
Required Capability
↓
Candidate Methods
例如:
Goal:
Reach Room-B
候选Method:
Method-A:
Walk
Method-B:
Vehicle
Method-C:
Elevator + Walk
GoalService不选择具体Method。
Method选择由Decision负责。
因此:
Goal→MethodCandidates→DecisionGoal \rightarrow MethodCandidates \rightarrow Decision
175.25 GoalService与Decision
多个Goal也可能存在优先级冲突。
例如:
Goal-A:
Continue Task
Goal-B:
Recharge
Goal-C:
Avoid Risk
此时需要:
Goal
+
Priority
+
Condition
+
Risk
交给Decision体系进行选择或排序。
因此:
GoalService→DecisionGoalService \rightarrow Decision
但:
GoalService≠DecisionServiceGoalService\neq DecisionService
GoalService负责Goal对象,DecisionService负责选择。
175.26 Goal与Priority
Goal通常需要Priority。
可以定义:
P(G)∈[0,∞)P(G)\in[0,\infty)
数值越大表示优先级越高,或者采用明确的离散等级:
Low
Normal
High
Critical
例如:
Goal-A = Complete Task
Priority = Normal
Goal-B = Protect System
Priority = Critical
当两者冲突时:
Decision
↓
Compare Goal Priority
↓
Select Goal
因此Priority是Goal的重要属性,但Priority本身不等于Decision。
175.27 多目标结构
Individual可能同时具有多个Goal:
Individual
├── Goal-A
├── Goal-B
├── Goal-C
└── Goal-D
形成:
G={G1,G2,…,Gn}G=\{G_1,G_2,\ldots,G_n\}
例如:
Goal-A: Finish Task
Goal-B: Maintain Safety
Goal-C: Save Resource
这些Goal可以同时存在。
但当资源、时间或状态无法同时满足时:
Goal Conflict
↓
Decision
因此GoalService需要支持多个Goal,而不是把Goal设计成系统唯一变量。
175.28 Goal关系
Goal之间也可以存在关系。
例如:
Goal-A
↓
requires
↓
Goal-B
或者:
Goal-A
↓
conflicts_with
↓
Goal-B
还可以:
Goal-A
↓
depends_on
↓
Goal-B
因此Goal本身可以利用第173章Relation体系:
Goal→RelationGoalGoal \xrightarrow{Relation} Goal
形成Goal Network。
例如:
Main Goal
├── depends_on → Sub Goal A
├── requires → Sub Goal B
└── conflicts_with → Goal C
175.29 Goal分解
复杂Goal可以分解为多个Sub Goal。
例如:
Goal:
Obtain Cup
可以分解:
Goal-A:
Locate Cup
Goal-B:
Move to Cup
Goal-C:
Grasp Cup
Goal-D:
Confirm Cup Held
形成:
Gmain={G1,G2,G3,G4}G_{main} = \{G_1,G_2,G_3,G_4\}
关系:
Main Goal
↓
Sub Goal A
↓
Sub Goal B
↓
Sub Goal C
↓
Sub Goal D
GoalService可以负责建立和维护这种Goal结构。
但具体Action执行仍由Behavior/Action/Execution体系负责。
175.30 Goal验证
Goal状态最终必须能够由实际结果验证。
例如:
Goal:
Cup Held
不能因为:
Grasp Action Executed
就认为Goal完成。
必须:
Action
↓
Execution
↓
Result
↓
Verification
↓
Goal State
如果Result:
Gripper contains Cup = True
才能:
Goal = Completed
因此:
GoalCompleted=Result+VerificationGoalCompleted = Result + Verification
而不是:
GoalCompleted=ActionExecutedGoalCompleted = ActionExecuted
175.31 Goal与Feedback
Goal执行后会产生Feedback。
例如:
Goal:
Reach Room-B
执行后:
Result:
Room-A
Feedback:
Goal condition not satisfied
于是:
Feedback
↓
GoalService
↓
Goal State Update
可能变为:
Active
或者:
Blocked
或者:
Failed
因此:
Feedback→GoalStateUpdateFeedback \rightarrow GoalStateUpdate
175.32 Goal与Risk
Goal执行过程中可能发现Risk。
例如:
Goal:
Move to Room-B
发现:
Risk:
Path unsafe
系统可能:
Goal
↓
Blocked
↓
Protection
↓
Alternative Method
↓
Decision
因此Risk不直接修改Goal内容,而通过Risk/Protection/Decision影响Goal状态和执行路径。
175.33 Goal与Diagnosis
Goal失败之后:
Goal
↓
Failed
↓
Diagnosis
Diagnosis可以分析:
为什么Goal没有完成?
例如:
Goal:
Reach Room-B
Result:
Room-A
Diagnosis:
Navigation Method Failed
然后:
Diagnosis
↓
Repair / Method Change
↓
Goal remains Active
因此Goal Failed并不一定意味着Goal永久结束。
根据系统规则,可以:
Failed
↓
Diagnosis
↓
Repair
↓
Active
175.34 GoalService PHP结构
可以建立:
class GoalService
{
protected $repository;
protected $stateService;
protected $needService;
public function __construct(
$repository,
$stateService,
$needService
) {
$this->repository =
$repository;
$this->stateService =
$stateService;
$this->needService =
$needService;
}
public function create(
$needId,
$targetId,
$type,
$condition,
$priority
) {
$need =
$this->needService->get(
$needId
);
if (!$need) {
return false;
}
$goal = new Goal(
null,
$needId,
$targetId,
$type,
$condition,
$priority
);
return $this->repository->save(
$goal
);
}
public function get($id)
{
return $this->repository->find(
$id
);
}
public function update(
$goalId,
$data
) {
$goal =
$this->get($goalId);
if (!$goal) {
return false;
}
$goal->update($data);
return $this->repository->save(
$goal
);
}
public function changeState(
$goalId,
$event
) {
$goal =
$this->get($goalId);
if (!$goal) {
return false;
}
return $this->stateService
->transition(
$goal,
$event
);
}
}
这个结构体现:
GoalService
├── Need
├── GoalRepository
└── StateService
而不是把所有逻辑塞入Goal类。
175.35 GoalRepository
GoalRepository负责Persistence。
例如:
class GoalRepository
{
public function find($id)
{
// Load Goal
}
public function findByNeed(
$needId
) {
// Load Goals
}
public function findActive(
$targetId
) {
// Load active Goals
}
public function save(
$goal
) {
// Insert / Update
}
}
Service不负责数据库细节。
因此:
GoalService→GoalRepository→MySQLGoalService \rightarrow GoalRepository \rightarrow MySQL
175.36 Goal数据库结构
可以建立:
goals
id
need_id
target_id
type
condition
priority
state
created_at
updated_at
Goal History:
goal_history
id
goal_id
from_state
to_state
event
reason
created_at
Goal关系:
goal_relations
id
source_goal_id
relation_type
target_goal_id
state
created_at
Goal Evidence:
goal_evidence
id
goal_id
source_type
source_id
evidence_type
value
created_at
这样Goal就具有:
Goal
├── Need
├── Target
├── Condition
├── Priority
├── State
├── Relation
├── History
└── Evidence
175.37 Goal状态保存
Goal状态必须根据真实事件和规则更新。
例如:
Goal = Active
执行:
Execution
得到:
Result
再经过:
Verification
得到:
Goal Condition Satisfied
最终:
Goal = Completed
因此:
StateUpdate=Event+Condition+Result+VerificationStateUpdate = Event + Condition + Result + Verification
而不能:
Goal → Completed
直接修改状态。
175.38 Goal与Runtime Object
Runtime Object拥有当前运行上下文。
因此可以:
Runtime Object
↓
Current Goal
↓
Current Method
↓
Current Behavior
↓
Current Action
↓
Execution
例如:
Robot Runtime
State = Running
Goal = Fetch Cup
Method = MoveTo
Behavior = FetchCup
Action = Move
这样Goal成为Runtime Object的重要认知上下文。
175.39 GoalService完整架构
最终形成:
Controller
↓
GoalService
├── NeedService
├── StateService
└── GoalRepository
↓
MySQL
与Engine结合:
GoalService
↓
GoalEngine
├── Goal Condition
├── Goal Validation
├── Goal Completion
└── Goal Conflict
因此:
Service
=
流程协调
Engine
=
规则计算
Domain Object
=
Goal本体
Repository
=
Persistence
175.40 Goal完整认知流程
完整过程:
Current State
↓
Need
↓
Need Processing
↓
Goal Candidate
↓
Goal Creation
↓
Goal Active
↓
Capability
↓
Method
↓
Decision
↓
Behavior
↓
Action
↓
Execution
↓
Result
↓
Feedback
↓
Verification
↓
Goal State
如果成功:
Goal State
↓
Completed
如果失败:
Goal State
↓
Failed
↓
Diagnosis
↓
Repair / Method Change
↓
Active
如果阻塞:
Goal State
↓
Blocked
↓
Protection / Alternative
↓
Decision
↓
Active
175.41 Goal与整个ICAI闭环
至此,ICAI已经形成:
State
↓
Need
↓
Goal
↓
Capability
↓
Method
↓
Decision
↓
Behavior
↓
Action
↓
Execution
↓
Result
↓
Feedback
↓
Verification
↓
Goal State
↓
Memory
↓
Experience
这个结构具有一个重要特点:
Goal不是执行过程,而是整个执行过程的方向约束和完成判断依据。
因此:
Goal→ExecutionDirectionGoal \rightarrow ExecutionDirection
而:
Result→GoalVerificationResult \rightarrow GoalVerification
最终形成:
Need→Goal→Execution→Result→Verification→GoalState\boxed{ Need \rightarrow Goal \rightarrow Execution \rightarrow Result \rightarrow Verification \rightarrow GoalState }
175.42 本章核心原则
第一,Need与Goal必须分离
Need≠GoalNeed\neq Goal
Need表示需求,Goal表示希望达到的状态或结果。
第二,Goal必须可结构化
至少包含:
Target
Condition
Priority
State
第三,Goal不负责执行
Goal
↓
Capability
↓
Method
↓
Decision
↓
Behavior
第四,Goal完成必须基于Result
GoalCompleted≠ActionExecutedGoalCompleted \neq ActionExecuted
而是:
GoalCompleted=Result+VerificationGoalCompleted = Result + Verification
第五,Goal State必须独立于Object State
GoalState≠ObjectStateGoalState \neq ObjectState
第六,Goal可以更新
Gt+ΔF→Gt+1G_t+\Delta F \rightarrow G_{t+1}
但更新必须有事实依据。
第七,Goal可以存在层级和关系
Main Goal
├── Sub Goal
├── depends_on
├── requires
└── conflicts_with
第八,GoalService、StateService、DecisionService必须保持边界
GoalService
↓
管理目标
StateService
↓
管理状态转换
DecisionService
↓
管理候选选择
BehaviorService
↓
管理行为执行组织
175.43 本章小结
第175章建立了GoalService。
Goal的核心模型:
G=(ID,N,T,C,P,S)G=(ID,N,T,C,P,S)
其中:
- IDID:Goal Identity;
- NN:来源Need;
- TT:Target;
- CC:目标条件;
- PP:Priority;
- SS:Goal State。
GoalService核心模型:
GoalService=NeedProcessing+GoalCreation+GoalUpdate+GoalState\boxed{ GoalService = NeedProcessing + GoalCreation + GoalUpdate + GoalState }
其完整流程:
Need
↓
需求处理
↓
Goal建立
↓
Goal Active
↓
Capability
↓
Method
↓
Decision
↓
Behavior
↓
Execution
↓
Result
↓
Verification
↓
Goal State Update
最终可以归纳为:
Goal=Target+Condition+Priority+State\boxed{ Goal = Target + Condition + Priority + State }
而GoalService的真正作用是:
将Need转化为明确的Goal,并持续维护Goal的目标条件、优先级、生命周期和实际完成状态,使整个ICAI认知—执行—反馈闭环始终围绕可验证目标运行。
因此,在整个ICAI Service体系中:
NeedService→GoalService→CapabilityService→MethodService→DecisionService→BehaviorService\boxed{ NeedService \rightarrow GoalService \rightarrow CapabilityService \rightarrow MethodService \rightarrow DecisionService \rightarrow BehaviorService }
GoalService成为连接需求层与执行层的核心服务节点。