我使用的是ABP样板6.0,并已将NRules与我们的平台集成。
中使用这些单词。
调用代码
public WasteManagementManager(
IRepository<WasteBirthCertificateBlock, long> wasteBirthCertificateBlockRepository,
IRepository<WasteBirthCertificateChain, long> wasteBirthCertificateChainRepository,
ISession nRuleSession,
ISessionFactory nRuleSessionFactory,
ILogger log
)
{
_wasteBirthCertificateBlockRepository = wasteBirthCertificateBlockRepository;
_wasteBirthCertificateChainRepository = wasteBirthCertificateChainRepository;
_nRuleSession = nRuleSession;
_nRuleSessionFactory = nRuleSessionFactory;
_log = log;
}
public void Trigger()
{==>
When I am in debug, _questionResponseRepository is NOT NUll. I'm trying inject it as a fact but that is not property injection .. I'm just trying one way or the other to get it working
_nRuleSession.Insert(_questionResponseRepository);
_nRuleSession.Fire();
}规则代码
namespace SagerSystems.AI.WasteManagements.NRules
{
[Name("Track Explosive Substances Rule")]
public class TrackExplosiveSubstancesRule : Rule
{
private string[] _listOfExplosiveKeyWords = new string[] { "H3O", "N2" };
public IRepository<QuestionResponse, long> _questionResponseRepository { get; set; }
public TrackExplosiveSubstancesRule()
{
**This does NOT work** (the value remains null)
Dependency()
.Resolve(() => _questionResponseRepository);
}
public override void Define()
{
*This does work* but only after the rule fires)
Dependency()
.Resolve(() => _questionResponseRepository);
When()
.Match(() => questionResponseDto, c => CheckKeyWord(c));
Then()
.Do(ctx => Console.WriteLine(“Test Condition Works”))
}
private bool CheckKeyWord(QuestionResponseDto questionResponseDto)
{
==> How do I user ‘questionResponseRepository’
var isKeyWord=
_listOfExplosiveKeyWords.Any(c => questionResponseDto.QuestionText.Contains(c));
return isKeyWord;
}
}
}发布于 2021-01-01 00:05:54
在NRules中规则的匹配条件中,有几种方法可以使用外部信息(在本例中是来自DB的关键字)。
Keyword实体的存储库,可以用来从DB中获取关键字。我也在使用构造函数注入,但同样适用于属性注入。 public class RuleActivator : IRuleActivator
{
private readonly IIocResolver _iocResolver;
public RuleActivator(IIocResolver iocResolver)
{
_iocResolver = iocResolver;
}
public IEnumerable<Rule> Activate(Type type)
{
yield return (Rule)_iocResolver.Resolve(type);
}
}
public class RulesEngineModule : AbpModule
{
public override void Initialize()
{
//Find rule classes
var scanner = new RuleTypeScanner();
scanner.AssemblyOf<TrackExplosiveSubstancesRule>();
var ruleTypes = scanner.GetRuleTypes();
//Register rule classes with the container
foreach (var ruleType in ruleTypes)
{
IocManager.Register(ruleType);
}
//Load rules into the repository; use a rule activator to resolve rules via the container
var repository = new RuleRepository {Activator = new RuleActivator(IocManager)};
repository.Load(x => x.From(s => s.Type(ruleTypes)));
//Compile rules into the factory
var factory = repository.Compile();
//Register session factory instance
IocManager.IocContainer.Register(
Component.For<ISessionFactory>().Instance(factory));
//Register session as a delegate that creates a new instance from a factory
IocManager.IocContainer.Register(
Component.For<ISession>().UsingFactoryMethod(k => k.Resolve<ISessionFactory>().CreateSession()).LifestyleTransient());
}
}
[Name("Track Explosive Substances Rule")]
public class TrackExplosiveSubstancesRule : Rule
{
private readonly IRepository<Keyword, long> _keywordRepository;
public TrackExplosiveSubstancesRule(IRepository<Keyword, long> keywordRepository)
{
_keywordRepository = keywordRepository;
}
public override void Define()
{
QuestionResponseDto questionResponseDto = default;
When()
.Match(() => questionResponseDto, c => ContainsKeyword(c));
Then()
.Do(ctx => Console.WriteLine("Test Condition Works"));
}
private bool ContainsKeyword(QuestionResponseDto questionResponseDto)
{
var keywords = _keywordRepository.GetAll().ToList();
var hasKeyWord = keywords.Any(keyword => questionResponseDto.QuestionText.Contains(keyword.Value));
return hasKeyWord;
}
}然后在触发器方法中的某个位置或相应的控制器中的某个位置:
var dto = new QuestionResponseDto(...);
_session.Insert(dto);
_session.Fire(); public class KeywordSet
{
private readonly Keyword[] _keywords;
public KeywordSet(IEnumerable<Keyword> keywords)
{
_keywords = keywords.ToArray();
}
public bool ContainsAny(string value)
{
return _keywords.Any(keyword => value.Contains(keyword.Value));
}
}
[Name("Track Explosive Substances Rule")]
public class TrackExplosiveSubstancesRule : Rule
{
public override void Define()
{
KeywordSet keywordSet = default;
QuestionResponseDto questionResponseDto = default;
When()
.Match(() => keywordSet)
.Match(() => questionResponseDto, c => keywordSet.ContainsAny(c.QuestionText));
Then()
.Do(ctx => Console.WriteLine("Test Condition Works"));
}
}然后在触发器方法中的某个位置或相应的控制器中的某个位置:
var keywords = _keywordRepository.GetAll().ToList();
var keywordSet = new KeywordSet(keywords);
_session.Insert(keywordSet);
var dto = new QuestionResponseDto(...);
_session.Insert(dto);
_session.Fire();https://stackoverflow.com/questions/65469249
复制相似问题