1// BEGIN LICENSE BLOCK
2// Version: CMPL 1.1
3//
4// The contents of this file are subject to the Cisco-style Mozilla Public
5// License Version 1.1 (the "License"); you may not use this file except
6// in compliance with the License.  You may obtain a copy of the License
7// at www.eclipse-clp.org/license.
8//
9// Software distributed under the License is distributed on an "AS IS"
10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11// the License for the specific language governing rights and limitations
12// under the License.
13//
14// The Original Code is  The ECLiPSe Constraint Logic Programming System.
15// The Initial Developer of the Original Code is  Cisco Systems, Inc.
16// Portions created by the Initial Developer are
17// Copyright (C) 2001 - 2006 Cisco Systems, Inc.  All Rights Reserved.
18//
19// Contributor(s): Josh Singer, Parc Technologies
20//
21// END LICENSE BLOCK
22
23//Title:        Java/ECLiPSe interface
24//Version:      $Id: QueueExample1.java,v 1.1 2006/09/23 01:54:13 snovello Exp $
25//Author:       Josh Singer
26//Company:      Parc Technologies
27//Description:  Java/ECLiPSe Interface example Java program
28import com.parctechnologies.eclipse.*;
29import java.io.*;
30import java.util.*;
31
32public class QueueExample1
33{
34  public static void main(String[] args) throws Exception
35  {
36    // Create some default Eclipse options
37    EclipseEngineOptions eclipseEngineOptions = new EclipseEngineOptions();
38
39    // Object representing the Eclipse process
40    EclipseEngine eclipse;
41
42    // Path of the Eclipse program
43    File eclipseProgram;
44
45    // Data going out from java
46    ToEclipseQueue java_to_eclipse;
47
48    // Data coming in from eclipse
49    FromEclipseQueue eclipse_to_java;
50
51    // EXDR translation of data going out from Java
52    EXDROutputStream java_to_eclipse_formatted;
53
54    // EXDR translation of data coming in from eclipse
55    EXDRInputStream eclipse_to_java_formatted;
56
57    // Connect the Eclipse's standard streams to the JVM's
58    eclipseEngineOptions.setUseQueues(false);
59
60    // Initialise Eclipse
61    eclipse = EmbeddedEclipse.getInstance(eclipseEngineOptions);
62
63    String sep = System.getProperty("file.separator");
64
65    // Set up the path of the example Eclipse program to be used.
66    eclipseProgram = new File(System.getProperty("eclipse.directory") +
67				   sep + "doc" + sep + "examples" + sep +
68                                   "JavaInterface" + sep +
69				   "queue_example_1.pl");
70
71    // Compile the eclipse program.
72    eclipse.compile(eclipseProgram);
73
74    // Create the two queues
75    java_to_eclipse = eclipse.getToEclipseQueue("java_to_eclipse");
76    eclipse_to_java = eclipse.getFromEclipseQueue("eclipse_to_java");
77
78    // Set up the two formatting streams
79    java_to_eclipse_formatted = new EXDROutputStream(java_to_eclipse);
80    eclipse_to_java_formatted = new EXDRInputStream(eclipse_to_java);
81
82    java_to_eclipse_formatted.write(new Atom("a"));
83    java_to_eclipse_formatted.write(new Atom("b"));
84    java_to_eclipse_formatted.flush();
85
86    eclipse.rpc("read_2_write_1");
87
88    System.out.println(eclipse_to_java_formatted.readTerm());
89
90    // Destroy the Eclipse process
91    ((EmbeddedEclipse) eclipse).destroy();
92
93  }
94
95   /**
96   * QueueListener which just writes everything to standard out.
97   */
98  static class OutputQL implements QueueListener
99  {
100    String name;
101    EXDRInputStream eis;
102
103    public OutputQL(String name, EXDRInputStream eis)
104    {
105      this.name = name;
106      this.eis = eis;
107    }
108
109    public void dataAvailable(Object source)
110    {
111      try
112      {
113        while(((InputStream) source).available() > 0)
114        {
115          System.out.println(name + ": "+eis.readTerm());
116        }
117      }
118      catch(IOException e)
119      {
120        System.err.println("IOException: "+e);
121      }
122    }
123
124    public void dataRequest(Object source){};
125  }
126
127}
128