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) 2000 - 2006 Cisco Systems, Inc. All Rights Reserved. 18// 19// Contributor(s): Stefano Novello / Josh Singer, Parc Technologies 20// 21// END LICENSE BLOCK 22 23//Title: Java/ECLiPSe interface 24//Version: $Id: CompoundTermImpl.java,v 1.1 2006/09/23 01:54:08 snovello Exp $ 25//Author: Stefano Novello / Josh Singer 26//Company: Parc Technologies 27//Description: Class for constructing Java objects representing ECLiPSe compound terms. 28package com.parctechnologies.eclipse; 29 30/** 31 * An ECLiPSe compound term. This concrete class can be used to construct objects 32 * which implement the CompoundTerm interface. 33 * @see CompoundTerm 34 */ 35public class CompoundTermImpl extends AbstractCompoundTerm implements CompoundTerm 36{ 37 /** 38 * Element 0 of term is the functor String, the remaining elements are the 39 * compound term's arguments. Arity is therefore term.length - 1 40 * 41 */ 42 private Object term[]; 43 44 /** 45 * Construct a compound term from an Object array. In this constructor the 46 * functor (which should be a String) 47 * is passed in as element 0 of the array parameter, the other arguments, 48 * which can 49 * be instances of any Java classes/interfaces representing ECLiPSe types, 50 * are the remaining elements of the array. The arity is 51 * therefore the size of the array - 1. 52 */ 53 public CompoundTermImpl(Object[] term) 54 { 55 // call AbstractCompoundTerm constructor setting up functor and arity 56 super((String) term[0], term.length - 1); 57 this.term = term; 58 } 59 60 /** 61 * Construct a compound term from a String and an Object array. User 62 * supplies functor and an array of argument objects. 63 * Arity of the resulting compound term is <code>args.length</code>. 64 */ 65 public CompoundTermImpl(String functor, Object[] args) 66 { 67 // call AbstractCompoundTerm constructor setting up functor and arity 68 super(functor, args.length); 69 int i; 70 term = new Object[args.length + 1]; 71 term[0] = functor; 72 for(i = 0; i < args.length; i++) 73 { 74 term[i+1] = args[i]; 75 } 76 } 77 78 /** 79 * Convenience constructor for compound terms with arity 1. 80 */ 81 public CompoundTermImpl(String functor,Object a1) 82 { 83 this(new Object[] {functor,a1}); 84 } 85 /** 86 * Convenience constructor for terms with arity 2. 87 */ 88 public CompoundTermImpl(String functor,Object a1,Object a2) 89 { 90 this(new Object[] {functor,a1,a2}); 91 } 92 93 /** 94 * Convenience constructor for compound terms with arity 3. 95 */ 96 public CompoundTermImpl(String functor,Object a1,Object a2,Object a3) 97 { 98 this(new Object[] {functor,a1,a2,a3}); 99 } 100 101 /** 102 * Convenience constructor for compound terms with arity 4. 103 */ 104 public CompoundTermImpl(String functor,Object a1,Object a2,Object a3,Object a4) 105 { 106 this(new Object[] {functor,a1,a2,a3,a4}); 107 } 108 /** 109 * Convenience constructor for compound terms with arity 5. 110 */ 111 public CompoundTermImpl(String functor,Object a1,Object a2,Object a3,Object a4,Object a5) 112 { 113 this(new Object[] {functor,a1,a2,a3,a4,a5}); 114 } 115 116 /** 117 * Return one of the term's arguments. These may be instances of any Java 118 * class/interface representing an ECLiPSe type. 119 * @param i the argument index. This may vary between 1 and <code>arity()</code> inclusive. 120 * 121 */ 122 public Object arg(int i) 123 { 124 if(i == 0 || i > arity()) 125 { 126 throw new IllegalArgumentException("Argument index must be between 1 and "+ 127 arity()+" inclusive."); 128 129 } 130 return term[i]; 131 } 132 133 /** 134 * Return the argument at position <code>i</code>, as a CompoundTermImpl. 135 * This operation is the same as arg, except that it tries to cast the result 136 * to a CompoundTermImpl. Useful for extracting nested objects: eg. 137 * <code>a = result.argCT(1).argCT(3).argCT(3);</code> 138 * @param i may vary between 1 and <code>arity()</code> 139 */ 140 public CompoundTermImpl argCT(int i) 141 { 142 return((CompoundTermImpl) arg(i)); 143 } 144 145 public String toString() 146 { 147 String result = this.getClass().getName() + " with ["; 148 result += "functor="+functor(); 149 result += " arity="+arity(); 150 for(int i = 1; i <= arity(); i++) 151 { 152 result += " arg("+i+")="+arg(i); 153 } 154 result +="]"; 155 // System.err.println("WARNING: toString called on"+result); 156 return(result); 157 } 158 159 160} 161 162