2016-11-20 4 views
1

여기 내 프롤로그 데이터베이스 코드입니다.질의 결과를 프롤로그에 파일로 작성

:- 
    dynamic myTable/2. 

init :- 
    removeAll, 
    asserta(myTable('avalue', 'another value')), 
    asserta(myTable('avalue1', 'another value 1')), 
    asserta(myTable('avalue2', 'another value 2')), 
    asserta(myTable('avalue3', 'another value 3')), 
    asserta(myTable('avalue4', 'another value 4')). 

read(Col1, Col2) :- 
    myTable(Col1, Col2). 

saveQueries(FileName) :- 
    tell(FileName). 

stopSavingQueries :- 
    told. 

파일로 프롤로그 출력 저장을 시작하려고합니다. 파일에 저장해야하는 동적 데이터베이스에 대한 쿼리를 만든 다음 쿼리 저장을 중지합니다.

?- init. 
true. 

?- saveQueries('queries.txt'). 
true. 

?- read(Col1, Col2). 
... 
?- stopSavingQueries. 
true. 

이 코드 파일을 실행하면 queries.txt이 생성됩니다. read(Col1, Col2).을 실행하면 콘솔에 출력이 표시되고 queries.txt 파일은 비어 있습니다.

답변

1

잠시 인터넷 검색을 한 후이 솔루션을 발견했습니다. 나는 모든 쿼리 및 결과를 포함하는 파일 queries.txt이 그 명령을 실행 한 후

saveQueries(FileName) :- 
    protocol(FileName). 

stopQueriesSaving :- 
    noprotocol. 

그럼이

?- saveQueries('queries.txt'). 
true. 

/* execute some queries here */ 

?- stopQueriesSaving. 
true. 

을 할 수 있습니다.