00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 package gui.jmatlink;
00025
00026 import java.io.UnsupportedEncodingException;
00027 import java.util.Vector;
00028
00029 import commandline.debuggingCode.Debug;
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 public class CoreJMatLink extends Thread {
00050
00051
00052
00053
00054 private final static int idleI = 0;
00055 private final static int engOpenI = 1;
00056 private final static int engCloseI = 2;
00057 private final static int engEvalStringI = 3;
00058 private final static int engGetScalarI = 4;
00059
00060 private final static int engGetArrayI = 6;
00061 private final static int engPutArray2dI = 9;
00062 private final static int engOutputBufferI = 10;
00063 private final static int engGetCharArrayI = 11;
00064 private final static int destroyJMatLinkI = 12;
00065 private final static int engOpenSingleUseI = 13;
00066 private final static int engSetVisibleI = 14;
00067 private final static int engGetVisibleI = 15;
00068
00069
00070
00071 private int status = idleI;
00072 private String arrayS;
00073 private String engEvalStringS;
00074 private String engOutputBufferS;
00075 private double engGetScalarD;
00076 private double[][] engGetArrayD;
00077
00078 private double[][] engPutArray2dD;
00079 private long epL;
00080 private int retValI;
00081 private String startCmdS;
00082 private int buflenI;
00083 private boolean debugB = false;
00084 private boolean engVisB = false;
00085
00086
00087 private boolean lockEngineB = false;
00088 private boolean lockThreadB = false;
00089 private boolean lockWaitForValueB = false;
00090 private boolean destroyJMatLinkB = false;
00091
00092
00093 private Vector<Long> enginePointerVector = new Vector<Long>();
00094
00095
00096 private long engOpenPointerL = 0;
00097
00098 private Thread runner;
00099
00100 private native long engOpenNATIVE (String startCmdS );
00101 private native long engOpenSingleUseNATIVE (String startCmdS );
00102
00103 private native int engCloseNATIVE (long epL);
00104
00105 private native int engSetVisibleNATIVE (long epL, boolean visB);
00106 private native int engGetVisibleNATIVE (long epL);
00107
00108 private native int engEvalStringNATIVE (long epL, String evalS );
00109
00110 private native double engGetScalarNATIVE (long epL, String nameS );
00111 private native double[][] engGetVariableNATIVE (long epL, String nameS );
00112 private native String[] engGetCharArrayNATIVE (long epL, String nameS );
00113
00114 private native void engPutVariableNATIVE (long epL, String matrixS, double[][] valuesDD);
00115
00116 private native String engOutputBufferNATIVE (long epL, int buflenI );
00117
00118 private native void setDebugNATIVE (boolean debugB );
00119
00120
00121
00122 static {
00123 try {
00124 System.loadLibrary("JMatLink");
00125
00126
00127 }
00128 catch (UnsatisfiedLinkError e) {
00129 Debug.print("ERROR: Could not load the JMatLink library");
00130 Debug.print(" This error occures, if the path to");
00131 Debug.print(" MATLAB's <matlab>\\bin directory is");
00132 Debug.print(" not set properly.");
00133 Debug.print(" Or if JMatLink.dll is not found.");
00134 }
00135 }
00136
00139 public CoreJMatLink() {
00140 if (debugB) Debug.print("JMatLink constructor");
00141 runner = new Thread(this);
00142 runner.start();
00143 }
00144
00148 public void destroy() {
00149 destroyJMatLinkB = true;
00150 notifyAll();
00151 }
00152
00156 public void kill() {
00157 destroyJMatLinkB = true;
00158 callThread(destroyJMatLinkI);
00159 }
00160
00161
00168 public synchronized void engOpen(String startCmdS)
00169 {
00170
00171 if (engOpenPointerL != 0)
00172 throw(new JMatLinkException("engine already open"));
00173
00174 lockEngineLock();
00175 lockWaitForValue();
00176
00177 this.startCmdS = startCmdS;
00178
00179 callThread( engOpenI );
00180
00181 WaitForValue();
00182 releaseEngineLock();
00183
00184
00185 if (engOpenPointerL==0)
00186 throw(new JMatLinkException("couldn't open engine"));
00187
00188
00189
00190
00191
00192 if (enginePointerVector.contains(new Long(engOpenPointerL)))
00193 throw(new JMatLinkException("pointer already in use "+engOpenPointerL));
00194
00195
00196 enginePointerVector.add(new Long(engOpenPointerL));
00197
00198 }
00199
00205 public synchronized long engOpenSingleUse(String startCmdS)
00206 {
00207 lockEngineLock();
00208 lockWaitForValue();
00209
00210 this.startCmdS = startCmdS;
00211
00212 callThread( engOpenSingleUseI );
00213
00214 WaitForValue();
00215 releaseEngineLock();
00216
00217
00218 if (this.epL==0)
00219 throw(new JMatLinkException("couldn't open engine"));
00220
00221
00222
00223
00224
00225 if (enginePointerVector.contains(new Long(epL)))
00226 Debug.print("engine pointer already in use "+epL);
00227
00228
00229 enginePointerVector.add(new Long(epL));
00230
00231 return this.epL;
00232
00233 }
00234
00235
00239 public synchronized void engClose()
00240 {
00241
00242 engClose( engOpenPointerL );
00243
00244
00245 engOpenPointerL = 0;
00246
00247 }
00248
00252 public synchronized void engClose(long epL)
00253 {
00254
00255 if (!enginePointerVector.contains(new Long(epL)))
00256 throw(new JMatLinkException("handle does not exist"));
00257
00258 lockEngineLock();
00259 lockWaitForValue();
00260
00261 this.epL = epL;
00262
00263 callThread( engCloseI );
00264
00265 WaitForValue();
00266 releaseEngineLock();
00267
00268
00269 if (retValI!=0)
00270 throw(new JMatLinkException("engClose didn't work"));
00271
00272
00273 enginePointerVector.remove(new Long(epL));
00274
00275
00276 }
00277
00283 public synchronized void engCloseAll( )
00284 {
00285
00286
00287 while(!enginePointerVector.isEmpty())
00288 {
00289
00290 long pointer = ((Long)enginePointerVector.elementAt(0)).longValue();
00291
00292
00293 engClose(pointer);
00294
00295
00296 enginePointerVector.remove(new Long(pointer));
00297
00298
00299 if (engOpenPointerL == pointer)
00300 engOpenPointerL = 0;
00301
00302 }
00303
00304 }
00305
00311 public synchronized void engSetVisible(long epL, boolean engVisB)
00312 {
00313 if (!enginePointerVector.contains(new Long(epL)))
00314 throw(new JMatLinkException("engine unknown"));
00315
00316
00317 lockEngineLock();
00318 lockWaitForValue();
00319
00320 this.epL = epL;
00321 this.engVisB = engVisB;
00322
00323 callThread( engSetVisibleI );
00324
00325 WaitForValue();
00326 releaseEngineLock();
00327
00328
00329 if (retValI!=0)
00330 throw(new JMatLinkException("engSetVisibility didn't work"));
00331
00332 }
00333
00339 public synchronized boolean engGetVisible(long epL)
00340 {
00341 if (!enginePointerVector.contains(new Long(epL)))
00342 throw(new JMatLinkException("engine unknown"));
00343
00344 lockEngineLock();
00345 lockWaitForValue();
00346
00347 this.epL = epL;
00348
00349 callThread( engGetVisibleI );
00350
00351 WaitForValue();
00352 releaseEngineLock();
00353
00354 Debug.print("retValI " + retValI);
00355
00356 if (retValI==0)
00357 return false;
00358 else if (retValI==1)
00359 return true;
00360 else
00361 throw(new JMatLinkException("engGetVisibility didn't work"));
00362
00363 }
00364
00370 public synchronized void engEvalString(String evalS)
00371 {
00372 engEvalString( engOpenPointerL, evalS);
00373 }
00374
00375
00382 public synchronized void engEvalString(long epL, String evalS)
00383 {
00384 if (!enginePointerVector.contains(new Long(epL)))
00385 throw(new JMatLinkException("engine unknown"));
00386
00387
00388 if (debugB) Debug.print("eval(ep,String) in " + epL + " "+evalS);
00389 lockEngineLock();
00390 lockWaitForValue();
00391
00392 this.epL = epL;
00393 engEvalStringS = evalS;
00394
00395 callThread( engEvalStringI );
00396
00397 WaitForValue();
00398 releaseEngineLock();
00399
00400
00401 if (retValI!=0)
00402 throw(new JMatLinkException("engEvalString didn't work"));
00403
00404 if (debugB) Debug.print("eval(ep,String) out "+epL+" "+evalS);
00405
00406 }
00407
00408
00412 public synchronized double engGetScalar(String arrayS)
00413 {
00414 return engGetScalar( engOpenPointerL, arrayS);
00415 }
00416
00417
00424 public synchronized double engGetScalar(long epL, String arrayS)
00425 {
00426
00427
00428
00429
00430 if (!enginePointerVector.contains(new Long(epL)))
00431 throw(new JMatLinkException("engine unknown"));
00432
00433 lockEngineLock();
00434 lockWaitForValue();
00435
00436
00437 this.epL = epL;
00438 this.arrayS = arrayS;
00439
00440 callThread( engGetScalarI );
00441
00442 WaitForValue();
00443 releaseEngineLock();
00444
00445 return engGetScalarD;
00446 }
00447
00451 public synchronized double[][] engGetArray(String arrayS )
00452 {
00453 return engGetArray(engOpenPointerL, arrayS );
00454 }
00455
00458 public synchronized double[][] engGetArray(long epL, String arrayS )
00459 {
00460
00461
00462 if (!enginePointerVector.contains(new Long(epL)))
00463 throw(new JMatLinkException("engine unknown"));
00464
00465 lockEngineLock();
00466 lockWaitForValue();
00467
00468 this.epL = epL;
00469 this.arrayS = arrayS;
00470
00471 callThread( engGetArrayI );
00472 WaitForValue();
00473 releaseEngineLock();
00474
00475 return engGetArrayD;
00476 }
00477
00480 public synchronized String[] engGetCharArray(String arrayS)
00481 {
00482
00483 engEvalString( "engGetCharArrayD=double(" + arrayS +")" );
00484
00485
00486 double[][] arrayD = engGetArray("engGetCharArrayD");
00487
00488
00489 engEvalString("clear engGetCharArrayD");
00490
00491
00492
00493 if (arrayD == null) return null;
00494
00495
00496 return double2String( arrayD );
00497 }
00498
00499
00500
00503 public synchronized void engPutArray( String arrayS, double[][] valuesDD )
00504 {
00505 engPutArray( engOpenPointerL, arrayS, valuesDD );
00506 }
00507
00508
00513 public synchronized void engPutArray(long epL, String arrayS, double[][] valuesDD)
00514 {
00515
00516
00517
00518 if (!enginePointerVector.contains(new Long(epL)))
00519 throw(new JMatLinkException("engine unknown"));
00520
00521 lockEngineLock();
00522 lockWaitForValue();
00523
00524 this.epL = epL;
00525 this.arrayS = arrayS;
00526 this.engPutArray2dD = valuesDD;
00527
00528 callThread( engPutArray2dI );
00529
00530 WaitForValue();
00531 releaseEngineLock();
00532 }
00533
00537 public synchronized String engOutputBuffer( )
00538 {
00539 return engOutputBuffer( engOpenPointerL, this.buflenI );
00540 }
00541
00546 public synchronized String engOutputBuffer( long epL, int buflenI )
00547 {
00548
00549
00550 if (!enginePointerVector.contains(new Long(epL)))
00551 throw(new JMatLinkException("engine unknown"));
00552
00553 if (debugB) Debug.print("Thread in: "+Thread.currentThread().getName());
00554
00555 lockEngineLock();
00556 lockWaitForValue();
00557
00558 this.epL = epL;
00559 this.buflenI = buflenI;
00560
00561 callThread( engOutputBufferI );
00562
00563 WaitForValue();
00564 releaseEngineLock();
00565 if (debugB) Debug.print("Thread out: "+Thread.currentThread().getName());
00566
00567 return engOutputBufferS;
00568 }
00569
00570
00573 public void setDebug( boolean debugB )
00574 {
00575 this.debugB = debugB;
00576 setDebugNATIVE( debugB );
00577 }
00578
00579
00581
00582
00583
00584 private synchronized void callThread(int status)
00585 {
00586 this.status = status;
00587 lockThreadB = false;
00588 notifyAll();
00589 }
00590
00591
00593
00594
00595
00596 public synchronized void run()
00597 {
00598 if (debugB) Debug.print("JMatLink: thread is running");
00599 while (true) {
00600
00601
00602
00603
00604
00605
00606 switch (status) {
00607 case engOpenI: engOpenPointerL = engOpenNATIVE( startCmdS );
00608 releaseWaitForValue();
00609 break;
00610
00611 case engOpenSingleUseI: epL = engOpenSingleUseNATIVE( startCmdS );
00612 releaseWaitForValue();
00613 break;
00614
00615 case engCloseI: retValI = engCloseNATIVE( epL );
00616 releaseWaitForValue();
00617 break;
00618
00619 case engSetVisibleI: retValI = engSetVisibleNATIVE( epL, engVisB );
00620 releaseWaitForValue();
00621 break;
00622
00623 case engGetVisibleI: retValI = engGetVisibleNATIVE( epL );
00624 releaseWaitForValue();
00625 break;
00626
00627 case engEvalStringI: retValI = engEvalStringNATIVE(epL, engEvalStringS);
00628 releaseWaitForValue();
00629 break;
00630
00631 case engGetScalarI: engGetScalarD = engGetScalarNATIVE(epL, arrayS );
00632 releaseWaitForValue();
00633 break;
00634
00635 case engGetArrayI: engGetArrayD = engGetVariableNATIVE(epL, arrayS );
00636 releaseWaitForValue();
00637 break;
00638
00639 case engGetCharArrayI: engGetCharArrayNATIVE(epL, arrayS );
00640 releaseWaitForValue();
00641 break;
00642
00643 case engPutArray2dI: engPutVariableNATIVE( epL, arrayS, engPutArray2dD );
00644 releaseWaitForValue();
00645 break;
00646
00647 case engOutputBufferI: engOutputBufferS = engOutputBufferNATIVE( epL, buflenI );
00648 releaseWaitForValue();
00649 break;
00650
00651
00652 default:
00653 }
00654 status=0;
00655
00656 lockThreadB = true;
00657 while (lockThreadB == true) {
00658 synchronized(this) {
00659 try { wait();}
00660 catch (InterruptedException e) { }
00661 }
00662 }
00663
00664 if (destroyJMatLinkB == true) break;
00665 }
00666 if (debugB) Debug.print("JMatLink: thread terminated");
00667 }
00668
00669
00671
00672
00673
00674
00675 private void lockEngineLock(){
00676 synchronized(this){
00677 while (lockEngineB==true){
00678 try {
00679 wait();}
00680 catch (InterruptedException e) { }
00681 }
00682
00683 lockEngineB = true;
00684 }
00685 }
00686
00687 private synchronized void releaseEngineLock(){
00688 lockEngineB = false;
00689 notifyAll();
00690 }
00691
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 private synchronized void lockWaitForValue(){
00711 lockWaitForValueB = true;
00712 }
00713
00714 private void WaitForValue(){
00715 synchronized(this){
00716 while (lockWaitForValueB==true){
00717 try {
00718 wait();}
00719 catch (InterruptedException e) { }
00720 }
00721 }
00722
00723 }
00724
00725 private synchronized void releaseWaitForValue(){
00726 lockWaitForValueB = false;
00727 notifyAll();
00728 }
00729
00730
00733
00734
00735
00736 private String[] double2String(double[][] d)
00737 {
00738
00739
00740 if (d == null)
00741 {
00742
00743 return null;
00744 }
00745
00746 String encodeS[]=new String[d.length];
00747
00748
00749 for (int n=0; n<d.length; n++){
00750 byte b[] = new byte[d[n].length];
00751
00752 for (int i=0; i<d[n].length ;i++) b[i]=(byte)d[n][i];
00753
00754
00755 try { encodeS[n] = new String(b, "UTF8");}
00756 catch (UnsupportedEncodingException e) {}
00757 }
00758 return encodeS;
00759 }
00760
00761 }