开始扩展rule

This commit is contained in:
zhanghaishan
2022-06-14 18:46:38 +08:00
parent f91c1edc1a
commit fd59741ce8
6 changed files with 442 additions and 0 deletions
+14
View File
@@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestLinux", "TestLinux\Test
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FireDogEditor", "FireDogEditor\FireDogEditor.vcxproj", "{ED98F27A-F902-458D-877D-FF5FCA80E10B}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestNewRule", "TestNewRule\TestNewRule.vcxproj", "{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
FireDog\FireDog.vcxitems*{07ecb430-e41b-4b28-abaa-ea857b15e618}*SharedItemsImports = 9
@@ -75,6 +77,18 @@ Global
{ED98F27A-F902-458D-877D-FF5FCA80E10B}.Release|x64.ActiveCfg = Release|Win32
{ED98F27A-F902-458D-877D-FF5FCA80E10B}.Release|x86.ActiveCfg = Release|Win32
{ED98F27A-F902-458D-877D-FF5FCA80E10B}.Release|x86.Build.0 = Release|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Debug|ARM.ActiveCfg = Debug|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Debug|ARM64.ActiveCfg = Debug|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Debug|x64.ActiveCfg = Debug|x64
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Debug|x64.Build.0 = Debug|x64
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Debug|x86.ActiveCfg = Debug|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Debug|x86.Build.0 = Debug|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Release|ARM.ActiveCfg = Release|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Release|ARM64.ActiveCfg = Release|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Release|x64.ActiveCfg = Release|x64
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Release|x64.Build.0 = Release|x64
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Release|x86.ActiveCfg = Release|Win32
{D0C3AB96-FA28-4095-A4BE-4AA0DE724F87}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
+40
View File
@@ -0,0 +1,40 @@
// TestNewRule.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include "rule.h"
using namespace std;
using namespace mountcloud;
void testRuleData() {
RuleData rd;
rd.hit("m");
long mcount = rd.count("m");
long scount = rd.count("s");
bool mhas = rd.has("m");
bool shas = rd.has("s");
cout << "end" << endl;
}
int main()
{
//testRuleData();
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
+151
View File
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{d0c3ab96-fa28-4095-a4be-4aa0de724f87}</ProjectGuid>
<RootNamespace>TestNewRule</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="rule.cpp" />
<ClCompile Include="TestNewRule.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="rule.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
+30
View File
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="TestNewRule.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="rule.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="rule.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
+142
View File
@@ -0,0 +1,142 @@
#include "rule.h"
#include <stdexcept>
using namespace mountcloud;
RuleData::RuleData() {
this->data = new std::unordered_map<std::string, long>();
}
RuleData::~RuleData() {
if (data != NULL) {
data->clear();
delete data;
data = NULL;
}
}
void RuleData::hit(std::string id) {
long count = data->operator[](id);
data->operator[](id) = (count + 1);
}
bool RuleData::has(std::string id) {
bool state = false;
try {
state = data->operator[](id) > 0;
}
catch (std::out_of_range ex) {
state = false;
}
return state;
}
long RuleData::count(std::string id) {
long count = data->operator[](id);
return count;
}
Rule::Rule() {
rules = new std::vector<Rule*>();
}
Rule::~Rule() {
if (rules != NULL) {
for (int i = 0; i < rules->size(); i++) {
Rule* rule = rules->at(i);
delete rule;
rule = NULL;
}
delete rules;
rules = NULL;
}
}
void Rule::addRule(Rule* rule) {
rules->push_back(rule);
}
bool AndRule::check(RuleData* data) {
//andµÄÇé¿ö
if (rules != NULL && rules->size() > 0) {
for (int i = 0; i < rules->size(); i++) {
Rule* r = rules->at(i);
if (!r->check(data)) {
return false;
}
}
return true;
}
}
bool OrRule::check(RuleData* data) {
//or
if (rules != NULL && rules->size() > 0) {
for (int i = 0; i < rules->size(); i++) {
Rule* r = rules->at(i);
if (r->check(data)) {
return true;
}
}
}
}
//Rule::~Rule() {
// if (ands != NULL) {
// for (int i = 0; i < ands->size(); i++) {
// Rule* r = ands->at(i);
// if (r != NULL) {
// delete r;
// r = NULL;
// }
// }
// ands->clear();
// delete ands;
// ands = NULL;
// }
//
// if (ors != NULL) {
// for (int i = 0; i < ors->size(); i++) {
// Rule* r = ors->at(i);
// if (r != NULL) {
// delete r;
// r = NULL;
// }
// }
// ors->clear();
// delete ors;
// ors = NULL;
// }
//}
//
//bool Rule::check(RuleData* data) {
// //andµÄÇé¿ö
// if (ands != NULL && ands->size() > 0) {
// for (int i = 0; i < ands->size(); i++) {
// Rule* r = ands->at(i);
// if (!r->check(data)) {
// return false;
// }
// }
// return true;
// }
//
// //or
// if (ors != NULL && ors->size() > 0) {
// for (int i = 0; i < ors->size(); i++) {
// Rule* r = ors->at(i);
// if (r->check(data)) {
// return true;
// }
// }
// }
//
// //ids
// if (!id.empty()) {
// if (data->has(id)) {
// return true;
// }
// }
//
// return false;
//}
+65
View File
@@ -0,0 +1,65 @@
#ifndef _MOUNTCOUD_RULE_H_
#define _MOUNTCOUD_RULE_H_
#include <string>
#include <vector>
#include <unordered_map>
/// <summary>
/// Lightweight rules engine
/// 轻量级规则引擎
/// 比如这样:{$ors:[$ands:["a","b"],$ands:["c","d"]]}
/// </summary>
namespace mountcloud {
class RuleData {
public:
RuleData();
~RuleData();
void hit(std::string id);
bool has(std::string id);
long count(std::string id);
private:
std::unordered_map<std::string, long>* data = NULL;
};
class Rule {
public:
Rule();
~Rule();
void addRule(Rule* rule);
public:
virtual bool check(RuleData* data);
protected:
std::vector<Rule*>* rules = NULL;
};
class AndRule : public Rule{
public:
bool check(RuleData* data) override;
};
class OrRule : public Rule {
public:
bool check(RuleData* data) override;
};
//class CountRule : public Rule {
//public:
//private:
// int
//};
//{$ors:[$ands:["a","b"],$ands:["c","d"]]}
//class Rule {
//public:
// std::vector<Rule*>* ands = NULL;
// std::vector<Rule*>* ors = NULL;
// std::string id;
// bool check(RuleData* data);
// ~Rule();
//};
}
#endif