?? parsrule.m
字號:
if symbFlag
% If the rules are in symbolic format, a little pre-processing
% will save the day
txtRule=strrep(txtRule,'=>',[' ' thenStr ' ']);
txtRule=strrep(txtRule,'&',[' ' andStr ' ']);
txtRule=strrep(txtRule,'|',[' ' orStr ' ']);
txtRule=strrep(txtRule,'~=',[' ' notStr ' ']);
txtRule=strrep(txtRule,'=',' ');
else
txtRule=[' ' txtRule];
txtRule=strrep(txtRule,ifStr,' ');
txtRule=strrep(txtRule,isStr,' ');
end
% Compress all multiple spaces down into one space
% This is a crucial maneuver that allows me to separate words
% quickly and painlessly.
spaceIndex=find(txtRule==' ');
dblSpaceIndex=find(diff(spaceIndex)==1);
txtRule(spaceIndex(dblSpaceIndex))=[];
% Form the antecedent and consequent strings
antStart=1;
antEnd=findstr(txtRule,[' ' thenStr ' ']);
if isempty(antEnd),
errorStr=['Rule is not an if-then rule.'];
if nargout<3,
error(errorStr);
else
errorFlag=1;
end
end
antStr=deblank(txtRule((antStart):(antEnd-1)));
consStr=deblank(txtRule((antEnd+6):size(txtRule,2)));
% Decode the antecedent
spaceIndex=[findstr(antStr,' ') length(antStr)];
if spaceIndex(1)~=1, spaceIndex=[1 spaceIndex]; end
% Ignore the first word if it's a line number
firstWord=antStr(spaceIndex(1):spaceIndex(2));
if all(abs(firstWord)<58),
% If all characters are less than ASCII 58 ('9') then it's a number,
% so start reading from the second word.
antPtr=2;
else
antPtr=1;
end
% You need at least two words in your antecedent in order to play
if (length(spaceIndex)-antPtr)<2,
errorStr=['Rule antecedent is incomplete'];
if nargout<3,
error(errorStr);
else
errorFlag=1;
end
end
while antPtr<length(spaceIndex) & ~errorFlag,
nextWord=antStr(spaceIndex(antPtr):spaceIndex(antPtr+1));
nextWord(find(nextWord==32))=[];
if strcmp(nextWord,andStr),
andOrCode(ruleIndex)=1;
elseif strcmp(nextWord,orStr),
andOrCode(ruleIndex)=2;
else
varName=nextWord;
varIndex=findrow(varName,inLabels);
if isempty(varIndex),
errorStr=['There is no input variable called ' varName];
if nargout<3,
error(errorStr);
else
errorFlag=1;
end
end
antPtr=antPtr+1;
nextWord=antStr(spaceIndex(antPtr):spaceIndex(antPtr+1));
nextWord(find(nextWord==32))=[];
% Handle potential usage of the word NOT
if strcmp(nextWord,notStr),
MFIndexMultiplier=-1;
antPtr=antPtr+1;
nextWord=antStr(spaceIndex(antPtr):spaceIndex(antPtr+1));
else
MFIndexMultiplier=1;
end
MFIndexBegin=sum(numInputMFs(1:(varIndex-1)))+1;
MFIndexEnd=MFIndexBegin+numInputMFs(varIndex)-1;
MFList=inMFLabels(MFIndexBegin:MFIndexEnd,:);
mfIndex=findrow(nextWord,MFList)*MFIndexMultiplier;
if isempty(mfIndex) & ~errorFlag,
errorStr=['There is no MF called ' nextWord ...
' for the input variable ' varName];
if nargout<3,
error(errorStr);
else
errorFlag=1;
end
end
if ~errorFlag,
antCode(ruleIndex,varIndex)=mfIndex;
end
end
antPtr=antPtr+1;
end
% Decode the consequent
spaceIndex=[1 findstr(consStr,' ') length(consStr)];
consPtr=1;
% You need at least two words in your consequent in order to play
if (length(spaceIndex)-consPtr)<2,
errorStr=['Rule consequent is incomplete'];
if nargout<3,
error(errorStr);
else
errorFlag=1;
end
end
while consPtr<length(spaceIndex) & ~errorFlag,
nextWord=consStr(spaceIndex(consPtr):spaceIndex(consPtr+1));
nextWord(find(nextWord==32))=[];
if all((nextWord>=32) & (nextWord<=57)) & ~isempty(nextWord),
wtCode(ruleIndex)=eval(nextWord);
elseif ~isempty(nextWord),
varName=nextWord;
varIndex=findrow(varName,outLabels);
if isempty(varIndex),
errorStr=['There is no output variable called ' varName];
if nargout<3,
error(errorStr);
else
errorFlag=1;
end
end
consPtr=consPtr+1;
nextWord=consStr(spaceIndex(consPtr):spaceIndex(consPtr+1));
nextWord(find(nextWord==32))=[];
% Handle potential usage of the word NOT
if strcmp(nextWord,notStr),
MFIndexMultiplier=-1;
consPtr=consPtr+1;
nextWord=consStr(spaceIndex(consPtr):spaceIndex(consPtr+1));
else
MFIndexMultiplier=1;
end
MFIndexBegin=sum(numOutputMFs(1:(varIndex-1)))+1;
MFIndexEnd=MFIndexBegin+numOutputMFs(varIndex)-1;
MFList=outMFLabels(MFIndexBegin:MFIndexEnd,:);
mfIndex=findrow(nextWord,MFList)*MFIndexMultiplier;
if isempty(mfIndex) & ~errorFlag,
errorStr=['There is no MF called ' nextWord ...
' for the output variable ' varName];
if nargout<3,
error(errorStr);
else
errorFlag=1;
end
end
if ~errorFlag,
consCode(ruleIndex,varIndex)=mfIndex;
end
end
consPtr=consPtr+1;
end
if errorFlag,
errRuleIndex=[errRuleIndex ruleIndex];
else
goodRuleIndex=[goodRuleIndex ruleIndex];
end
errorFlag=0;
end
ruleList=[antCode consCode wtCode andOrCode];
end % if strcmp(ruleFormat, ...
% At this point, we're nearly done. Compile the parsed rules along with
% the rules that have been flagged with errors. Make sure and
% don't include any error-ridden rules in the parsed output
% Get back the cleaned rules that were properly parsed
ruleList(errRuleIndex,:)=[];
outFis=setfis(fis,'ruleList',ruleList);
numRules=length(goodRuleIndex);
goodRuleList=showrule(outFis,1:numRules,ruleFormat,lang);
goodWid=size(goodRuleList,2);
numErrs=length(errRuleIndex);
errWid=0;
if numErrs>0,
inTxtRuleList(goodRuleIndex,:)=[];
% Replace any already existing # signs with spaces so they don't keep piling up
inTxtRuleList(find(inTxtRuleList=='#'))= ...
32*ones(size(find(inTxtRuleList=='#')));
% Add new # signs to indicate the fact that there's been an error
inTxtRuleList=[abs('#')*ones(numErrs,1) inTxtRuleList];
% Compress all multiple spaces down into one space
errTxtRuleList=[];
for count=1:numErrs,
errTxtRule=inTxtRuleList(count,:);
spaceIndex=find(errTxtRule==' ');
dblSpaceIndex=find(diff(spaceIndex)==1);
errTxtRule(spaceIndex(dblSpaceIndex))=[];
errTxtRuleList=fstrvcat(errTxtRuleList,errTxtRule);
end;
errTxtRuleList=setstr(errTxtRuleList);
errWid=size(errTxtRuleList,2);
end
% Combine the good and bad lines for display
outTxtRuleList=32*ones(numRules,max(goodWid,errWid));
if ~isempty(goodRuleIndex),
outTxtRuleList(goodRuleIndex,1:goodWid)=goodRuleList;
end
if ~isempty(errRuleIndex),
outTxtRuleList(errRuleIndex,1:errWid)=errTxtRuleList;
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -