Page 1 of 1

writef not printing to Message Window

Posted: 24 Mar 2021 2:33
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?

Re: writef not printing to Message Window

Posted: 24 Mar 2021 12:03
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.

Re: writef not printing to Message Window

Posted: 24 Mar 2021 12:59
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.

Re: writef not printing to Message Window

Posted: 24 Mar 2021 13:35
by Thomas Linder Puls
Please read about [code] ... [/code] formatting in the forum guides.

Re: writef not printing to Message Window

Posted: 27 Mar 2021 20:40
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.

Re: writef not printing to Message Window

Posted: 27 Mar 2021 21:07
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.

Re: writef not printing to Message Window

Posted: 28 Mar 2021 20:14
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.