File size: 6,975 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*

------------------------------------------------------------------------------------------

Author:

Boroş Tiberiu                                                                                                  

Administrator Sistem                                                    Tel.: +40745310081

Institutul de Cercetări pentru Inteligenţă Artificială,

Academia Română

Calea 13 Septembrie, Nr. 13, CASA ACADEMIEI, Bucuresti 050711, ROMANIA

Tel.: +40-(0)213188103 

Fax: +40-(0)213188142 

E-mail: office@racai.ro

Web: http://www.racai.ro

------------------------------------------------------------------------------------------

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Globalization;

public class Octave
{
    public Process OctaveProcess { get; set; }
    private string OctaveEchoString { get; set; }
    public Octave(string PathToOctaveBinaries)
    {
        StartOctave(PathToOctaveBinaries, false);
    }

    public Octave(string PathToOctaveBinaries, bool CreateWindow)
    {
        StartOctave(PathToOctaveBinaries, CreateWindow);
    }

    string ptob;
    bool cw;
    private void StartOctave(string PathToOctaveBinaries, bool CreateWindow)
    {
        ptob = PathToOctaveBinaries;
        cw = CreateWindow;
        this.OctaveEchoString = Guid.NewGuid().ToString();
        OctaveProcess = new Process();
        ProcessStartInfo pi = new ProcessStartInfo();
        if (System.Environment.OSVersion.Platform == PlatformID.Unix)
        {
            pi.FileName = "octave-cli";
        }
        else
        {
            if (PathToOctaveBinaries[PathToOctaveBinaries.Length - 1] != '\\')
                PathToOctaveBinaries = PathToOctaveBinaries + Path.DirectorySeparatorChar;
            pi.FileName = PathToOctaveBinaries + "octave-cli.exe";
        }
        pi.RedirectStandardInput = true;
        pi.RedirectStandardOutput = true;
        pi.RedirectStandardError = true;
        pi.UseShellExecute = false;
        pi.CreateNoWindow = !CreateWindow;
        pi.Verb = "open";
        //
        pi.WorkingDirectory = ".";
        OctaveProcess.StartInfo = pi;
        OctaveProcess.Start();
        OctaveProcess.OutputDataReceived += new DataReceivedEventHandler(OctaveProcess_OutputDataReceived);
        OctaveProcess.BeginOutputReadLine();
        OctaveEntryText = ExecuteCommand(null);
    }

    public double GetScalar(string scalar)
    {
        string rasp = ExecuteCommand(scalar, 30000);
        string val = rasp.Replace(scalar + " = ", "").Substring(rasp.LastIndexOf("\\") + 1).Trim();
        return double.Parse(val, CultureInfo.InvariantCulture);
    }

    public double[] GetVector(string vector)
    {
        string rasp = ExecuteCommand(vector, 30000);
        string[] lines = rasp.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        int i = 0;
        //catam urmatorul entry
        List<double> data = new List<double>();
        while (i < lines.Length - 1)
        {
            i++;
            string line = lines[i];
            string[] dataS = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            for (int k = 0; k < dataS.Length; k++)
            {
                data.Add(double.Parse(dataS[k], CultureInfo.InvariantCulture));
            }
        }
        //caz special in care a pus toate rezultatele pe o singura linie
        if (data.Count == 0)
        {
            string[] dataS = lines[lines.Length - 1].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            if (dataS.Length != 0)
                for (int k = 0; k < dataS.Length; k++)
                {
                    data.Add(double.Parse(dataS[k], CultureInfo.InvariantCulture));
                }
        }
        return data.ToArray();
    }

    public double[][] GetMatrix(string matrix)
    {
        //string rasp = ExecuteCommand(matrix);
        //aflam numarul de randuri
        string rasp = ExecuteCommand(matrix + "(:,1)", 30000);
        string[] lines = rasp.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        double[][] mat = new double[lines.Length - 1][];
        for (int i = 0; i < mat.Length; i++)
        {
            mat[i] = GetVector(matrix + "(" + (i + 1) + ",:)");
        }
        return mat;
    }

    StringBuilder SharedBuilder = new StringBuilder();
    ManualResetEvent OctaveDoneEvent = new ManualResetEvent(false);
    public string OctaveEntryText { get; internal set; }

    public void WorkThread(object o)
    {
        string command = (string)o;
        SharedBuilder.Clear();
        OctaveDoneEvent.Reset();
        if (command != null)
        {
            OctaveProcess.StandardInput.WriteLine(command);
        }
        //ca sa avem referinta pentru output
        OctaveProcess.StandardInput.WriteLine("\"" + OctaveEchoString + "\"");
        OctaveDoneEvent.WaitOne();
    }
    public string ExecuteCommand(string command, int timeout)
    {
        command = "pwd();" + command;
        if (OctaveProcess.HasExited)
        {
            StartOctave(ptob, cw);
            if (OctaveRestarted != null) OctaveRestarted(this, EventArgs.Empty);
        }
        exitError = false;

        Thread tmp = new Thread(new ParameterizedThreadStart(WorkThread));
        tmp.Start(command);

        if (!tmp.Join(timeout))
        {
            tmp.Abort();
            throw new Exception("Octave timeout");
        }
        if (exitError)
        {
            throw new Exception(errorMessage);
        }
        return SharedBuilder.ToString();
    }
    public string ExecuteCommand(string command)
    {
        command = "pwd();" + command;
        Thread tmp = new Thread(new ParameterizedThreadStart(WorkThread));
        tmp.Start(command);

        tmp.Join();

        return SharedBuilder.ToString();
    }
    bool exitError = false;
    string errorMessage = null;
    void OctaveProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data == null)
        {
            SharedBuilder.Clear();
            errorMessage = OctaveProcess.StandardError.ReadToEnd();
            SharedBuilder.Append("Octave has exited with the following error message: \r\n" + errorMessage);
            exitError = true;
            OctaveDoneEvent.Set();
            return;
        }
        if (e.Data.Trim() == "ans = " + OctaveEchoString)
            OctaveDoneEvent.Set();
        else
            SharedBuilder.Append(e.Data + "\r\n");
    }
    public event OctaveRestartedEventHandler OctaveRestarted;
    public delegate void OctaveRestartedEventHandler(object sender, EventArgs e);
}