Разработка алгоритмического и программного обеспечения ситуационного управления безопасностью магистральных газопроводов
end;
function find_legal;
var
curr_frime: frime_ptr;
curr_legal: legal_ptr;
counter: integer;
begin
curr_frime: =find_frime (f_frime);
Result: =true;
if curr_frime<>nil
then begin
curr_legal: =curr_frime^. legal_list;
_word: =curr_legal^. name;
counter: =1;
if curr_legal=nil
then Result: =false;
while (curr_legal<>nil) and (counter<n) do begin
curr_legal: =curr_legal^. next;
if curr_legal<>nil
then begin
_word: =curr_legal^. name;
inc (counter);
end
else
Result: =False;
end;
end
else
Result: =False;
end;
procedure make_legals;
var
curr_frime: frime_ptr;
counter,
st_place: integer;
new_line: line_string;
_word,
f_frime,
dummy: word_string;
done: boolean;
begin
split (m_line,f_frime,dummy);
curr_frime: =find_frime (f_frime);
if curr_frime=nil
then begin
make_node (curr_frime);
curr_frime^. name: =f_frime;
end;
st_place: =pos (equals,f_frime);
new_line: =copy (f_frime,st_place+1,length (f_frime) - st_place);
counter: =1;
done: =false;
while not done do begin
done: =find_word (new_line,counter,_word);
add_legal (_word,curr_frime);
counter: =counter+1;
end;
end;
procedure make_legals_from_form;
var
curr_frime: frime_ptr;
i: integer;
begin
curr_frime: =find_frime (f_frime);
if curr_frime=nil
then begin
make_node (curr_frime);
curr_frime^. name: =f_frime;
end;
with MainForm. M_MakeLegal_Value do
If Lines. Count>0 then
for i: =0 to Lines. Count-1 do
add_legal (Lines [i],curr_frime);
end;
procedure add_question;
var
curr_frime: frime_ptr;
begin
curr_frime: =find_frime (f_frime);
if curr_frime=nil
then begin
make_node (curr_frime);
curr_frime^. name: =f_frime;
end;
curr_frime^. question: =s_value;
end;
function p_question;
var
curr_frime: frime_ptr;
begin
curr_frime: =find_frime (f_frime);
if curr_frime<>nil
then begin
if curr_frime^. question<>''
then
Result: =curr_frime^. question
else
Result: ='Вопрос объекта пуст';
еnd
else
Result: ='Объект в базе не найден';
end;
procedure ask;
var
pick,
num_vals: integer;
_word: word_string;
begin
if not find_legal (f_frime,1,_word)
then begin
MainForm. Memo_Report. Lines. Add ('Введите значение и нажмите кнопку "Выбрать"');
MainForm. B_Answer_GetNumVals. Enabled: =True;
while MainForm. B_Answer_GetNumVals. Tag=0 do
Application. ProcessMessages;
MainForm. B_Answer_GetNumVals. Tag: =0;
f_value: =MainForm. LE_Answer_Value. Text; // readln (f_value)
end
else begin
num_vals: =1;
with MainForm. Memo_Report. Lines do begin
Add ('Допустимые значения объекта "'+f_frime+'": ');
while find_legal (f_frime,num_vals,_word) do begin
Add (IntToStr (num_vals) +'. '+_word);
inc (num_vals);
end;
end;
MainForm. SE_Answer. MaxValue: =num_vals-1;
MainForm. Memo_Report. Lines. Add ('Выберите номер ответа и нажмите кнопку "Выбрать"');
MainForm. B_Answer_GetNumVals. Enabled: =True;
while MainForm. B_Answer_GetNumVals. Tag=0 do
Application. ProcessMessages;
pick: =MainForm. SE_Answer. Value; // ord (select [1]) - 48;
MainForm. B_Answer_GetNumVals. Tag: =0;
find_legal (f_frime,pick,_word);
f_value: =_word;
end;
end;
procedure p_read;
var
c: char;
len,
counter,
st_place: integer;
supress: boolean;
in_line: line_string;
begin
readln (RulesFile, in_line);
in_line: =AnsiLowerCase (in_line);
oline: ='';
len: =length (in_line);
st_place: =pos (' (', in_line);
if st_place>0
then len: =st_place;
supress: =false;
for counter: =1 to len do begin
c: =in_line [counter] ;
if (c=equals) and (pos ('вопрос',oline) >0)
then supress: =true;
if ord (c) =9
then c: =' ';
if (c<>'') or (supress=true)
then oline: =concat (oline,c);
end;
end;
function add_prem;
var
new_prem: prem_ptr;
f_frime,f_value: word_string;
begin
split (f_line,f_frime,f_value);
add_prem: =curr_prem;
new (new_prem);
with new_prem^ do begin
frime: =f_frime;
value: =f_value;
next: =nil;
end;
if curr_prem=nil
then
add_prem: =new_prem
else begin
while (curr_prem^. next<>nil) do
curr_prem: =curr_prem^. next;
curr_prem^. next: =new_prem;
end;
end;
function add_con (curr_con: con_ptr; f_line: line_string): con_ptr;
var
new_con: con_ptr;
f_frime,
f_value: word_string;
begin
split (f_line,f_frime,f_value);
add_con: =curr_con;
new (new_con);
with new_con^ do begin
frime: =f_frime;
value: =f_value;
cert: =get_cf (f_line);
next: =nil;
end;
if curr_con=nil
then
add_con: =new_con
else begin
while (curr_con^. next<>nil) do
curr_con^. next: =new_con;
end;
end;
procedure p_rule (curr_rule: rule_ptr);
var
curr_prem: prem_ptr;
curr_con: con_ptr;
bufStr: string;
begin
bufStr: =curr_rule^. name+' ';
curr_prem: =curr_rule^. prem;
while (curr_prem<>nil) do begin
bufStr: =bufStr+curr_prem^. frime+'=';
bufStr: =bufStr+curr_prem^. value;
curr_prem: =curr_prem^. next;
if curr_prem<>nil
then
bufStr: =bufStr+' '
else
MainForm. Memo_Report. Lines. Add (BufStr);
end;
curr_con: =curr_rule^. con;
while curr_con<>nil do begin
bufStr: =curr_con^. frime+'=';
bufStr: =bufStr+curr_con^. value+', Кд='+IntToStr (curr_con. cert);
curr_con: =curr_con^. next; if curr_con<>nil
then
bufStr: =bufStr+' '
else
MainForm. Memo_Report. Lines. Add (BufStr);
end;
end;
procedure enter_rule (rule_name: word_string);
var
new_rule,
curr_rule: rule_ptr;
line: line_string;
done: boolean;
begin
new (new_rule);
if top_rule<>nil
then begin
curr_rule: =top_rule;
while curr_rule^. next<>nil do
curr_rule: =curr_rule^. next;
curr_rule^. next: =new_rule;
end
else
top_rule: =new_rule;
with new_rule^ do begin
name: =rule_name;
next: =nil;
prem: =nil;
con: =nil;
end;
p_read (line);
done: =false;
while ( (not done) and (not Eof (RulesFile))) do begin
new_rule^. prem: =add_prem (new_rule^. prem,line);
p_read (line);
done: = (pos ('ВВ',line) >0) and (length (line) =2);
end;
p_read (line);
repeat
done: =Eof (RulesFile);
new_rule^. con: =add_con (new_rule^. con,line);
done: =done or (line [length (line)] ='. ');
if not done then p_read (line);
until done;
p_rule (new_rule);
end;
procedure LoadFormFile;
var
command: word_string;
m_line,f_line: line_string;
st_place: integer;
s_frime,s_value: word_string;
begin
MainForm. Memo_Report. Lines. Add ('Чтение файла, содержащего правила');
assign (RulesFile,'rules. txt');
reset (RulesFile);
top_rule: =nil;
command: ='';
while not Eof (RulesFile) do begin
p_read (f_line);
st_place: =pos (' (',f_line);
if st_place=0
then
st_place: =pos (colon,f_line);
if st_place>1
then begin
command: =copy (f_line,1,st_place-1);
m_line: =copy (f_line,st_place+1,length (f_line) - st_place);
if command='многозначный'
then begin
split (m_line,s_frime,s_value);
make_multi (s_frime);
add_frime (s_frime,s_value);
add_cf (s_frime,s_value,get_cf (m_line));
end else
if command='вопрос'
then begin
split (m_line,s_frime,s_value);
add_question (s_frime,s_value);
end else
if command='разрешён'
then begin
make_legals (m_line);
end else
if command='правило'
then begin
split (m_line,s_frime,s_value);
enter_rule (s_frime);
end;
end;
end;
end;
procedure SaveToFile;
var
a_frime: frime_ptr;
a_legal: legal_ptr;
a_value: value_ptr;
a_rule: rule_ptr;
a_con: con_ptr;
a_prem: prem_ptr;
f: TextFile;
begin
AssignFile (f,'rules. txt');
Rewrite (f);
a_frime: =top_fact;
while a_frime<>nil do begin
a_value: =a_frime^. value_list;
while a_value<>nil do begin
writeln (f,'многозначный'+colon+a_frime^. name+equals+a_value^. name+comma+'Кд=',a_value^. cert);
a_value: =a_value. next;
end;
a_Legal: =a_frime^. Legal_list;
write (f,'разрешён'+colon,a_frime^. name,equals);
while a_Legal<>nil do begin
write (f,a_legal^. name,comma);
a_legal: =a_legal. next;
end;
writeln (f);
writeln (f,'вопрос'+colon+a_frime^. name+equals+a_frime^. question);
a_frime: =a_frime^. next;
end;
a_rule: =top_rule;
while a_rule<>nil do begin
writeln (f,'правило'+a_rule^. name);
a_prem: =a_rule^. prem;
while a_prem<>nil do begin
writeln (f,a_prem^. frime+equals+a_prem^. value);
a_prem: =a_prem^. next;
end;
writeln (f,'ВВ');
a_con: =a_rule^. con;
while a_con<>nil do begin
writeln (f,a_con^. frime+equals+a_con^. value+comma+'Кд=',a_con^. cert);
a_con: =a_con^. next;
if a_prem=nil
then writeln (f,'. ');
end;
a_rule: =a_rule^. next;
end;
CloseFile (f);
end;
function find_rule (fri: word_string; curr_rule: rule_ptr): rule_ptr;
var
found: boolean;
curr_con: con_ptr;
begin
found: =false;
find_rule: =nil;
while (curr_rule<>nil) and (not found) do begin
curr_con: =curr_rule^. con;
while curr_con<>nil do begin
if curr_con^. frime=fri
then begin
found: =true;
find_rule: =curr_rule;
end;
curr_con: =curr_con^. next;
end;
curr_rule: =curr_rule^. next;
end;
end;
procedure conclude (curr_rule: rule_ptr; prem_cert: integer);
var
curr_con: con_ptr;
cert: integer;
begin
curr_con: =curr_rule^. con;
while curr_con<>nil do begin
add_frime (curr_con^. frime,curr_con^. value);
cert: = (prem_cert*curr_con^. cert) div 100;
add_cf (curr_con^. frime,curr_con^. value,cert);
curr_con: =curr_con^. next;
end;
end;
procedure pursue;
var
f_value: word_string;
curr_frime: frime_ptr;
curr_value: value_ptr;
curr_rule: rule_ptr;
curr_prem: prem_ptr;
bad: boolean;
solved: boolean;
lowest: integer;
begin
curr_frime: =find_frime (f_frime);
if curr_frime=nil
then begin
make_node (curr_frime);
curr_frime^. name: =f_frime;
end;
solved: =false;
if not curr_frime^. sought then begin
solved: =false;
curr_frime^. sought: =true;
curr_rule: =find_rule (f_frime,top_rule);
while (curr_rule<>nil) and (ok_add (f_frime,definite)) do begin
curr_prem: =curr_rule^. prem;
bad: =false;
lowest: =definite;
while (curr_prem<>nil) and (not bad) do begin
pursue (curr_prem^. frime);
curr_value: =test (curr_prem^. frime,curr_prem^. value);
if curr_value=nil
then
bad: =true
else
if curr_value^. cert<lowest
then
lowest: =curr_value^. cert;
curr_prem: =curr_prem^. next;
end;
if not bad
then begin
if explain
then
conclude (curr_rule,lowest);
solved: =true;
end;
curr_rule: =find_rule (f_frime,curr_rule^. next);
end;
if not solved
then begin
if explain
then
ask (f_frime,f_value);
add_frime (f_frime,f_value);
add_cf (f_frime,f_value,definite);
end;
end;
end;
procedure q_result (f_frime: word_string);
var
curr_frime: frime_ptr;
begin
MainForm. Memo_Report. Lines. Add ('Результат консультации: ');
curr_frime: =find_frime (f_frime);
see_vals (curr_frime,true);
MainForm. Memo_Report. Lines. Add ('Конец консультации');
end;
procedure explain_how (curr_rule: rule_ptr);
var
curr_prem: prem_ptr;
curr_con: con_ptr;
begin
MainForm. Memo_Report. Lines. Add ('');
MainForm. Memo_Report. Lines. Add ('Tак как: ');
curr_prem: =curr_rule^. prem;
while curr_prem<>nil do begin
MainForm. Memo_Report. Lines. Add (curr_prem^. frime+'='+curr_prem^. value);
curr_prem: =curr_prem^. next;
if curr_prem<>nil
then
MainForm. Memo_Report. Lines. Add (' ')
else
MainForm. Memo_Report. Lines. Add ('');
end;
MainForm. Memo_Report. Lines. Add ('Можно сделать вывод, что ');
curr_con: =curr_rule^. con;
while curr_con<>nil do begin
MainForm. Memo_Report. Lines. Add (curr_con^. frime+'='+curr_con^. value+', Кд='+IntToStr (curr_con^. cert));
curr_con: =curr_con^. next;
if curr_con<>nil
then
MainForm. Memo_Report. Lines. Add (' ')
else
MainForm. Memo_Report. Lines. Add ('');
end;
end;
procedure explain_why (f_frime: word_string);
begin
MainForm. Memo_Report. Lines. Add ('')
end;
procedure TMainForm. FormCreate (Sender: TObject);
begin
last_try: =nil;
top_fact: =nil;
LoadFormFile;
explain: =true;
end;
procedure TMainForm. B_AddFactClick (Sender: TObject);
var
s_frime,
s_value: word_string;
s_cf: integer;
begin
s_cf: =StrToInt (LE_AddFact_Cf. Text);
s_frime: =LE_AddFact_Frime. Text;
s_value: =LE_AddFact_Value. Text;
if ok_add (s_frime,s_cf)
then begin
add_frime (s_frime,s_value);
add_cf (s_frime,s_value,s_cf);
MainForm. Memo_Report. Lines. Add ('Факт добавлен');
end
else
MainForm. Memo_Report. Lines. Add ('Добавление не разрешено (Объект '+s_frime+' нe объявлен многозначным) ! ');
end;
procedure TMainForm. B_TestFactClick (Sender: TObject);
var
s_frime,
s_value: word_string;
begin
s_frime: =LE_TestFact_Frime. Text;
s_value: =LE_TestFact_Value. Text;
if test (s_frime,s_value) =nil
then
MainForm. Memo_Report. Lines. Add ('Факт неверен')
else
MainForm. Memo_Report. Lines. Add ('Факт верен');
end;
procedure TMainForm. B_SeeFactsClick (Sender: TObject);
begin
see_frimes (true);
end;
procedure TMainForm. B_MakeFrimeMultivalidClick (Sender: TObject);
begin
make_multi (LE_MakeMulti_Frime. Text);
end;
procedure TMainForm. B_MakeLegalClick (Sender: TObject);
begin
make_legals_from_form (LE_MakeLegal_Frime. Text);
end;
procedure TMainForm. B_AddQuestionClick (Sender: TObject);
var
s_frime,
s_value: word_string;
begin
s_frime: =LE_AddQuestion_Frime. Text;
s_value: =LE_AddQuestion_Value. Text;
add_question (s_frime,s_value);
end;
procedure TMainForm. B_GetQuestionClick (Sender: TObject);
var
s_frime: word_string;
begin
s_frime: =LE_Answer_Frime. Text;
LE_GetQuestion. Text: =p_question (s_frime);
end;
procedure TMainForm. B_Answer_GetNumValsClick (Sender: TObject);
begin
B_Answer_GetNumVals. Tag: =1;
end;
procedure TMainForm. B_AnswerClick (Sender: TObject);
var
s_frime,
s_value: word_string;
begin
s_frime: =LE_Answer_Frime. Text;
ask (s_frime,s_value);
add_frime (s_frime,s_value);
add_cf (s_frime,s_value,definite);
end;
procedure TMainForm. LE_OnExit (Sender: TObject);
begin
TLabeledEdit (Sender). Text: =AnsiLowerCase (trim (TLabeledEdit (Sender). Text));
end;
procedure TMainForm. M_MakeLegal_ValueExit (Sender: TObject);
var i: integer;
begin
with M_MakeLegal_Value do
If Lines. Count>0 then
for i: =Lines. Count-1 downto 0 do begin
Lines [i]: =AnsiLowerCase (trim (Lines [i]));
If Lines [i] ='' then Lines. Delete (i);
end;
end;
procedure TMainForm. B_GetTargetClick (Sender: TObject);
var
s_frime: word_string;
begin
s_frime: =LE_GetTarget. Text;
if s_frime<>''
then begin
pursue (s_frime);
q_result (s_frime);
end
else
MainForm. Memo_Report. Lines. Add ('Ошибка! Объект не указан! ');
end;
procedure TMainForm. Button1Click (Sender: TObject);
begin
SaveToFile;
end;
procedure TMainForm. Button2Click (Sender: TObject);
begin
qmport;
end;
end.