Skip to content

Commit

Permalink
Implementaion of lc semantic check on path with only variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrzej Uszok committed Nov 16, 2023
1 parent 11eb9a2 commit 4ff5f21
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
17 changes: 13 additions & 4 deletions domiknows/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ def check_path(self, path, resultConcept, variableConceptParent, lc_name, foundV
pathVariable = path[0]
pathPart = path[0]

if len(path) == 1:
if requiredLeftConcept == requiredEndOfPathConceptRoot:
return
else:
exceptionStr1 = f"The variable {pathVariable}, defined in the path for {lc_name} is not valid. The concept of {pathVariable} is a of type {requiredLeftConcept},"
exceptionStr2 = f"but the required concept by the logical constraint element is {requiredEndOfPathConceptRoot}."
exceptionStr3 = f"The variable used inside the path should match its type with {requiredEndOfPathConceptRoot}."
raise Exception(f"{exceptionStr1} {exceptionStr2} {exceptionStr3}")

for pathIndex, pathElement in enumerate(path[1:], start=1):
if isinstance(pathElement, (eqL,)):
continue
Expand Down Expand Up @@ -701,14 +710,14 @@ def __exit__(self, exc_type, exc_value, traceback):

if isinstance(path[0], tuple): # this path is a combination of paths
for subpath in path:
if len(subpath) < 2:
continue # skip this subpath as it has only the starting point variable
if len(subpath) < 1:
continue # skip this subpath it is empty

self.check_path(subpath, resultConcept, variableConceptParent, headLcName, foundVariables, variableName)

else: # this path is a single path
if len(path) < 2:
continue # skip this path as it has only the starting point variable
if len(path) < 1:
continue # skip this path it is empty

self.check_path(path, resultConcept, variableConceptParent, headLcName, foundVariables, variableName)

Expand Down
6 changes: 4 additions & 2 deletions test_regr/graph_errors/graph_variable_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ def setup_graph(fix_constraint=False):
weather_details = Concept(name='weather_details')

# Task Concepts
accident_cause = accident_details(name='accident_cause', ConceptClass=EnumConcept, values=['human error', 'mechanical failure', 'road conditions', 'weather conditions', 'other'])
weather_condition = weather_details(name='weather_condition', ConceptClass=EnumConcept, values=['clear', 'cloudy', 'rainy', 'snowy', 'foggy', 'stormy', 'other'])
accident_cause = accident_details(name='accident_cause', ConceptClass=EnumConcept,
values=['human error', 'mechanical failure', 'road conditions', 'weather conditions', 'other'])
weather_condition = weather_details(name='weather_condition', ConceptClass=EnumConcept,
values=['clear', 'cloudy', 'rainy', 'snowy', 'foggy', 'stormy', 'other'])

# Constraints
# If the accident cause is weather condition, then the weather quality is not clear.
Expand Down
3 changes: 2 additions & 1 deletion test_regr/graph_errors/test_graph_variable_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def test_setup_graph_exception():
sanitized_pattern = re.sub(
r'[^\x20-\x7E]',
'',
("In constraint testLC, the variable x is defined to have the type accident_details but is later reused in weather_condition.__getattr__('clear')(path=('x')) which is incorrect!"
("The variable x, defined in the path for testLC is not valid. The concept of x is a of type accident_details,"
"but the required concept by the logical constraint element is weather_details."
"The variable used inside the path should match its type with weather_details.")
).replace(" ", "")

Expand Down

0 comments on commit 4ff5f21

Please sign in to comment.