C# and Prolog integration

977 Views Asked by At

I want to execute a prolog file when the user click the 'Enter' button in a windows 8 application. Can I execute a separate prolog file or do I need to write the prolog code in the middle of my C# codes. I'm using visual studio 2013 and SWI prolog.

I want to do this because I'm developing an application which takes an arithmetic equation as the user input, use prolog to solve it and output the answer.

1

There are 1 best solutions below

0
On

I recommend using the regular Windows desktop environment rather than Windows Store (which I see in the question tags). Windows store apps have a lot of restrictions to prevent people from creating malicious apps, and that will prevent you from launching the Prolog app.

But with a desktop app, you could start the Prolog interpreter in its own process, with the standard-input and standard-output streams redirected. You could then send content to the Prolog process via the input stream, and relay the output stream of the Prolog process to a text box in your C# WinForms app.

This sample at MSDN shows how to redirect the standard-output stream of a process:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx

And with some minor changes to suit your needs:

        Process myProcess = new Process();
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("prolog.exe", "arguments to the prolog process");
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcess.StartInfo = myProcessStartInfo;
        myProcess.Start();
        StreamReader myStreamReader = myProcess.StandardOutput;
        // Read the standard output of the spawned process. 
        string myString = myStreamReader.ReadLine();
        Console.WriteLine(myString);

You could also create a DLL version of the Prolog code, which you could load into your C# app, however this will take more work and I doubt that there is enough benefit to justify that extra work.