
    ni                         S r SSKrSSKrSSKrSSKrSSKrSSKJr  SSK	r
SSKJr  SSKJr   " S S5      r " S S	5      r " S
 S5      r " S S5      rg)z
Unit tests for pyreason reasoning functionality.
Tests the _reason() function through the reason() public API to achieve maximum branch coverage.
    N)StringIO)Rule)Factc                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)TestReasoningFunction   z@Test reasoning functionality with comprehensive branch coverage.c                 X    [         R                  " 5         [         R                  " 5         gzClean state before each test.Nprresetreset_settingsselfs    a/home/james-whalen/.local/lib/python3.13/site-packages/tests/api_tests/test_pyreason_reasoning.pysetup_method"TestReasoningFunction.setup_method       


    c                     [         R                  " [         R                  " SS5      5        [        R                  " [
        SS9   [         R                  " 5       nSSS5        g! , (       d  f       g= f)z7Test reasoning without graph uses empty graph and warnsztest(x) <- test2(x)	test_rulezGraph not loadedmatchN)r   add_ruler   pytestwarnsUserWarningreason)r   interpretations     r   *test_reason_without_graph_uses_empty_graph@TestReasoningFunction.test_reason_without_graph_uses_empty_graph   sA    
BGG1;?@\\+-?@YY[N A@@s   A$$
A2c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [
        R                  " [        SS9   [        R                  " SS9  SSS5        g! , (       d  f       g= f)z2Test reasoning without any rules raises exception.ABzThere are no rulesr      	timestepsN)	nxDiGraphadd_edger   
load_graphr   raises	Exceptionr   )r   graphs     r   *test_reason_with_no_rules_raises_exception@TestReasoningFunction.test_reason_with_no_rules_raises_exception!   sO    

sC 
e]]9,@AII" BAAs   A55
Bc                 :   [         R                  " [         R                  " S5      5        [         R                  " [         R                  " S5      5        [         R                  " 5       nUS   R	                  5       S:X  d   eUS   R	                  5       S:X  d   eg)z4Test that rules get auto-named when no name providedztest1(x) <- test2(x)ztest3(x) <- test4(x)r   rule_0r%   rule_1N)r   r   r   	get_rulesget_rule_name)r   ruless     r   test_reason_auto_names_rules2TestReasoningFunction.test_reason_auto_names_rules*   so    
BGG234
BGG234Qx%%'8333Qx%%'8333r   c                 <   [         R                  " S5      n[         R                  " S5      n[         R                  " U5        [         R                  " U5        UR                  R	                  S5      (       d   eUR                  R	                  S5      (       d   eg)z4Test that facts get auto-named when no name providedztest(node1)ztest(node1, node2)fact_N)r   r   add_factname
startswith)r   fact1fact2s      r   test_reason_auto_names_facts2TestReasoningFunction.test_reason_auto_names_facts3   so    &,-
E
E zz$$W----zz$$W----r   c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        S[        R                  l	         [        R                  " SS	9nUc   e S[        R                  l        [        R                  " S5       H1  nUR                  S5      (       d  M  [        R                  " U5        M3     g
! S[        R                  l        [        R                  " S5       H1  nUR                  S5      (       d  M  [        R                  " U5        M3     f = f)z+Test reasoning with output_to_file setting.r#   r$   friend(A, B) <- connected(A, B)r   FTtest_outputr%   r&   N.test_output_)r(   r)   r*   r   r+   r   r   settingsoutput_to_fileoutput_file_namer   oslistdirr=   remove)r   r.   r   files       r   test_reason_with_output_to_file5TestReasoningFunction.test_reason_with_output_to_file?   s    

sC 
e
D:KOP%)"'4$		$YY3N!---).BKK&

3??>22IIdO ( */BKK&

3??>22IIdO (s   
D AE'E'c                     [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   eg)	z)Test reasoning when __node_facts is None.r#   r$   rC   r   Fr%   r&   Nr(   r)   r*   r   r+   r   r   r   r   r.   r   s      r   7test_reason_with_none_node_facts_initializes_empty_listMTestReasoningFunction.test_reason_with_none_node_facts_initializes_empty_listT   Y    

sC 
e
D:KOP Q/)))r   c                     [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   eg)	z)Test reasoning when __edge_facts is None.r#   r$   rC   r   Fr%   r&   NrQ   rR   s      r   7test_reason_with_none_edge_facts_initializes_empty_listMTestReasoningFunction.test_reason_with_none_edge_facts_initializes_empty_list_   rU   r   c                     [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   eg)	z"Test reasoning when __ipl is None.r#   r$   rC   r   Fr%   r&   NrQ   rR   s      r   0test_reason_with_none_ipl_initializes_empty_listFTestReasoningFunction.test_reason_with_none_ipl_initializes_empty_listj   rU   r   c                 \   [         R                  " 5       nUR                  SSS9  UR                  SSS9  UR                  SS5        S[        R
                  l        [        R                  " U5        [        R                  " [        SSS5      5        [        R                  " SS	9nUc   eg
)z/Test reasoning with specific graph node labels.r#   Tpersonr$   rC   r   Fr%   r&   N)r(   r)   add_noder*   r   rG   graph_attribute_parsingr+   r   r   r   rR   s      r   +test_reason_with_specific_graph_node_labelsATestReasoningFunction.test_reason_with_specific_graph_node_labelsu   s    

s4(s4(sC .2+
e
D:KOPQ/)))r   c                    [         R                  " 5       nUR                  SSSS9  S[        R                  l        [        R                  " U5        [        R                  " [        SSS5      5        [        R                  " SS	9nUc   eg
)z/Test reasoning with specific graph edge labels.r#   r$   Tknowsfriend(A, B) <- knows(A, B)r   Fr%   r&   N
r(   r)   r*   r   rG   r`   r+   r   r   r   rR   s      r   +test_reason_with_specific_graph_edge_labelsATestReasoningFunction.test_reason_with_specific_graph_edge_labels   g    

sCt,.2+
e
D6UKLQ/)))r   c                    [         R                  " 5       nUR                  SSS9  UR                  SSS9  UR                  SS5        S[        R
                  l        [        R                  " U5        [        R                  " [        SSS5      5        [        R                  " [        SS	S
S5      5        [        R                  " SS9nUc   eg)zHTest reasoning when specific node labels already exist and get extended.r#   Tr]   r$   z$friend(A, B) <- person(A), person(B)r   Fzperson("C")person_cr   r%   r&   N)r(   r)   r_   r*   r   rG   r`   r+   r   r   r;   r   r   rR   s      r   .test_reason_with_existing_specific_node_labelsDTestReasoningFunction.test_reason_with_existing_specific_node_labels   s    

s4(s4(sC .2+
e
D?eTU 	D
Aq9:Q/)))r   c                    [         R                  " 5       nUR                  SSSS9  S[        R                  l        [        R                  " U5        [        R                  " [        SSS5      5        [        R                  " SS	9nUc   eg
)zHTest reasoning when specific edge labels already exist and get extended.r#   r$   Trd   rf   r   Fr%   r&   Nrg   rR   s      r   .test_reason_with_existing_specific_edge_labelsDTestReasoningFunction.test_reason_with_existing_specific_edge_labels   rj   r   c                 F   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        S[        R                  l	        [        R                  " SS9nUc   eg	)
zPTest that atom_trace is set to False when store_interpretation_changes is False.r#   r$   rC   r   FTr%   r&   N)r(   r)   r*   r   r+   r   r   rG   store_interpretation_changes
atom_tracer   rR   s      r   Itest_reason_with_store_interpretation_changes_false_sets_atom_trace_false_TestReasoningFunction.test_reason_with_store_interpretation_changes_false_sets_atom_trace_false   so    

sC 
e
D:KOP380!%Q/)))r   c                 <   SSK Jn  [        R                  " 5       nUR	                  SS5        [
        R                  " U5        [
        R                  " [        SSS5      5        S[
        R                  l
        U" S	5      /n[
        R                  " S
US9nUc   eg)z5Test reasoning with verbose mode and query filtering.r   )Queryr#   r$   rC   r   FTzfriend(A, B)r%   r'   queriesN)pyreason.scripts.query.queryrx   r(   r)   r*   r   r+   r   r   rG   verboser   )r   rx   r.   rz   r   s        r   *test_reason_with_verbose_queries_filtering@TestReasoningFunction.test_reason_with_verbose_queries_filtering   sw    6

sC 
e
D:KOP" ()Q@)))r   c                     [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SSS9nUc   eg)	z3Test reasoning when queries is None (no filtering).r#   r$   rC   r   Fr%   Nry   rQ   rR   s      r   test_reason_with_queries_none3TestReasoningFunction.test_reason_with_queries_none   sY    

sC 
e
D:KOPQ=)))r   c                 Z   [         R                  " 5       nUR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        5       n[        R                  n U[        l        [        R                  " S	S
9nUR                  5       nSU;   d  Uc   eU[        l        g! U[        l        f = f)zQTest reasoning when graph has more edges than nodes (triggers rule optimization).r#   r$   CDrC   r   FTr%   r&   zOptimizing rulesN)r(   r)   r*   r   r+   r   r   rG   r|   r   sysstdoutr   getvalue)r   r.   captured_outputoriginal_stdoutr   outputs         r   6test_reason_with_more_edges_than_nodes_optimizes_rulesLTestReasoningFunction.test_reason_with_more_edges_than_nodes_optimizes_rules   s    

sC sC sC sC sC 
e
D:KOP" #***	)(CJYY3N$--/F &/>3MMM(CJCJs   :D D*c                 z   [         R                  " 5       nUR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  SS5        [        R
                  " U5        [        R                  " [        SSS5      5        [        R                  " SS	9nUc   eg
)zKTest reasoning when graph has more nodes than edges (no rule optimization).r#   r$   r   r   rC   r   Fr%   r&   N)	r(   r)   r_   r*   r   r+   r   r   r   rR   s      r   6test_reason_with_more_nodes_than_edges_no_optimizationLTestReasoningFunction.test_reason_with_more_nodes_than_edges_no_optimization   s    

sssssC 
e
D:KOPQ/)))r   c                 B   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        S[        R                  l	        S[        R                  l
        S[        R                  l        S[        R                  l        S[        R                  l        S[        R                  l        S[        R                  l        [        R                   " SS9nUc   eg	)
z-Test reasoning with various settings enabled.r#   r$   rC   r   FTr%   r&   N)r(   r)   r*   r   r+   r   r   rG   reverse_digraphrt   save_graph_attributes_to_trace
persistentinconsistency_checkrs   parallel_computingallow_ground_rulesr   rR   s      r   %test_reason_with_all_settings_enabled;TestReasoningFunction.test_reason_with_all_settings_enabled   s    

sC 
e
D:KOP '+#!%592!%*.'370).&)-&Q/)))r   c                     [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SSSS	9nUc   eg
)z>Test reasoning with convergence threshold and bound threshold.r#   r$   rC   r   F   {Gz?皙?)r'   convergence_thresholdconvergence_bound_thresholdNrQ   rR   s      r   'test_reason_with_convergence_parameters=TestReasoningFunction.test_reason_with_convergence_parameters  sb    

sC 
e
D:KOP"&(+

 )))r   c                 h   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " [        SSSS	5      5        [        R                  " S	S
9nUc   e[        R                  " S	S
9nUc   eg)z:Test that node and edge facts are cleared after reasoning.r#   r$   rC   r   Fperson("A")person_ar   r%   r&   N)
r(   r)   r*   r   r+   r   r   r;   r   r   )r   r.   r   interpretation2s       r   (test_reason_clears_facts_after_reasoning>TestReasoningFunction.test_reason_clears_facts_after_reasoning  s    

sC 
e
D:KOP 	D
Aq9:Q/))) ))a0***r   c                 .   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S H1  nU[        R                  l        [        R                  " SS9nUb  M1   e   g	)
z+Test reasoning with different update modes.r#   r$   rC   r   F)SynchronousAsynchronousr%   r&   N)
r(   r)   r*   r   r+   r   r   rG   update_moder   )r   r.   r   r   s       r   'test_reason_with_different_update_modes=TestReasoningFunction.test_reason_with_different_update_modes0  so    

sC 
e
D:KOP ;K&1BKK#YY3N!--- ;r   c                    [         R                  " 5       nUR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        UR                  SS5        [        R                  " U5        [        R
                  " [        SSS	5      5        [        R                  " S
S9nUc   eg)zGTest reasoning with complex rules that might trigger clause reordering.r#   r$   r   r   EFz5friend(A, B) <- connected(A, B), person(A), person(B)complex_ruleFr%   r&   NrQ   rR   s      r   'test_reason_with_complex_rule_structure=TestReasoningFunction.test_reason_with_complex_rule_structure=  s    

sC sC sC sC sC 
e 	DPR`bghiQ/)))r    N)__name__
__module____qualname____firstlineno____doc__r   r    r/   r7   r@   rN   rS   rW   rZ   ra   rh   rm   rp   ru   r}   r   r   r   r   r   r   r   r   __static_attributes__r   r   r   r   r      sx    J
)#4
.$*	*	*	**	** 	*** *):* *(*+".*r   r   c                   *    \ rS rSrSrS rS rS rSrg)TestReasonAgainFunctioniO  z?Test _reason_again functionality through multiple reason calls.c                 X    [         R                  " 5         [         R                  " 5         gr
   r   r   s    r   r   $TestReasonAgainFunction.setup_methodR  r   r   c                 h   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   e[        R                  " [        S	S
SS5      5        [        R                  " SS9nUc   eg)z'Test multiple calls to reason function.r#   r$   rC   r   Fr%   r&   Nr   r   r   
r(   r)   r*   r   r+   r   r   r   r;   r   r   r.   interpretation1r   s       r   test_multiple_reason_calls2TestReasonAgainFunction.test_multiple_reason_callsW  s    

sC 
e
D:KOP ))a0*** 	D
Aq9:))a0***r   c                 &   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   e[        R                  " S	S
S9nUc   eg)z&Test reasoning with restart parameter.r#   r$   rC   r   Fr%   r&   N   T)r'   restartrQ   r   s       r   "test_reason_with_restart_parameter:TestReasonAgainFunction.test_reason_with_restart_parameterg  sw    

sC 
e
D:KOP ))a0*** ))a>***r   r   N)	r   r   r   r   r   r   r   r   r   r   r   r   r   r   O  s    I
+ +r   r   c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS rS rSrg)TestFilterAndSortFunctionsiv  z?Test filter_and_sort_nodes and filter_and_sort_edges functions.c                 X    [         R                  " 5         [         R                  " 5         gr
   r   r   s    r   r   'TestFilterAndSortFunctions.setup_methody  r   r   c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " [        SS	9   [        R                  " US
/5        SSS5        g! , (       d  f       g= f)zQTest that filter_and_sort_nodes requires store_interpretation_changes to be True.r#   r$   rC   r   Fr%   r&   +store interpretation changes setting is offr   friendN)r(   r)   r*   r   r+   r   r   rG   rs   r   r   r,   AssertionErrorfilter_and_sort_nodesrR   s      r   @test_filter_and_sort_nodes_requires_store_interpretation_changes[TestFilterAndSortFunctions.test_filter_and_sort_nodes_requires_store_interpretation_changes~      

sC 
e
D:KOP380Q/]]>1^_$$^hZ@ `__   !C
Cc                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " [        SS	9   [        R                  " US
/5        SSS5        g! , (       d  f       g= f)zQTest that filter_and_sort_edges requires store_interpretation_changes to be True.r#   r$   rC   r   Fr%   r&   r   r   r   N)r(   r)   r*   r   r+   r   r   rG   rs   r   r   r,   r   filter_and_sort_edgesrR   s      r   @test_filter_and_sort_edges_requires_store_interpretation_changes[TestFilterAndSortFunctions.test_filter_and_sort_edges_requires_store_interpretation_changes  r   r   c                    [         R                  " 5       nUR                  SS5        UR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS	9n[        R                  " US
/5      nUc   e[        U[        5      (       d   eg)z2Test basic functionality of filter_and_sort_nodes.r#   r$   r   rC   r   FTr   r&   r   N)r(   r)   r*   r   r+   r   r   rG   rs   r   r   
isinstancelistr   r.   r   results       r   .test_filter_and_sort_nodes_basic_functionalityITestFilterAndSortFunctions.test_filter_and_sort_nodes_basic_functionality      

sC sC 
e
D:KOP370Q/ )).8*E!!!&$''''r   c                    [         R                  " 5       nUR                  SS5        UR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS	9n[        R                  " US
/5      nUc   e[        U[        5      (       d   eg)z2Test basic functionality of filter_and_sort_edges.r#   r$   r   rC   r   FTr   r&   r   N)r(   r)   r*   r   r+   r   r   rG   rs   r   r   r   r   r   s       r   .test_filter_and_sort_edges_basic_functionalityITestFilterAndSortFunctions.test_filter_and_sort_edges_basic_functionality  r   r   c                    SSK Js  Js  Js  Jn  [
        R                  " 5       nUR                  SS5        [        R                  " U5        [        R                  " [        SSS5      5        S[        R                  l        [        R                  " S	S
9nUR                  SS5      n[        R                   " US/US9nUc   eg)z6Test filter_and_sort_nodes with custom interval bound.r   Nr#   r$   rC   r   FTr   r&         ?      ?r   bound)8pyreason.scripts.numba_wrapper.numba_types.interval_typescriptsnumba_wrappernumba_typesinterval_typer(   r)   r*   r   r+   r   r   rG   rs   r   closedr   r   intervalr.   r   custom_boundr   s         r   ,test_filter_and_sort_nodes_with_custom_boundGTestFilterAndSortFunctions.test_filter_and_sort_nodes_with_custom_bound      SS

sC 
e
D:KOP370Q/  sC0)).8*LY!!!r   c                    SSK Js  Js  Js  Jn  [
        R                  " 5       nUR                  SS5        [        R                  " U5        [        R                  " [        SSS5      5        S[        R                  l        [        R                  " S	S
9nUR                  SS5      n[        R                   " US/US9nUc   eg)z6Test filter_and_sort_edges with custom interval bound.r   Nr#   r$   rC   r   FTr   r&   r   r   r   r   )r   r   r   r   r   r(   r)   r*   r   r+   r   r   rG   rs   r   r   r   r   s         r   ,test_filter_and_sort_edges_with_custom_boundGTestFilterAndSortFunctions.test_filter_and_sort_edges_with_custom_bound  r   r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/S
S9nUc   eg)z0Test filter_and_sort_nodes with sort_by='upper'.r#   r$   rC   r   FTr   r&   r   uppersort_byNr(   r)   r*   r   r+   r   r   rG   rs   r   r   r   s       r   (test_filter_and_sort_nodes_sort_by_upperCTestFilterAndSortFunctions.test_filter_and_sort_nodes_sort_by_upper  {    

sC 
e
D:KOP370Q/ )).8*gV!!!r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/S
S9nUc   eg)z0Test filter_and_sort_edges with sort_by='upper'.r#   r$   rC   r   FTr   r&   r   r   r   Nr(   r)   r*   r   r+   r   r   rG   rs   r   r   r   s       r   (test_filter_and_sort_edges_sort_by_upperCTestFilterAndSortFunctions.test_filter_and_sort_edges_sort_by_upper  r   r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/S
S9nUc   eg)z0Test filter_and_sort_nodes with sort_by='lower'.r#   r$   rC   r   FTr   r&   r   lowerr   Nr   r   s       r   (test_filter_and_sort_nodes_sort_by_lowerCTestFilterAndSortFunctions.test_filter_and_sort_nodes_sort_by_lower  r   r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/S
S9nUc   eg)z0Test filter_and_sort_edges with sort_by='lower'.r#   r$   rC   r   FTr   r&   r   r  r   Nr  r   s       r   (test_filter_and_sort_edges_sort_by_lowerCTestFilterAndSortFunctions.test_filter_and_sort_edges_sort_by_lower  r   r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/SS
9nUc   eg)z0Test filter_and_sort_nodes with ascending order.r#   r$   rC   r   FTr   r&   r   
descendingNr   r   s       r   *test_filter_and_sort_nodes_ascending_orderETestFilterAndSortFunctions.test_filter_and_sort_nodes_ascending_order  |    

sC 
e
D:KOP370Q/ )).8*QVW!!!r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/SS
9nUc   eg)z0Test filter_and_sort_edges with ascending order.r#   r$   rC   r   FTr   r&   r   r  Nr  r   s       r   *test_filter_and_sort_edges_ascending_orderETestFilterAndSortFunctions.test_filter_and_sort_edges_ascending_order"  r  r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/SS
9nUc   eg)z1Test filter_and_sort_nodes with descending order.r#   r$   rC   r   FTr   r&   r   r  Nr   r   s       r   +test_filter_and_sort_nodes_descending_orderFTestFilterAndSortFunctions.test_filter_and_sort_nodes_descending_order0  |    

sC 
e
D:KOP370Q/ )).8*QUV!!!r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/SS
9nUc   eg)z1Test filter_and_sort_edges with descending order.r#   r$   rC   r   FTr   r&   r   r  Nr  r   s       r   +test_filter_and_sort_edges_descending_orderFTestFilterAndSortFunctions.test_filter_and_sort_edges_descending_order>  r  r   c                    [         R                  " 5       nUR                  SS5        UR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R
                  " [        SSS5      5        S	[        R                  l        [        R                  " S
S9n[        R                  " USS/5      nUc   eg)z0Test filter_and_sort_nodes with multiple labels.r#   r$   r   rC   rule1Fenemy(A, B) <- ~friend(A, B)rule2Tr   r&   r   enemyNr   r   s       r   *test_filter_and_sort_nodes_multiple_labelsETestFilterAndSortFunctions.test_filter_and_sort_nodes_multiple_labelsL      

sC sC 
e
D:GUKL
D7%HI370Q/ )).8W:MN!!!r   c                    [         R                  " 5       nUR                  SS5        UR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R
                  " [        SSS5      5        S	[        R                  l        [        R                  " S
S9n[        R                  " USS/5      nUc   eg)z0Test filter_and_sort_edges with multiple labels.r#   r$   r   rC   r  Fr  r  Tr   r&   r   r   Nr  r   s       r   *test_filter_and_sort_edges_multiple_labelsETestFilterAndSortFunctions.test_filter_and_sort_edges_multiple_labels\  r#  r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " U/ 5      nUc   eg	)
z2Test filter_and_sort_nodes with empty labels list.r#   r$   rC   r   FTr   r&   Nr   r   s       r   ,test_filter_and_sort_nodes_empty_labels_listGTestFilterAndSortFunctions.test_filter_and_sort_nodes_empty_labels_listl  w    

sC 
e
D:KOP370Q/ ))."=!!!r   c                 J   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " U/ 5      nUc   eg	)
z2Test filter_and_sort_edges with empty labels list.r#   r$   rC   r   FTr   r&   Nr  r   s       r   ,test_filter_and_sort_edges_empty_labels_listGTestFilterAndSortFunctions.test_filter_and_sort_edges_empty_labels_listz  r*  r   c                 z   [         R                  " 5       nUR                  SS5        UR                  SS5        UR                  SS5        UR                  S5        [        R
                  " U5        [        R                  " [        SSS5      5        [        R                  " [        S	S
S5      5        [        R                  " [        SSSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " U/ SQSSS9nUc   e[        R                  " USS/SSS9nUc   eg)z;Test both functions with a more complex reasoning scenario.r#   r$   r   r   r   rC   r  Fz"close_friend(A, B) <- friend(A, B)r  r   r>   r   r   T   r&   )r   close_friendr^   r  )r   r  Nr   r0  r   )r(   r)   r*   r_   r   r+   r   r   r;   r   rG   rs   r   r   r   r   r.   r   node_resultedge_results        r   4test_filter_and_sort_functions_with_complex_scenarioOTestFilterAndSortFunctions.test_filter_and_sort_functions_with_complex_scenario  s   

sC sC sC s
e 	D:GUKL
D=wNO
DA67370Q/ ..0	
 &&& ..~&	
 &&&r   c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SS9n[        R                  " US	/5      n[        R                  " US	/5      nUc   eUc   eg
)z;Test filter_and_sort functions with all default parameters.r#   r$   rC   r   FTr   r&   r   N)r(   r)   r*   r   r+   r   r   rG   rs   r   r   r   r1  s        r   0test_filter_and_sort_with_all_default_parametersKTestFilterAndSortFunctions.test_filter_and_sort_with_all_default_parameters  s    

sC 
e
D:KOP370Q/ ..~zJ..~zJ&&&&&&r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r
  r  r  r  r  r!  r%  r(  r,  r4  r7  r   r   r   r   r   r   v  sq    I
AA("("""""""""""""" " ""!'F'r   r   c                   Z    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rSrg)TestReasonFunctionBranchesi  z?Test missing branches in reason() and reason_again() functions.c                 X    [         R                  " 5         [         R                  " 5         gr
   r   r   s    r   r   'TestReasonFunctionBranches.setup_method  r   r   c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l         [        R                  " SSS9nUc   e S[        R                  l        g	! S[        R                  l        f = f)
z7Test reason() with memory_profile=True and again=False.r#   r$   rC   r   FTr%   r'   againN
r(   r)   r*   r   r+   r   r   rG   memory_profiler   rR   s      r   2test_reason_with_memory_profile_enabled_first_timeMTestReasonFunctionBranches.test_reason_with_memory_profile_enabled_first_time  s    

sC 
e
D:KOP &*"	/  YY%@N!---).BKK&BKK&s   5B& &B=c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   e[        R                  " [        S	S
SS5      5        S[        R                  l         [        R                  " SSSS9nUc   e S[        R                  l        g! S[        R                  l        f = f)z6Test reason() with memory_profile=True and again=True.r#   r$   rC   r   Fr%   r&   Nr   r   r   Tr'   r?  r   r(   r)   r*   r   r+   r   r   r   r;   r   rG   rA  r   s       r   2test_reason_with_memory_profile_enabled_again_trueMTestReasonFunctionBranches.test_reason_with_memory_profile_enabled_again_true  s    

sC 
e
D:KOP ))a0*** 	D
Aq9: &*"	/ !ii!4NO"...).BKK&BKK&s   0C" "C9c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SSS9nUc   eg)	z8Test reason() with memory_profile=False and again=False.r#   r$   rC   r   Fr%   r>  Nr@  rR   s      r   3test_reason_with_memory_profile_disabled_first_timeNTestReasonFunctionBranches.test_reason_with_memory_profile_disabled_first_time  si    

sC 
e
D:KOP &+" Qe<)))r   c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   e[        R                  " [        S	S
SS5      5        S[        R                  l        [        R                  " SSSS9nUc   eg)z7Test reason() with memory_profile=False and again=True.r#   r$   rC   r   Fr%   r&   Nr   r   r   TrE  rF  r   s       r   3test_reason_with_memory_profile_disabled_again_trueNTestReasonFunctionBranches.test_reason_with_memory_profile_disabled_again_true  s    

sC 
e
D:KOP ))a0*** 	D
Aq9: &+" ))atTJ***r   c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SSS9nUc   e[        R                  " [        S	S
SS5      5        [        R                  " SSSS9nUc   e[        R                  " [        SSSS5      5        [        R                  " SSSS9nUc   eg)z:Test reason() with different again parameter combinations.r#   r$   rC   r   Fr%   r>  Nr   r   r   TrE  person("B")person_br   )r   r.   r   r   interpretation3s        r   (test_reason_again_parameter_combinationsCTestReasonFunctionBranches.test_reason_again_parameter_combinations  s    

sC 
e
D:KOP ))au=*** 	D
Aq9: ))atTJ*** 	D
Aq9: ))atUK***r   c                     [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SSS9nUc   eg)	zTTest that on fresh start with again=True, it uses _reason because __program is None.r#   r$   rC   r   Fr%   r>  NrQ   rR   s      r   2test_reason_first_call_with_again_true_uses_reasonMTestReasonFunctionBranches.test_reason_first_call_with_again_true_uses_reason.  s]     

sC 
e
D:KOP Qe<)))r   c                 l   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   e[        R                  " [        S	S
SS5      5        [        R                  " SSSS9nUc   eg)zGTest branches within _reason_again function through reason(again=True).r#   r$   rC   r   Fr%   r&   Nr   r   r   r   TrE  r   r   s       r   ,test_reason_again_internal_function_branchesGTestReasonFunctionBranches.test_reason_again_internal_function_branches@  s    

sC 
e
D:KOP ))a0*** 	D
Aq9: ))atTJ***r   c                    [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   e[        R                  " [        S	S
SS5      5        S[        R                  l         [        R                  " SSSSSS9nUc   e S[        R                  l        g! S[        R                  l        f = f)zCTest _reason_again with verbose setting to exercise all parameters.r#   r$   rC   r   Fr%   r&   Nr   r   r   Tr   r   r   )r'   r?  r   r   r   )r(   r)   r*   r   r+   r   r   r   r;   r   rG   r|   r   s       r   &test_reason_again_with_verbose_settingATestReasonFunctionBranches.test_reason_again_with_verbose_settingR  s    

sC 
e
D:KOP ))a0*** 	D
Aq9: #	( ii&*,/O #..."'BKK%BKKs   0C$ $C;c                    [         R                  " 5       nUR                  SS5        UR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        S[        R                  l        [        R                  " SSS9nUc   eS
[        R                  l         [        R                  " SSS9nUc   e S[        R                  l        [        R                  " [        SSSS5      5        S[        R                  l        [        R                  " SS
S9nUc   e[        R                  " [        SSSS5      5        S
[        R                  l         [        R                  " SS
S9nUc   e S[        R                  l        g	! S[        R                  l        f = f! S[        R                  l        f = f)zLTest reason() with various parameter combinations to ensure branch coverage.r#   r$   r   rC   r   Fr%   r>  NTr   r   r   rP  rQ  )r(   r)   r*   r   r+   r   r   rG   rA  r   r;   r   )r   r.   r   r   rR  interpretation4s         r   +test_reason_with_all_parameter_combinationsFTestReasonFunctionBranches.test_reason_with_all_parameter_combinationsp  sf   

sC sC 
e
D:KOP &+"))au=*** &*"	/ ii!5AO"...).BKK& 	D
Aq9: &+"))at<*** 	D
Aq9: &*"	/ ii!4@O"...).BKK&' */BKK&& */BKK&s   6F  /F:  F7:Gc                 j   [         R                  " 5       nUR                  SS5        [        R                  " U5        [        R
                  " [        SSS5      5        [        R                  " SS9nUc   e[        R                  " [        S	S
SS5      5        [        R                  " SSS9nUc   eg)zLTest that _reason_again assert is covered by trying without prior reasoning.r#   r$   rC   r   Fr%   r&   Nr   r   r   Tr>  r   r   s       r   !test_reason_again_assert_coverage<TestReasonFunctionBranches.test_reason_again_assert_coverage  s     

sC 
e
D:KOP ))a0*** 	D
Aq9: ))at<***r   r   N)r   r   r   r   r   r   rB  rG  rJ  rM  rS  rV  rY  r\  r`  rc  r   r   r   r   r:  r:    s=    I
/$/2*+,+2*$+$(<&/P+r   r:  )r   r   networkxr(   tempfilerJ   r   ior   pyreasonr   pyreason.scripts.rules.ruler   pyreason.scripts.facts.factr   r   r   r   r:  r   r   r   <module>rk     sX   
    	 
   , ,{* {*|	%+ %+ND' D'N
o+ o+r   