Skip to content

Commit

Permalink
support parse feature
Browse files Browse the repository at this point in the history
Change-Id: Ifcdac2447e05160d5f2b09b53c076a574b5d9e12
  • Loading branch information
AoranAllen committed Jul 25, 2024
1 parent 01c9074 commit 0aae0f4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
4 changes: 2 additions & 2 deletions arishem/arishem_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestBuildCondition(t *testing.T) {
expr, err := condGroup.Build()
assert.Nil(t, err)
assert.NotEmpty(t, expr)
assert.Equal(t, `{"OpLogic":"&&","Conditions":[{"Operator":"==","Lhs":{"Const":{"NumConst":1}},"Rhs":{"Const":{"NumConst":9223372036854775807}}}]}`, expr)
assert.Equal(t, `{"OpLogic":"&&","Conditions":[{"Operator":"==","Lhs":{"Const":{"NumConst":1.0}},"Rhs":{"Const":{"NumConst":9223372036854775807}}}]}`, expr)

cond2 := NewCondition(ListIn, NOT)
var varPath VarExpr = "user.user_list#2.name"
Expand All @@ -48,7 +48,7 @@ func TestBuildCondition(t *testing.T) {
expr, err = condGroup.Build()
assert.Nil(t, err)
assert.NotEmpty(t, expr)
assert.Equal(t, `{"OpLogic":"&&","Conditions":[{"Operator":"==","Lhs":{"Const":{"NumConst":1}},"Rhs":{"Const":{"NumConst":9223372036854775807}}},{"Operator":"!LIST_IN","Lhs":{"VarExpr":"user.user_list#2.name"},"Rhs":{"ConstList":[{"StrConst":"Jack"},{"StrConst":"Jane"},{"StrConst":"John"},{"StrConst":"Ezreal"}]}}]}`, expr)
assert.Equal(t, `{"OpLogic":"&&","Conditions":[{"Operator":"==","Lhs":{"Const":{"NumConst":1.0}},"Rhs":{"Const":{"NumConst":9223372036854775807}}},{"Operator":"!LIST_IN","Lhs":{"VarExpr":"user.user_list#2.name"},"Rhs":{"ConstList":[{"StrConst":"Jack"},{"StrConst":"Jane"},{"StrConst":"John"},{"StrConst":"Ezreal"}]}}]}`, expr)

pass, err := JudgeConditionWithFactMeta(context.Background(), expr, `{"user":{"user_list":[{"name":"Aatrox"},{"name":"Ahri"},{"name":"Ezreal"},{"name":"MalPhite"}]}}`)
assert.Nil(t, err)
Expand Down
6 changes: 3 additions & 3 deletions arishem/arishem_lw_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func ParseRuleTree(expr string, exprType ExprType) (exprTree *RuleTree, err erro
return
}

// ParseFeature input a feature expression string and return the typedef.FeatureParam
func ParseFeature(featureExpr string) (typedef.FeatureParam, error) {
// ParseSingleFeature input a feature expression string and return the typedef.FeatureParam
func ParseSingleFeature(featureExpr string) (typedef.FeatureParam, error) {
fppl := core.NewFeaturePreParseListener()
err := parser.ParseSingleFeature(featureExpr, fppl)
err := parser.ParseSingleExpr(featureExpr, fppl)
if err != nil {
return nil, err
}
Expand Down
52 changes: 52 additions & 0 deletions arishem/arishem_lw_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,55 @@ func TestWalkAim(t *testing.T) {
testCase.check(WalkAim(testCase.aimExpr, dc))
}
}

func TestParseFeature(t *testing.T) {
testCases := []*struct {
featExpr string
check func(fp typedef.FeatureParam, err error)
}{
{
`{"FeatureExpr":{"FeaturePath":"Hit_Knowledge_Count","BuiltinParam":{"Knowledge_id":{"ConstList":[{"NumConst":7348866865063544851},{"NumConst":7348866865063790611}]},"Start_time":{"Const":{"StrConst":"2024-01-01"}}}}}`,
func(fp typedef.FeatureParam, err error) {
assert.Nil(t, fp)
assert.NotNil(t, err)
fmt.Printf("%v\n", err)
},
},
{
`{"FeatureExpr":{"FeaturePath":"Hit_Knowledge_Count.xxxx","BuiltinParam":{"Knowledge_id":{"ConstList":[{"NumConst":7348866865063544851},{"NumConst":7348866865063790611}]},"Start_time":{"Const":{"StrConst":"2024-01-01"}}}}}`,
func(fp typedef.FeatureParam, err error) {
assert.Nil(t, err)
assert.NotNil(t, fp)
assert.NotNil(t, fp.BuiltinParam())
assert.Equal(t, fp.FeatureName(), "Hit_Knowledge_Count")
idList := fp.BuiltinParam()["Knowledge_id"]
assert.NotEmpty(t, idList)
for _, i := range idList.([]interface{}) {
fmt.Printf("%v\n", i)
}

},
},
{
`{"FeatureExpr":{"FeaturePath":"user_center.user_personality"}}`,
func(fp typedef.FeatureParam, err error) {
assert.Nil(t, err)
assert.NotNil(t, fp)
assert.Nil(t, fp.BuiltinParam())
assert.Equal(t, fp.FeatureName(), "user_center")
},
},
{
`{"MathExpr":{"OpMath":">","ParamList":[{"Const":{"NumConst":100}},{"Const":{"NumConst":20}}]}}`,
func(fp typedef.FeatureParam, err error) {
assert.Nil(t, fp)
assert.NotNil(t, err)
fmt.Printf("%v\n", err)
},
},
}
for _, testCase := range testCases {
featParam, err := ParseSingleFeature(testCase.featExpr)
testCase.check(featParam, err)
}
}
4 changes: 2 additions & 2 deletions internal/parser/arishem_ctx_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strings"
)

func ParseSingleFeature(expr string, pos typedef.ParseObserver) error {
func ParseSingleExpr(expr string, pos typedef.ParseObserver) error {
if pos == nil {
return errors.New("feature parse observer is nil")
}
Expand All @@ -45,7 +45,7 @@ func ParseSingleFeature(expr string, pos typedef.ParseObserver) error {
cl.AddListenerProxy(pos)
ruleParser.AddParseListener(cl)

_ = ruleParser.Feature()
_ = ruleParser.Expr()
return nil
}

Expand Down

0 comments on commit 0aae0f4

Please sign in to comment.