<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* ScoreEngine.php
|
|
* Created by PhpStorm.
|
|
* Date: 2026/10/17
|
|
* Time: 09:40
|
|
* Author: qian.guo
|
|
* E-mail: yiyezhiqiutel@gmail.com
|
|
*/
|
|
|
|
namespace app\service\score;
|
|
|
|
/**
|
|
* 评分编排器
|
|
* 设计说明:
|
|
* - 业务层只跟本类打交道,不直接 new 策略
|
|
* - 三种用法:默认策略算分 / 指定策略试算 / 同批答案多策略对比
|
|
* - 不改动数据源,也不碰数据库,输入输出都是值对象
|
|
*/
|
|
class ScoreEngine
|
|
{
|
|
// 策略工厂
|
|
private $strategy_factory;
|
|
|
|
public function __construct(ScoreStrategyFactory $strategy_factory)
|
|
{
|
|
$this->strategy_factory = $strategy_factory;
|
|
}
|
|
|
|
/**
|
|
* 执行评分
|
|
* score
|
|
* @param ScoreContext $context 评分输入
|
|
* @param string $alias 策略标识,为空取默认策略
|
|
* @return ScoreResult
|
|
*/
|
|
public function score(ScoreContext $context, $alias = '')
|
|
{
|
|
$strategy = $this->strategy_factory->createStrategy($alias);
|
|
|
|
return $strategy->score($context);
|
|
}
|
|
|
|
/**
|
|
* 同批答案多策略对比
|
|
* compare
|
|
* @param ScoreContext $context 评分输入
|
|
* @param array $alias_list 策略标识列表
|
|
* @param bool $with_debug 是否附带中间量
|
|
* @return array 键为策略标识
|
|
*
|
|
* 用途是算法选型:把同一份答案喂给多个策略,看它们在哪类结果上分歧最大
|
|
* 再决定题库或算法该改哪里。这也是「纯计算」约定换来的能力
|
|
* 只要策略有副作用,对比就没有意义
|
|
*/
|
|
public function compare(ScoreContext $context, $alias_list, $with_debug = false)
|
|
{
|
|
$data = [];
|
|
|
|
foreach ($alias_list as $alias) {
|
|
$result = $this->score($context, $alias);
|
|
$data[$alias] = $result->getData($with_debug);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取已注册的策略清单
|
|
* getStrategyList
|
|
* @return array 每项含 alias / name / version
|
|
*/
|
|
public function getStrategyList()
|
|
{
|
|
$alias_list = $this->strategy_factory->getAliasList();
|
|
|
|
$data = [];
|
|
foreach ($alias_list as $alias) {
|
|
$strategy = $this->strategy_factory->createStrategy($alias);
|
|
|
|
$item = [];
|
|
$item['alias'] = $alias;
|
|
$item['name'] = $strategy->getName();
|
|
$item['version'] = $strategy->getVersion();
|
|
$data[] = $item;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 获取当前生效的策略标识
|
|
* getDefaultAlias
|
|
* @return string 落库时与策略版本一起写入
|
|
*/
|
|
public function getDefaultAlias()
|
|
{
|
|
$strategy = $this->strategy_factory->createStrategy();
|
|
|
|
return $strategy->getName();
|
|
}
|
|
}
|