Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: alice/logictuple/LogicTuple.java


1   /*
2    * Logic Tuple Communication Language - Copyright (C) 2001 deis.unibo.it
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   */
18  package alice.logictuple;
19  import alice.tuprolog.*;
20  
21  /**
22   * Defines the communication language based on
23   * logic tuples, which stand both for tuple and tuple template
24   *
25   * For logic tuple, the matching is the classic prolog matching and
26   * the propagation corresponds to unification.  A logic tuple is like a Prolog
27   * term, usually a Prolog structure characterized by a name and 0 or more
28   * argument (which in this case are objects of the class  TupleArgument)
29   *
30   * @see TupleArgument
31   * @see alice.tuplemedium.Tuple
32   * @see alice.tuplemedium.TupleTemplate
33   *
34   * @author <a href="mailto:aricci@deis.unibo.it">Alessandro Ricci</a>
35   * @version 1.0
36   */
37  public class LogicTuple implements alice.tuplemedium.Tuple, alice.tuplemedium.TupleTemplate, java.io.Serializable {
38  
39      /** the information content of logic tuple */
40      private TupleArgument info;
41  
42      /**
43       * Constructs the logic tuple providing
44       * the tuple name and argument list
45       *
46       * @param name the name of the tuple (the functor)
47       * @param list the list of tuple argument
48       */
49      public LogicTuple(String name,TupleArgument[] list){
50          info=new Value(name,list);
51      }
52  
53      /**
54       * Constructs the logic tuple providing
55       * the tuple name, without arguments
56       *
57       * @param name the name of the tuple (the functor)
58       */
59      public LogicTuple(String name){
60          info=new Value(name);
61      }
62  
63      /**
64       * Constructs the logic tuple providing
65       * the tuple name and one argument
66       *
67       * @param name the name of the tuple (the functor)
68       * @param t1 the tuple argument
69       */
70      public LogicTuple(String name,TupleArgument t1){
71          info=new Value(name,t1);
72      }
73  
74      /**
75       * Constructs the logic tuple providing
76       * the tuple name and two arguments
77       *
78       * @param name the name of the tuple (the functor)
79       * @param t1 the first tuple argument
80       * @param t2 the second tuple argument
81       */
82      public LogicTuple(String name,TupleArgument t1,TupleArgument t2){
83          info=new Value(name,t1,t2);
84      }
85  
86      /**
87       * Constructs the logic tuple providing
88       * the tuple name and three arguments
89       *
90       * @param name the name of the tuple (the functor)
91       * @param t1 the first tuple argument
92       * @param t2 the second tuple argument
93       * @param t3 the third tuple argument
94       */
95      public LogicTuple(String name,TupleArgument t1,TupleArgument t2,TupleArgument t3){
96          info=new Value(name,t1,t2,t3);
97      }
98  
99      /**
100      * Constructs the logic tuple providing
101      * the tuple name and four arguments
102      *
103      * @param name the name of the tuple (the functor)
104      * @param t1 the first tuple argument
105      * @param t2 the second tuple argument
106      * @param t3 the third tuple argument
107      * @param t4 the fourth tuple argument
108      */
109     public LogicTuple(String name,TupleArgument t1,TupleArgument t2,TupleArgument t3,TupleArgument t4){
110         info=new Value(name,t1,t2,t3,t4);
111     }
112 
113     /**
114      * Constructs the logic tuple providing
115      * the tuple name and five arguments
116      *
117      * @param name the name of the tuple (the functor)
118      * @param t1 the first tuple argument
119      * @param t2 the second tuple argument
120      * @param t3 the third tuple argument
121      * @param t4 the fourth tuple argument
122      * @param t5 the fifth tuple argument
123      */
124     public LogicTuple(String name,TupleArgument t1,TupleArgument t2,TupleArgument t3,TupleArgument t4,TupleArgument t5){
125         info=new Value(name,t1,t2,t3,t4,t5);
126     }
127 
128     /**
129      * Constructs the logic tuple providing
130      * the tuple name and six arguments
131      *
132      * @param name the name of the tuple (the functor)
133      * @param t1 the first tuple argument
134      * @param t2 the second tuple argument
135      * @param t3 the third tuple argument
136      * @param t4 the fourth tuple argument
137      * @param t5 the fifth tuple argument
138      * @param t6 the sixth tuple argument
139      */
140     public LogicTuple(String name,TupleArgument t1,TupleArgument t2,TupleArgument t3,TupleArgument t4,TupleArgument t5,TupleArgument t6){
141         info=new Value(name,t1,t2,t3,t4,t5,t6);
142     }
143 
144     /**
145      * Constructs the logic tuple providing
146      * the tuple name and seven arguments
147      *
148      * @param name the name of the tuple (the functor)
149      * @param t1 the first tuple argument
150      * @param t2 the second tuple argument
151      * @param t3 the third tuple argument
152      * @param t4 the fourth tuple argument
153      * @param t5 the fifth tuple argument
154      * @param t6 the sixth tuple argument
155      * @param t7 the seventh tuple argument
156      */
157     public LogicTuple(String name,TupleArgument t1,TupleArgument t2,TupleArgument t3,TupleArgument t4,TupleArgument t5,TupleArgument t6,TupleArgument t7){
158         info=new Value(name,t1,t2,t3,t4,t5,t6,t7);
159     }
160 
161     /**
162      * Constructs the logic tuple from a tuple argument
163      * (it's the most free form of construction)
164      *
165      * @param t the tuple argument
166      */
167     public LogicTuple(TupleArgument t){
168         info=t;
169     }
170 
171     /**
172      * Constructs the logic tuple from a tuprolog term
173      *
174      * @param t the tuprolog term
175      */
176     public LogicTuple(Term t){
177         info=new TupleArgument(t);
178     }
179 
180     public LogicTuple(){
181     }
182 
183     /**
184      * Gets the number of argument of the logic tuple
185      *
186      * @return the number of tuple arguments
187      */
188     public int getArity(){
189         return info.getArity();
190     }
191 
192     /**
193      * Gets the name of the logic tuple
194      *
195      * @return the name of the logic tuple
196      */
197     public String getName(){
198         return info.getName();
199     }
200 
201     /**
202      * Gets a argument inside the logic tuple
203      *
204      * @param index the position (index) of the argument
205      * @return the tuple argument if it exists, <code>null</code> otherwise
206      */
207     public TupleArgument getArg(int index) {
208         return info.getArg(index);
209     }
210 
211     /**
212      * Helper service to get directly a string argument inside the logic tuple
213      *
214      * @param index the position (index) of the string argument
215      * @return the string value if it exists, <code>null</code> otherwise
216      */
217     public String getString(int index){
218         return info.getString(index);
219     }
220 
221     /**
222      * Helper service to get directly an integer argument inside the logic tuple
223      *
224      * @param index the position (index) of the integer argument
225      * @return the integer value if it exists
226      */
227     public int getInt(int index){
228         return info.getInt(index);
229     }
230 
231     /**
232      * Helper service to get directly a float argument inside the logic tuple
233      *
234      * @param index the position (index) of the float argument
235      * @return the float value if it exists
236      */
237     public float getFloat(int index){
238         return info.getFloat(index);
239     }
240 
241 
242     /**
243      * Gets the Term representation of the logic tuple
244      *
245      * @return the logictuple as a term
246      */
247     public Term toTerm(){
248         return info.toTerm();
249     }
250 
251     /**
252      * Gets the string representation of the logic tuple
253      *
254      * @return the string representing the logic tuple
255      */
256     public String toString(){
257         try {
258             return info.toString();
259         } catch (Exception ex){
260             return null;
261         }
262     }
263 
264     /**
265      * Specifies if a logic tuple (as a tuple template) matches with a
266      * specified tuple, typically an other logic tuple
267      *
268      * @param t the matching tuple
269      * @return <code>true</code> if there is matching, <code>false</code> otherwise
270      */
271     public boolean match(alice.tuplemedium.Tuple t){
272         try {
273             LogicTuple tu=(LogicTuple)t;
274             Term term_a=info.toTerm();
275             Term term_b=tu.info.toTerm();
276             int count=term_a.renameVars(0);
277             term_b.renameVars(count);
278             return term_a.match(term_b);
279         } catch (Exception ex){
280             ex.printStackTrace();
281             return false;
282         }
283     }
284 
285     /**
286      * Tries to unify a logic tuple (as a tuple template) with a
287      * specified tuple, typically an other logic tuple
288      *
289      * @param t the matching tuple
290      * @return <code>true</code> if the propagation was successfull,
291      * <code>false</code> otherwise
292      */
293     public boolean propagate(alice.tuplemedium.Tuple t){
294         try {
295             LogicTuple tu=(LogicTuple)t;
296             Term term_a=info.toTerm();
297             Term term_b=tu.info.toTerm();
298             int count=term_a.renameVars(0);
299             term_b.renameVars(count);
300             return term_a.unify(term_b,0);
301         } catch (Exception ex){
302             return false;
303         }
304     }
305 
306     /**
307      * Static service to get a Logic tuple from a textual representation
308      *
309      * @param st the text representing the tuple
310      * @return the logic tuple interpreted from the text
311      * @exception InvalidLogicTupleException if the text does not represent a valid logic tuple
312      */
313     public static LogicTuple parse(String st) throws InvalidLogicTupleException {
314         Term t=alice.tuprolog.Term.parse(st);
315         if (t==null){
316             throw new InvalidLogicTupleException();
317         } else {
318             return new LogicTuple(new TupleArgument(t));
319         }
320     }
321 }
322 
323