1 //tabstop=4
2 //*****************************************************************************/
3 // Project: jpl
4 //
5 // File:    $Id$
6 // Date:    $Date$
7 // Author:  Fred Dushin <fadushin@syr.edu>
8 //
9 //
10 // Description:
11 //
12 //
13 // -------------------------------------------------------------------------
14 // Copyright (c) 2004 Paul Singleton
15 // Copyright (c) 1998 Fred Dushin
16 //                    All rights reserved.
17 //
18 // This library is free software; you can redistribute it and/or
19 // modify it under the terms of the GNU Library Public License
20 // as published by the Free Software Foundation; either version 2
21 // of the License, or (at your option) any later version.
22 //
23 // This library is distributed in the hope that it will be useful,
24 // but WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26 // GNU Library Public License for more details.
27 //*****************************************************************************/
28 package jpl;
29 
30 import java.util.Map;
31 import jpl.fli.Prolog;
32 import jpl.fli.term_t;
33 
34 //----------------------------------------------------------------------/
35 // JRef
36 /**
37  * JRef is a specialised Term with an Object field, representing JPL's Prolog references to Java objects (or to null).
38  * <pre>
39  * JRef r = new JRef( non_String_object_or_null );
40  * </pre>
41  * A JRef can be used (and re-used) in Compound Terms.
42  *
43  * <hr><i>
44  * Copyright (C) 2004  Paul Singleton<p>
45  * Copyright (C) 1998  Fred Dushin<p>
46  *
47  * This library is free software; you can redistribute it and/or
48  * modify it under the terms of the GNU Library Public License
49  * as published by the Free Software Foundation; either version 2
50  * of the License, or (at your option) any later version.<p>
51  *
52  * This library is distributed in the hope that it will be useful,
53  * but WITHOUT ANY WARRANTY; without even the implied warranty of
54  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55  * GNU Library Public License for more details.<p>
56  * </i><hr>
57  * @author  Fred Dushin <fadushin@syr.edu>
58  * @version $Revision$
59  * @see jpl.Term
60  * @see jpl.Compound
61  *
62  *  @deprecated
63  */
64 public class JRef extends Term {
65 
66 	//==================================================================/
67 	//  Attributes
68 	//==================================================================/
69 
70 	/**
71 	 * the JRef's value (a non-String Object or null)
72 	 */
73 	protected final Object ref;
74 
75 	//==================================================================/
76 	//  Constructors
77 	//==================================================================/
78 
79 	/**
80 	 * This constructor creates a JRef, initialized with the supplied
81 	 * non-String object (or null).
82 	 *
83 	 * @param   ref  this JRef's value (a non-String object, or null)
84 	 */
JRef(Object ref)85 	public JRef(Object ref) {
86 		if (ref instanceof String) {
87 			throw new JPLException("a JRef cannot have a String value (String maps to atom)");
88 		} else {
89 			this.ref = ref;
90 		}
91 	}
92 
93 	//==================================================================/
94 	//  Methods (common)
95 	//==================================================================/
96 
arg(int ano)97 	public Term arg(int ano) {
98 		return (ano == 1 ? new Atom(jpl.fli.Prolog.object_to_tag(ref)) : null);
99 	}
100 
101 	/**
102 	 * Returns a Prolog source text representation of this JRef
103 	 *
104 	 * @return  a Prolog source text representation of this JRef
105 	 */
toString()106 	public String toString() {
107 		return "" + ref + ""; // WRONG
108 	}
109 
110 	/**
111 	 * Two JRefs are equal if their references are identical (?)
112 	 *
113 	 * @param   obj  The Object to compare
114 	 * @return  true if the Object satisfies the above condition
115 	 */
equals(Object obj)116 	public final boolean equals(Object obj) {
117 		return this == obj || (obj instanceof JRef && ref == ((JRef) obj).ref);
118 	}
119 
type()120 	public final int type() {
121 		return Prolog.JREF;
122 	}
123 
typeName()124 	public String typeName(){
125 		return "JRef";
126 	}
127 
128 	//==================================================================/
129 	//  Methods (peculiar)
130 	//==================================================================/
131 
132 	/**
133 	 * The non-String object (or null) which this jpl.JRef represents
134 	 *
135 	 * @return the non-String object (or null) which this jpl.JRef represents
136 	 */
ref()137 	public Object ref() {
138 		return ref;
139 	}
140 
141 	//==================================================================/
142 	//  Methods (deprecated)
143 	//==================================================================/
144 
145 	/**
146 	 * The (nonexistent) args of this JRef
147 	 *
148 	 * @return the (nonexistent) args of this JRef
149 	 * @deprecated
150 	 */
args()151 	public Term[] args() {
152 		return new Term[] {
153 		};
154 	}
155 
156 	/**
157 	 * Returns a debug-friendly representation of this JRef
158 	 *
159 	 * @return  a debug-friendly representation of this JRef
160 	 * @deprecated
161 	 */
debugString()162 	public String debugString() {
163 		return "(JRef " + toString() + ")";
164 	}
165 
166 	//==================================================================/
167 	//  Converting JPL Terms to Prolog terms
168 	//==================================================================/
169 
170 	/**
171 	 * To convert a JRef to a term, we put its Object field (.value) into the
172 	 * term_t as a JPL ref (i.e. @/1) structure.
173 	 *
174 	 * @param   varnames_to_vars  A Map from variable names to Prolog variables.
175 	 * @param   term              A (newly created) term_t which is to be
176 	 *                            set to a Prolog 'ref' (i.e. @/1) structure denoting the
177 	 *                            .value of this JRef instance
178 	 */
put(Map varnames_to_vars, term_t term)179 	protected final void put(Map varnames_to_vars, term_t term) {
180 
181 		Prolog.put_jref(term, ref);
182 	}
183 
184 	//==================================================================/
185 	//  Computing Substitutions
186 	//==================================================================/
187 
188 	/**
189 	 * Nothing needs to be done if the Term is an Atom, Integer, Float or JRef
190 	 *
191 	 * @param   varnames_to_Terms  A Map from variable names to Terms.
192 	 * @param   vars_to_Vars       A Map from Prolog variables to JPL Variables.
193 	 */
getSubst(Map varnames_to_Terms, Map vars_to_Vars)194 	protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) {
195 	}
196 
hasFunctor(String name, int arity)197 	public boolean hasFunctor(String name, int arity) {
198 		return name != null && name.equals("@") && arity == 1;
199 	}
200 
hasFunctor(int value, int arity)201 	public boolean hasFunctor(int value, int arity) {
202 		return false;
203 	}
204 
hasFunctor(double value, int arity)205 	public boolean hasFunctor(double value, int arity) {
206 		return false;
207 	}
208 
jrefToObject()209 	public Object jrefToObject() {
210 		return ref;
211 	}
212 
213 }
214 
215 //345678901234567890123456789012346578901234567890123456789012345678901234567890
216