Skip to content

Commit

Permalink
Make json_format() generic in RuleBasedResources
Browse files Browse the repository at this point in the history
Instead of being a static function that needs to be implemented in every
class, apply templates in subclasses that use the ID and representation
variables

- DatumMatch, MultiBoolMatch and AbstractAction have standard json
formats
- NOTMatch is an exception to boolean matches and needs to be implemented
directly, but since it is the only bool gate that cant be multi-input,
that is not an issue

Signed-off-by: Rodrigo Volpe Battistin <[email protected]>
  • Loading branch information
rvbatt committed Aug 10, 2023
1 parent d5ac653 commit 770969f
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 59 deletions.
7 changes: 7 additions & 0 deletions addons/rule_based_godot/resources/actions/abstract_action.gd
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ func _get_agent_nodes(bindings: Dictionary) -> Array:
print_debug("No nodes to perform Action")
return agents

func json_format() -> String:
# ["ID", "?wild"|["groups"]|"agent", vars...]
var string = '["' + action_id + '", "?wild"|["groups"]|"agent"'
for variable in repr_vars:
string += ', ' + variable
return string + ']'

func to_json_repr() -> Variant:
# ["ID", "?wild"|["groups"]|"agent", vars...]
var json_array = [action_id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ extends AbstractAction
@export var method: StringName
@export var arguments: Array

static func json_format() -> String:
return '["CallMethod", "?var"|"node", "method", ["?vars"|arguments]]'

func _init():
action_id = "CallMethod"
repr_vars = ["method", "arguments"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ extends AbstractAction

@export var signal_name: StringName
@export var parameter_to_type: Dictionary = {}
@export var arguments: Array = []# same order as dict

static func json_format() -> String:
return '["EmitSignal", "?var|node", "signal"]'
@export var arguments: Array = [] # same order as dict

func _init():
action_id = "EmitSignal"
Expand All @@ -20,6 +17,8 @@ func _result_from_agent(agent: Node, bindings: Dictionary) -> Variant:
var arg = args[i]
if arg is String and arg.begins_with('?'):
args[i] = bindings.get(arg.trim_prefix('?'))

if not agent.has_signal(signal_name):
agent.add_user_signal(signal_name, signal_param_array(parameter_to_type))

return agent.emit_signal(signal_name, args)
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ extends AbstractAction
var _property: StringName = ""
var _value: Variant = null

static func json_format() -> String:
return '["SetProperty", "?var|node", {"property": "?var"|value}]'

func _init():
action_id = "SetProperty"
repr_vars = ["property_and_value"]
Expand Down
7 changes: 0 additions & 7 deletions addons/rule_based_godot/resources/matches/abstract_match.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,3 @@ var match_id: StringName # ID for json format
func is_satisfied(bindings: Dictionary) -> bool:
push_error("Abstract method")
return false

func to_json_repr() -> Variant:
push_error("Abstract Method")
return null

func build_from_repr(json_repr: Array) -> void:
push_error("Abstract Method")
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
class_name ANDMatch
extends MultiBoolMatch

static func json_format() -> String:
return '\
["AND", [
conditions
]]'

func _init():
match_id = "AND"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ extends AbstractMatch

@export var negated_condition: AbstractMatch

static func json_format() -> String:
return '["NOT", condition]'

func setup(system_node: Node) -> void:
if negated_condition != null:
negated_condition.setup(system_node)

func json_format() -> String:
return '["NOT", condition]'

func to_json_repr() -> Variant:
# ["NOT", condition]
return [&"NOT", negated_condition.to_json_repr()]
Expand Down
6 changes: 0 additions & 6 deletions addons/rule_based_godot/resources/matches/boolean/OR_match.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
class_name ORMatch
extends MultiBoolMatch

static func json_format() -> String:
return '\
["OR", [
conditions
]]'

func _init():
match_id = "OR"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ func setup(system_node: Node) -> void:
if condition == null: continue
condition.setup(system_node)

func json_format() -> String:
# ["ID", [conditions]]
return '["' + match_id + '", [conditions]]'

func to_json_repr() -> Variant:
# ["ID", [conditions]]
var conditions_array := []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ var _area # Area2D or Area3D
var _overlapping := []

func _init():
match_id = "AreaDetection"
repr_vars = ["area_path"]
Data_Retrieval = false
preset_node_path("area_path", "_area")
pre_connect("_area", "area_entered", _add_overlapping)
Expand All @@ -20,9 +22,6 @@ func _add_overlapping(entity: Variant) -> void:
func _remove_overlapping(entity: Variant) -> void:
_overlapping.erase(entity)

static func json_format() -> String:
return '["AreaDetection", "area_node", ?var|"collider"]'

func _node_satisfies_match(target_node: Node, bindings: Dictionary) -> bool:
if target_node == null:
print_debug("Invalid Area Detection node")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ func _get_data(tester_node: Node) -> Variant:
return null

################################ JSON format ###################################
func json_format() -> String:
# ["ID", ("?data"), vars..., "?wild"|"tester", ("prop"|"method", [args])]
var string = '["' + match_id + '", ("?data")'
for variable in repr_vars:
string += ', ' + variable
string += ', "?wild"|"tester", ("prop"|"method", [args])]'
return string

func to_json_repr() -> Variant:
# ["ID", ("?data"), vars..., "?wild"|"tester", ("prop"|"method", [args])]
var json_array = [match_id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ func _init():
repr_vars = ["source_node_path", "relation"]
preset_node_path("source_node_path", "_source_node")

static func json_format() -> String:
return '["Hierarchy", "source_node", "(Parent|Sibling|Child) of", "?var|node"]'

func _get_candidates() -> Array[Node]:
var candidates = []
match relation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ extends DatumMatch
@export var min_value: float
@export var max_value: float

static func json_format() -> String:
return '["Numeric", min, max, "?var|node", "property" | "method", {"types": "arguments"}]'

func _init():
Data_Extraction = true
match_id = "Numeric"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ extends DatumMatch

@export var string_value: String = ""

static func json_format() -> String:
return '["String", "value", "?var|node", "property" | "method", {"types": "arguments"}]'

func _init():
Data_Extraction = true
match_id = "String"
Expand Down
12 changes: 6 additions & 6 deletions addons/rule_based_godot/resources/rule.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ extends RuleBasedResource

var _bindings: Dictionary # variable -> value

static func json_format() -> String:
func setup(system_node: Node) -> void:
condition.setup(system_node)
for action in actions:
action.setup(system_node)

func json_format() -> String:
return '\
{"if":
condition,
"then": [
actions
]}'

func setup(system_node: Node) -> void:
condition.setup(system_node)
for action in actions:
action.setup(system_node)

func to_json_repr() -> Variant:
# {"if": condition, "then": [actions]}
var actions_array := []
Expand Down
8 changes: 4 additions & 4 deletions addons/rule_based_godot/resources/rule_based_resource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class_name RuleBasedResource

var _system_node: Node

static func json_format() -> String:
func setup(system_node: Node) -> void:
_system_node = system_node

func json_format() -> String:
# Abstract method
push_error("Abstract method call")
return ""

func setup(system_node: Node) -> void:
_system_node = system_node

func to_json_repr() -> Variant:
# Abstract method
push_error("Abstract method call")
Expand Down
10 changes: 5 additions & 5 deletions addons/rule_based_godot/resources/rule_set.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ extends RuleBasedResource
@export var _rule_based_godot: StringName = "RuleSet"
@export var rules: Array[Rule]

static func json_format() -> String:
func setup(system_node: Node) -> void:
for rule in rules:
rule.setup(system_node)

func json_format() -> String:
return '\
{"Rules": [
rules
]}'

func setup(system_node: Node) -> void:
for rule in rules:
rule.setup(system_node)

func to_json_repr() -> Variant:
# {"Rules": [rules]}
var rules_array := []
Expand Down

0 comments on commit 770969f

Please sign in to comment.