Skip to content

Commit

Permalink
feat(reasoner): thinker support deduction expression (#369)
Browse files Browse the repository at this point in the history
Co-authored-by: FishJoy <[email protected]>
  • Loading branch information
wangsff and fishjoy authored Oct 18, 2024
1 parent 88c522d commit 5dc03cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ LIMIT : ('L' | 'l')('I' | 'i')('M' | 'm')('I' | 'i')('T' | 't');
OFFSET :('O' | 'o')('F' | 'f')('F' | 'f')('S' | 's')('E' | 'e')('T' | 't');

AND : (('A' | 'a')('N' | 'n')('D' | 'd'))|('&&') ;
XOR : ('X' | 'x')('O' | 'o')('R' | 'r') ;
OR_Latter : ('O' | 'o')('R' | 'r') ;
OR_Symb : '||';

Expand All @@ -547,6 +548,7 @@ DESCRIPTION : 'Description';
// rule 表达式
or : OR_Latter|OR_Symb;
not : NOT_Latter | NOT_Symb;
xor: XOR;
value_expression_primary : parenthesized_value_expression|non_parenthesized_value_expression_primary_with_property ;
parenthesized_value_expression : left_paren value_expression right_paren ;
non_parenthesized_value_expression_primary_with_property: non_parenthesized_value_expression_primary (period property_name ) * ;
Expand Down Expand Up @@ -608,14 +610,15 @@ floor_operator: FLOOR;
ceiling_operator: CEIL|CEILING;

function_expr : function_name left_paren function_args? right_paren;
function_name : identifier;
function_name : identifier | list_common_agg_name;
function_args : list_element_list;

lambda_expr : left_paren binary_lambda_args right_paren labmda_body_array value_expression;
binary_lambda_args : identifier comma identifier ;
// 逻辑 计算
logic_value_expression : logic_term (or logic_term)* ;
logic_term : logic_factor (AND logic_factor)* ;
logic_value_expression : logic_term (or logic_term)*;
logic_term : logic_item (AND logic_item)* ;
logic_item : logic_factor (xor logic_factor)*;
logic_factor : (not)? logic_test ;
logic_test : (spo_rule | concept_name | expr) ( (IS ( NOT_Latter )?|equals_operator|not_equals_operator) truth_value )? ;
truth_value : TRUE|FALSE|NULL ;
Expand Down Expand Up @@ -963,6 +966,7 @@ thinker_script: (
define_rule_on_concept
| define_rule_on_relation_to_concept
| define_proiority_rule_on_concept
| logical_deduce
)*;
/*
Expand All @@ -979,6 +983,12 @@ Define (Med.drug)-[基本用药方案]->(药品/`ACEI+噻嗪类利尿剂`) {
*/
define_rule_on_relation_to_concept : define_rule_on_relation_to_concept_structure (description)?;
logical_deduce : deduce_premise labmda_body_array deduce_conclusion description?;
deduce_premise : logical_statement;
deduce_conclusion : logical_statement;
/*
DefinePriority(危险水平分层) {
超高危=100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,14 @@ class RuleExprParser extends Serializable {
}
}

def parseLogicItem(ctx: Logic_itemContext): Expr = {
val xorList = ctx.logic_factor().asScala.toList.map(x => parseLogicFactor(x))
xorList.reduce((A: Expr, B: Expr) => BinaryOpExpr(BXor, A, B))
}

def parseLogicTerm(ctx: Logic_termContext): Expr = {
val andlist = ctx.logic_factor().asScala.toList.map(x => parseLogicFactor(x))
andlist.reduce((A: Expr, B: Expr) => BinaryOpExpr(BAnd, A, B))
val andList = ctx.logic_item().asScala.toList.map(x => parseLogicItem(x))
andList.reduce((A: Expr, B: Expr) => BinaryOpExpr(BAnd, A, B))
}

def parseLogicValueExpression(ctx: Logic_value_expressionContext): Expr = {
Expand Down Expand Up @@ -698,7 +703,13 @@ class RuleExprParser extends Serializable {
val passArgs: List[Expr] = refExpr +: funcArgs
val functionExpr = parseFunctionExprDetail(ctx.function_name().getText, passArgs)
functionExpr match {
case c: FunctionExpr => AggOpExpr(AggUdf(c.name, funcArgs), refExpr)
case c: FunctionExpr =>
try {
parseAggFunc(c.name, c.funcArgs.head)
} catch {
case _: Exception =>
AggOpExpr(AggUdf(c.name, funcArgs), refExpr)
}
case c => c
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ case object BNotGreaterThan extends BinaryOpSet
case object BSmallerThan extends BinaryOpSet
case object BNotSmallerThan extends BinaryOpSet
case object BOr extends BinaryOpSet

case object BXor extends BinaryOpSet
case object BIn extends BinaryOpSet
case object BLike extends BinaryOpSet
Expand Down

0 comments on commit 5dc03cb

Please sign in to comment.