parseBoolean

PURPOSE ^

parseBoolean Parses a Boolean logic statement

SYNOPSIS ^

function [elements,newRule] = parseBoolean(str,tokens,allowedElementChars)

DESCRIPTION ^

parseBoolean Parses a Boolean logic statement

 [elements,newRule] = parseBoolean(str,tokens,allowedElementChars)

 str                   Input string
 tokens                Allowed operators in boolean statements (optional, default '()&|~')
 allowedElementChars   Allowed characters in elements of teh statement

 elements              Non-operator elements
 newRule               New rule translated to element numbers

 Markus Herrgard 5/11/04

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [elements,newRule] = parseBoolean(str,tokens,allowedElementChars)
0002 %parseBoolean Parses a Boolean logic statement
0003 %
0004 % [elements,newRule] = parseBoolean(str,tokens,allowedElementChars)
0005 %
0006 % str                   Input string
0007 % tokens                Allowed operators in boolean statements (optional, default '()&|~')
0008 % allowedElementChars   Allowed characters in elements of teh statement
0009 %
0010 % elements              Non-operator elements
0011 % newRule               New rule translated to element numbers
0012 %
0013 % Markus Herrgard 5/11/04
0014 
0015 if (nargin < 2)
0016     % Allowed separators for tokenizer
0017     tokens = '()&|~';
0018 end
0019 if (nargin < 3)
0020     % Allowed characters in elements
0021     allowedElementChars = '[A-Za-z0-9_\.\-]'; 
0022 end
0023 % changes (and, not, or) for (&, ~, |) within the string
0024 str1=str;
0025 %str1 = regexprep(str1, '+',' & ');
0026 %str1 = regexprep(str1, ',',' | ');
0027 str1 = regexprep(str1, 'and ', '& ','ignorecase');
0028 str1 = regexprep(str1, 'or ', '| ','ignorecase');
0029 str1 = regexprep(str1, 'not ', '~ ','ignorecase');
0030 str1 = regexprep(str1,'\[','');
0031 str1 = regexprep(str1,'\]','');
0032 newRule = str1;
0033 
0034 elements = {};
0035 endStr = str1;
0036 cnt = 0;
0037 tmpRule = [];
0038 while (~isempty(endStr))
0039     % Tokenize the input string (one token at a time)
0040     [tok,endStr] = strtok(endStr,tokens);
0041     % Empty token?
0042     if (~isempty(tok))
0043         % Non-whitespace characters?
0044         if (regexp(tok,'\S') > 0)
0045             % Remove white space
0046             tok = tok(~isspace(tok));
0047             % Have we already got this token?
0048             if (sum(strcmp(elements,tok)) == 0)
0049                 % If not add this
0050                 cnt = cnt + 1;
0051                 elements{end+1} = tok;
0052             end
0053             % The replacement token
0054             newTok = ['x(' num2str(cnt) ')'];
0055             % Remove troublesome characters ([])
0056             tok = regexprep(tok,'\[','');
0057             tok = regexprep(tok,'\]','');
0058             % Find all the instances of the original token
0059             [s,f] = regexp(newRule,tok);
0060             % Get the length of the rule (for replacing below)
0061             ruleLength = length(newRule);
0062             % Loop through all the instances
0063             replaceThisVector = false(length(s),1);
0064             % Find out which instances to replace (do not want to replace
0065             % instances that are parts of other tokens)
0066             for i = 1:length(s)
0067                 % It's the only token so go ahead and replace
0068                 if ((s(i) == 1) & (f(i) == ruleLength))
0069                    replaceThisFlag = true;
0070                 elseif (s(i) == 1) % Token at the beginning of string
0071                     if (isempty(regexp(newRule(f(i)+1),allowedElementChars)))
0072                         % It's not a part of another token - replace
0073                         replaceThisFlag = true;    
0074                     else
0075                         % Part of another token - do not replace
0076                         replaceThisFlag = false;
0077                     end
0078                 elseif (f(i) == ruleLength) % Token at the end of string
0079                     if (isempty(regexp(newRule(s(i)-1),allowedElementChars)))
0080                         % It's not a part of another token - replace
0081                         replaceThisFlag = true;    
0082                     else
0083                         % Part of another token - do not replace
0084                         replaceThisFlag = false;
0085                     end
0086                 else % Token in the middle of the string
0087                     if (isempty(regexp(newRule(f(i)+1),allowedElementChars)) & isempty(regexp(newRule(s(i)-1),allowedElementChars)))
0088                         % It's not a part of another token - replace
0089                         replaceThisFlag = true;
0090                     else
0091                         % Part of another token - do not replace
0092                         replaceThisFlag = false;
0093                     end
0094                 end
0095                 replaceThisVector(i) = replaceThisFlag;
0096             end
0097             % Only replace the correct tokens
0098             s = s(replaceThisVector);
0099             f = f(replaceThisVector);
0100             nRep = length(s);
0101             for i = 1:nRep
0102                 % Add the beginning of the string for the first token
0103                 if (i == 1)
0104                     if (s(i) > 1)
0105                         tmpRule = newRule(1:(s(i)-1));
0106                     else
0107                         tmpRule = [];
0108                     end
0109                 end
0110                 % Add the new token
0111                 tmpRule = [tmpRule newTok];
0112                 % Add the remainder of the string until the next token (if
0113                 % there is one)
0114                 if (i < nRep)
0115                     tmpRule = [tmpRule newRule((f(i)+1):(s(i+1)-1))];    
0116                 end
0117                 % Add the end of the string for the last token
0118                 if (i == nRep)
0119                     if (f(i) < ruleLength)
0120                         tmpRule = [tmpRule newRule(f(i)+1:end)];       
0121                     end
0122                 end
0123             end
0124             newRule = tmpRule;
0125         end
0126     end
0127 end
0128

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003