-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordsToLogicTable.m
More file actions
79 lines (59 loc) · 2.58 KB
/
Copy pathwordsToLogicTable.m
File metadata and controls
79 lines (59 loc) · 2.58 KB
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
% wordsToLogicTable() turns an expression such as 'p53 AND not DEAD' into a
% logic table, given variable names (in this case 'p53' and 'DEAD')
function [ usedVars, logicTable ] = wordsToLogicTable(varNames, transitionRule, globalCode)
% compute the new name for each variable so that they refer to their
% respective columns in the inputTable (defined below);
% also change 'and' to '&', etc.
subs = { '', '&', '|', '~', '', '&', '|', '~' };
numSubs = length(subs);
varNames(end+1:end+numSubs) = { 'either','and', 'or', 'not', 'EITHER', 'AND', 'OR', 'NOT' };
varLengths = zeros(1, length(varNames));
for loopVar = 1:length(varNames)
varLengths(loopVar) = length(varNames{loopVar});
end
[ ~, varsInOrder ] = sort(varLengths, 'descend');
usedVars = [];
numUsedVars = 0;
transitionRule = [ ' ' transitionRule ' ' ]; % so we can check the letter after every word
for loopVar = varsInOrder
textEndpoints = [ strfind(transitionRule, varNames{loopVar}) - 1, length(transitionRule) ];
textBeginnings = [ 1, textEndpoints(1:end-1) + length(varNames{loopVar}) + 1 ];
firstLetters = transitionRule(textBeginnings(2:end));
idx = ((firstLetters == ' ') | (firstLetters == ')'));
textEndpoints = textEndpoints([ idx true ]);
textBeginnings = textBeginnings([ true idx ]);
if loopVar == 119
end
if length(textEndpoints) > 1
if loopVar <= length(varNames)-numSubs
numUsedVars = numUsedVars + 1;
usedVars(1, numUsedVars) = loopVar;
newVarName = [ 'inputTable(:, ' num2str(numUsedVars) ')' ];
else
newVarName = subs{loopVar-length(varNames)+numSubs};
end
recodedRule = transitionRule(textBeginnings(1):textEndpoints(1));
for loopInsertion = 2:length(textEndpoints)
recodedRule = [ recodedRule newVarName ...
transitionRule(textBeginnings(loopInsertion):textEndpoints(loopInsertion)) ];
end
transitionRule = recodedRule;
end
end
% construct a table containing all possible inputs
inputTable = zeros(2^numUsedVars, numUsedVars);
allNums = (0:(2^numUsedVars-1))';
for loopVar = 1:numUsedVars
inputTable(:, loopVar) = mod(bitshift(allNums, loopVar-numUsedVars), 2);
end
if exist('globalCode', 'var')
eval(globalCode);
%transitionRule = [ globalCode transitionRule ];
end
% evaluate the rule as a MATLAB expression
try
logicTable = double(eval(transitionRule));
catch
error('wordsToLogicTable(): problem with inputs')
end
end