influence_properties.pl 3.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
:- module(
  influence_properties,
  [
    % Commands
    list_stable_states/0,
    list_tscc_candidates/0
    % Public API
  ]
).

:- reexport(library(clpb)).
:- use_module(library(assoc)).


list_stable_states :-
   biocham_command,
   doc('
      lists stable steady states of the state transition graph corresponding
      to the positive semantics of the current influence model.
   '),
   is_influence_model,
   enumerate_objects(Objects),
   create_bool_assoc(Objects, BoolState),
   add_stable_constraints(BoolState),
   assoc_to_values(BoolState, Bools),
   forall(
      labeling(Bools),
      (
         write_bool_state(BoolState),
         nl
      )
   ).


list_tscc_candidates :-
   biocham_command,
   doc('
      lists possible representative states of Terminal Strongly Connected
      Components (TSCC) of the state transition graph corresponding to the 
      positive semantics of the current influence model.
   '),
   is_influence_model,
   enumerate_objects(Objects),
   create_bool_assoc(Objects, BoolState),
   add_tscc_constraints(BoolState),
   assoc_to_values(BoolState, Bools),
   forall(
      labeling(Bools),
      (
         write_bool_state(BoolState),
         (
            has_enabled_effective('-', BoolState, _)
         ->
            (
               has_enabled_effective('-', BoolState, NewBoolState),
               \+ has_enabled_effective('+', NewBoolState, BoolState)
            ->
               write(' not terminal')
            ;
               true
            )
         ;
            write(' stable')
         ),
         nl
      )
   ).


add_stable_constraints(BoolState) :-
   findall(
      (PositiveInputs, Sign, Output),
      (
         item([kind: influence, item: Item]),
         influence(Item, _Force, PositiveInputs, _NegativeInputs, Sign,
            Output)
      ),
      InfluenceTriples
   ),
   add_stable_constraint(InfluenceTriples, BoolState).


add_tscc_constraints(BoolState) :-
   findall(
      (PositiveInputs, Sign, Output),
      (
         item([kind: influence, item: Item]),
         influence(Item, _Force, PositiveInputs, _NegativeInputs, Sign,
            Output),
         Sign = '+'
      ),
      InfluenceTriples
   ),
   add_stable_constraint(InfluenceTriples, BoolState).


add_stable_constraint([], _).

add_stable_constraint([(PositiveInputs, Sign, Output) | InfluenceTriples], BoolState) :-
   get_assoc_list(PositiveInputs, BoolState, BoolPositiveInputs),
   get_assoc(Output, BoolState, BoolOutput),
   (
      Sign = '+'
   ->
      sat(~(*(BoolPositiveInputs)) + BoolOutput)
   ;
      sat(~(*(BoolPositiveInputs)) + ~(BoolOutput))
   ),
   add_stable_constraint(InfluenceTriples, BoolState).


get_assoc_list([], _, []).

get_assoc_list([H | T], Assoc, [V | U]) :-
   get_assoc(H, Assoc, V),
   get_assoc_list(T, Assoc, U).


create_bool_assoc(Objects, Assoc) :-
   create_var_list(Objects, BoolList),
   list_to_assoc(BoolList, Assoc).


create_var_list([], []).

create_var_list([H | T], [H-_ | U]) :-
   create_var_list(T, U).


write_bool_state(BoolState) :-
   assoc_to_list(BoolState, BoolList),
   write(BoolList).


enumerate_objects(Objects) :-
   setof(
      Object,
      enumerate_object(Object),
      Objects
   ).


enumerate_object(Object) :-
   item([kind: influence, item: Item]),
   influence(Item, _Force, PositiveInputs, NegativeInputs, _Sign, Output),
   (
      member(Object, PositiveInputs)
   ;
      member(Object, NegativeInputs)
   ;
      Object = Output
   ).


has_enabled_effective(Sign, BoolState, NewBoolState) :-
   item([kind: influence, item: Item]),
   influence(Item, _Force, PositiveInputs, _NegativeInputs, Sign, Output),
   get_assoc_list(PositiveInputs, BoolState, BoolInputs),
   maplist(=(1), BoolInputs),
   (
      Sign = '+'
   ->
      get_assoc(Output, BoolState, 0),
      put_assoc(Output, BoolState, 1, NewBoolState)
   ;
      get_assoc(Output, BoolState, 1),
      put_assoc(Output, BoolState, 0, NewBoolState)
   ).