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: QueueExample2.java,v 1.1 2006/09/23 01:54:13 snovello Exp $ 25//Copyright: Copyright (c) 2001 26//Author: Josh Singer 27//Company: Parc Technologies 28//Description: Java/ECLiPSe Interface example Java program 29import com.parctechnologies.eclipse.*; 30import java.io.*; 31import java.util.*; 32 33public class QueueExample2 34{ 35 // Create some default Eclipse options 36 static EclipseEngineOptions eclipseEngineOptions = new EclipseEngineOptions(); 37 38 // Object representing the Eclipse process 39 static EclipseEngine eclipse; 40 41 // Path of the Eclipse program 42 static File eclipseProgram; 43 44 // Data going out from java 45 static ToEclipseQueue java_to_eclipse; 46 47 // Data coming in from eclipse 48 static FromEclipseQueue eclipse_to_java; 49 50 public static void main(String[] args) throws Exception 51 { 52 // Connect the Eclipse's standard streams to the JVM's 53 eclipseEngineOptions.setUseQueues(false); 54 55 // Initialise Eclipse 56 eclipse = EmbeddedEclipse.getInstance(eclipseEngineOptions); 57 58 String sep = System.getProperty("file.separator"); 59 60 // Set up the path of the example Eclipse program to be used. 61 eclipseProgram = new File(System.getProperty("eclipse.directory") + 62 sep + "doc" + sep + "examples" + sep + 63 "JavaInterface" + sep + 64 "queue_example_2.pl"); 65 66 // Compile the eclipse program. This sets up the two queue streams 67 eclipse.compile(eclipseProgram); 68 69 // Set up the java representation of the two queue streams 70 java_to_eclipse = eclipse.getToEclipseQueue("java_to_eclipse"); 71 eclipse_to_java = eclipse.getFromEclipseQueue("eclipse_to_java"); 72 73 // add a TermProducer as a listener to the java_to_eclipse ToEclipseQueue 74 java_to_eclipse.setListener(new TermProducer()); 75 76 // add a TermConsumer as a listener to the eclipse_to_java FromEclipseQueue 77 eclipse_to_java.setListener(new TermConsumer()); 78 79 eclipse.rpc("read_5_write_5"); 80 81 // Destroy the Eclipse process 82 ((EmbeddedEclipse) eclipse).destroy(); 83 84 } 85 86 /** 87 * QueueListener whose dataRequest method sends 5 different atoms 88 * in exdr format along the queue it is attached to. 89 */ 90 static class TermProducer implements QueueListener 91 { 92 ToEclipseQueue output_queue_stream = null; 93 EXDROutputStream output_queue_stream_formatted = null; 94 String[] atoms = {"mercury", "venus", "earth", "mars", "jupiter"}; 95 int atom_number = 0; 96 97 // Required to implement QueueListener 98 public void dataAvailable(Object source) 99 { 100 } 101 102 // Called when Eclipse tries to read from source when it is empty. 103 public void dataRequest(Object source) 104 { 105 if(output_queue_stream == null) 106 { 107 output_queue_stream = (ToEclipseQueue) source; 108 output_queue_stream_formatted = 109 new EXDROutputStream(output_queue_stream); 110 } 111 112 try 113 { 114 output_queue_stream_formatted.write(atoms[atom_number]); 115 output_queue_stream_formatted.flush(); 116 } catch(IOException ioe){} 117 118 atom_number++; 119 if(atom_number == 5) 120 atom_number = 0; 121 } 122 } 123 124 /** 125 * QueueListener whose dataAvailable method reads a term 126 * in exdr format from the queue it is attached to, converts it to the 127 * Java representation of the term, and prints this out to stdout. 128 */ 129 static class TermConsumer implements QueueListener 130 { 131 FromEclipseQueue input_queue_stream = null; 132 EXDRInputStream input_queue_stream_formatted = null; 133 134 // Called when Eclipse flushes source 135 public void dataAvailable(Object source) 136 { 137 if(input_queue_stream == null) 138 { 139 input_queue_stream = (FromEclipseQueue) source; 140 input_queue_stream_formatted = 141 new EXDRInputStream(input_queue_stream); 142 } 143 144 try 145 { 146 System.out.println(input_queue_stream_formatted.readTerm()); 147 } catch(IOException ioe){} 148 149 } 150 151 // Required to implement QueueListener 152 public void dataRequest(Object source) 153 { 154 } 155 } 156 157} 158