Discussions related to Visual Prolog
dcgreenwood
Posts: 14
Joined: 16 Jan 2021 1:33

writef not printing to Message Window

Unread post by dcgreenwood »

I'm running a gui program, with menu items calling clauses in Main. Two menu items trigger stdio::write that writes as normal to the Message Window. But another is supposed to, but its messages never appear. I've debugged it, and each write is being executed. It seems like maybe they are being directed somewhere other than the message window?. I cannot find anything different in the code tied to the menu items that would explain why. I am a beginner. What could I have done to make write not work?
Harrison Pratt
VIP Member
Posts: 336
Joined: 5 Nov 2000 0:01

Re: writef not printing to Message Window

Unread post by Harrison Pratt »

Can you post some of the relevant code?
Remember that you should format code in this forum using the [</>] button in the forums editor.

It is a better practice to put the bulk of your code in a class other than main.pro or taskwindow.pro and invoke those class predicates from taskwindow.pro , but I don't think this is related to your immediate problem.
dcgreenwood
Posts: 14
Joined: 16 Jan 2021 1:33

Re: writef not printing to Message Window

Unread post by dcgreenwood »

I figured out the problem, though I don't understand why it is a problem.
Here is the problematic part

Code: Select all

class predicates     checkFactionStatus : (faction Faction) nondeterm.     startExperiment : () multi.     checkIfExperimentOver : () multi.   clauses % Experiment over is triggered by CheckIfExperimentOver failing. % This just runs the experiment once. Will modify second clause to get repeats.     runExperiment() :-         stdio::write("Start Experiment.\n"),         startExperiment(),         fail.     runExperiment().       startExperiment() :-         assert(aiScore(1, currentplan, 0)),         assert(objectiveStatus(b2, no, "anonymous")),         stdio::write("\n\nStarting Experiment.\n"),         checkIfExperimentOver(),         executeTurn().     startExperiment() :-         stdio::write("\nExperiment Concluded.\n\n"),         fail.
When building, I had not declared a mode for startExperiment and checkIfExperimentOver. The builder gave an error message that these two predicates were declared as procedures but should be changed to multi, so I declared them as multi.
Upon execute, no printing happened after startExperiment was called.
I just now changed both from multi to nondeterm.
Now it all works. I don't understand what "multi" mode is, but it seemed to cause the problem.
So it now works with the declarations as

Code: Select all

class predicates     checkFactionStatus : (faction Faction) nondeterm.     startExperiment : () nondeterm     checkIfExperimentOver : () nondeterm.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1204
Joined: 28 Feb 2000 0:01

Re: writef not printing to Message Window

Unread post by Thomas Linder Puls »

Please read about [code] ... [/code] formatting in the forum guides.
Regards Thomas Linder Puls
PDC
dcgreenwood
Posts: 14
Joined: 16 Jan 2021 1:33

Re: writef not printing to Message Window

Unread post by dcgreenwood »

I now understand what was going on - I was completely wrong about the reason. What I did not realize was that the output to the Message Window is buffered and then written to the window only after the set of clauses terminates and returns to the window monitoring. I knew the clauses were executing (in fact, I knew they were in an infinite loop) but didn't understand why I didn't see output while that loop was executing. Once I solved the infinite looping, I got the output that I expected.
Harrison Pratt
VIP Member
Posts: 336
Joined: 5 Nov 2000 0:01

Re: writef not printing to Message Window

Unread post by Harrison Pratt »

You can use vpi::processEvents() to force immediate output to the Messages window.

Code: Select all

... % do some processing someProcessingPredicate(), stdio::write("\nSomething happens here!"), _=vpi::processEvents(), % more processing ...
This may slow down processing so use it judiciously.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1204
Joined: 28 Feb 2000 0:01

Re: writef not printing to Message Window

Unread post by Thomas Linder Puls »

While vpi::processEvents will help to update the windows, it will also allow users to press on menu-entries, buttons, etc. So it may mix other processing in the "current" one, hence it has many aspects in common with multi-threaded programming or (more precisely) coroutines and asynchronous programming.

All in all, I will recommend that you avoid using vpi::processEvents in real code, and try to find some other solution to you long running routine.
Regards Thomas Linder Puls
PDC
Post Reply