11import pyreason .scripts .numba_wrapper .numba_types .interval_type as interval
22import re
33
4- _IDENTIFIER_RE = re .compile (r'^[a-zA-Z_][a-zA-Z0-9_.\-]*$' )
4+ _PREDICATE_RE = re .compile (r'^[a-zA-Z_][a-zA-Z0-9_.\-]*$' )
5+ _COMPONENT_RE = re .compile (r'^[a-zA-Z0-9_][a-zA-Z0-9_.\-]*$' )
56
67
7- def _validate_name (name , context ):
8- """Validate that a predicate or component name matches _IDENTIFIER_RE ."""
8+ def _validate_predicate (name ):
9+ """Validate that a predicate name starts with a letter/underscore ."""
910 if not name :
10- raise ValueError (f" { context } name cannot be empty" )
11- if not _IDENTIFIER_RE .match (name ):
11+ raise ValueError ("Predicate name cannot be empty" )
12+ if not _PREDICATE_RE .match (name ):
1213 if name [0 ].isdigit ():
13- raise ValueError (f"{ context } name '{ name } ' cannot start with a digit. Must start with a letter or underscore" )
14+ raise ValueError (f"Predicate name '{ name } ' cannot start with a digit. Must start with a letter or underscore" )
1415 else :
15- raise ValueError (f"{ context } name '{ name } ' contains invalid characters. Must match [a-zA-Z_][a-zA-Z0-9_.\\ -]*" )
16+ raise ValueError (f"Predicate name '{ name } ' contains invalid characters. Must match [a-zA-Z_][a-zA-Z0-9_.\\ -]*" )
17+
18+
19+ def _validate_component (name , context ):
20+ """Validate that a component (entity) name contains only valid characters. May start with a digit."""
21+ if not name :
22+ raise ValueError (f"{ context } name cannot be empty" )
23+ if not _COMPONENT_RE .match (name ):
24+ raise ValueError (f"{ context } name '{ name } ' contains invalid characters. Must match [a-zA-Z0-9_][a-zA-Z0-9_.\\ -]*" )
1625
1726
1827# Input validation work was implemented with the help of Claude Sonnet 4.5.
@@ -89,7 +98,7 @@ def parse_fact(fact_text):
8998 component = pred_comp [idx + 1 :- 1 ]
9099
91100 # Validate predicate name
92- _validate_name (pred , "Predicate" )
101+ _validate_predicate (pred )
93102
94103 # Validate component is not empty
95104 if not component :
@@ -106,12 +115,12 @@ def parse_fact(fact_text):
106115
107116 # Validate component names
108117 for i , comp in enumerate (components ):
109- _validate_name (comp , f"Edge component { i + 1 } " )
118+ _validate_component (comp , f"Edge component { i + 1 } " )
110119
111120 component = tuple (components )
112121 else :
113122 fact_type = 'node'
114- _validate_name (component , "Node component" )
123+ _validate_component (component , "Node component" )
115124
116125 # Check if bound is a boolean or a list of floats
117126 if bound .lower () == 'true' :
0 commit comments