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) 2006 Cisco Systems, Inc. All Rights Reserved. 18// 19// Contributor(s): 20// 21// END LICENSE BLOCK 22 23package com.parctechnologies.eclipse.visualisation; 24 25import com.parctechnologies.eclipse.*; 26import java.util.*; 27import java.io.*; 28 29/** 30 * The BatchGoal class is used in conjunction with a simple ECLiPSe library 31 * to execute a number of goals independently at once (i.e. within a single 32 * invocation of rpc). The advantage of this is that it minimises the amount of 33 * communication with ECLiPSe, especially useful when the connection is with a 34 * RemoteEclipse and each rpc call requires several TCP/IP packets. <p> 35 * See the <code>batch_goals</code> library documentation to see how the 36 * ECLiPSe side of this design works.<p> 37 * On the Java side, the implementation is very simple. A BatchGoal is a kind of 38 * LinkedList. BatchGoals may in fact be nested. The ECLiPSe library detects 39 * this and executes nested batched goals appropriately. The key method is 40 * execute, which takes an EclipseConnection. In this method, the BatchGoal 41 * simply ensures that the <code>batch_goals</code> library is loaded, then 42 * passes itself in to an <code>execute_batch</code> goal as the first 43 * argument. It executes the goal using the rpc method of the Eclipse 44 * Connection. Finally, it extracts the results of the goal from the second 45 * argument and returns these, cast to a List. The results can be deconstructed 46 * by the client. 47 * 48 * 49 */ 50public class BatchGoal extends LinkedList 51{ 52 private static final Atom batchGoalsAtom = new Atom("batch_goals"); 53 54 private static final CompoundTerm libGoal = 55 new CompoundTermImpl("ensure_loaded", 56 new CompoundTermImpl("library", 57 batchGoalsAtom)); 58 59 // execute the batched goal in eclipse, return results as a collection. 60 // NB results of nested batched goals will be returned as inner-nested 61 // collections. The atomic elements are EclipseExceptions or CompoundTerms 62 List execute(EclipseConnection eclipse) 63 throws EclipseException, IOException 64 { 65 if(this.isEmpty()) 66 { 67 return(Collections.EMPTY_LIST); 68 } 69 CompoundTerm executeGoal = 70 new CompoundTermImpl(":", batchGoalsAtom, 71 new CompoundTermImpl("execute_batch", this, null)); 72 CompoundTerm fullGoal = 73 new CompoundTermImpl(",", libGoal, executeGoal); 74 75 CompoundTermImpl result = (CompoundTermImpl) eclipse.rpc(fullGoal); 76 return((List) (result.argCT(2).argCT(2).arg(2))); 77 } 78 79} 80