/[lambda4j]/lambda4j/src/tests/LogicalVariableTest.latte
ViewVC logotype

Contents of /lambda4j/src/tests/LogicalVariableTest.latte

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Tue Jan 7 09:20:41 2003 UTC (21 years, 4 months ago) by dauclair
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +40 -14 lines
Improved handling of logical variables -- comparison on values are now
value-to-value (without interposed transformations).  "Bare" logical
variables now accept unification (instead of failing on unification).

Added comments to fields of functional objects.

1 import lists.*;
2 import strings.*;
3 import logical.*;
4 import functional.*;
5
6 /*
7
8 * Copyright (c) 2003, Cotillion Group, Inc.
9
10 * This file is part of The Functionals Library For Java.
11
12 * The Functionals Library For Java is free software; you can
13 * redistribute it and/or modify it under the terms of the GNU Lesser General
14 * Public License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16
17 * The Functionals Library For Java is distributed in the hope that
18 * it will be useful, but WITHOUT ANY WARRANTY; without even the
19 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU Lesser General Public License for more details.
21
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with The Functionals Library For Java; if not, write
24 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
25 * Boston, MA 02111-1307 USA
26 */
27
28 /**
29 * Tests logical variables by creating a functional loop with
30 * functional conditionals -- pretty cool! if it works.
31 *
32 * What this test does is to create two functionals a) one that returns
33 * an Integer instance as a string, and b) the other that returns "0"
34 * plus the stringified integer instance. The logical variable
35 * dispatches based on the value of the integer x < 10 (to b) and
36 * otherwise (to a).
37 *
38 * @author Douglas M. Auclair */
39 public class LogicalVariableTest extends Fn implements FnBottom
40 {
41 public static void main(String args[])
42 {
43 // first, build the dispatch table for the logical variable
44 final Function add_zero = \x . "0" ++ x;
45 final Function gt10 = \'lt(10);
46 final Function lt10 = \'gt(10);
47 AssociationList alist = new AssociationList();
48 alist.add(#(gt10, asString));
49 alist.add(#(lt10, add_zero));
50
51 // now map the dispatch table into the lv and create our fn
52 final LogicalVariable two_digits = new LogicalVariable(alist);
53
54 // all the above will be replaced by:
55 // two_digits = \ X :- X < 10 . "0" ++ X.
56 // \ X :- X > 10 . X;
57
58 final LazyList threes = map(\'mult(3), ListUtils.N);
59
60 System.out.println("Testing logical variables:");
61 System.out.println(Fn.map(two_digits.rebinder(), threes));
62
63 System.out.println("Testing logical variable failure "
64 + "(fails test):");
65 try {
66 two_digits.unify(new Integer(10));
67 System.out.println(two_digits.value());
68 } catch(Exception ex) { System.out.println(ex); }
69
70 System.out.println("Testing open logical variable:");
71 {
72 LogicalVariable var = new LogicalVariable();
73 var.unify("foo");
74 System.out.println(var.value());
75 }
76
77 System.out.println("Testing rebind of logical variable:");
78 two_digits.unify(new Integer(15));
79 System.out.println(two_digits.value());
80 two_digits.unify(new Integer(15));
81 System.out.println(two_digits.value());
82
83 String[] index_types = { "Valued customer", "Dog",
84 "Car", "Cat", "Food",
85 "Mint Chocolate Chip Ice cream" };
86 System.out.println("Testing dispatch on strings for logical "
87 + "variables:");
88 System.out.println(map(xmlName, index_types));
89 }
90
91 private static Function xmlName = new Function() {
92 public Object apply(Object obj) {
93 // here's where the predicate calculus syntax would really help
94 // e.g.: chars = \char :- char = ' ' . '_'.
95 // :- otherwise . char;
96 // (or something like that, I'm still not sure of the syntax)
97
98 // anyway, first build the dispatch table for the logical variable
99 final Function isSpc = \'eq(' ');
100 final Function dash = \'always('_');
101 AssociationList alist = new AssociationList();
102 alist.add(#(isSpc, dash));
103 alist.add(#(otherwise, identity));
104
105 // now map the dispatch table into the lv and create our fn
106 final LogicalVariable chars = new LogicalVariable(alist);
107
108 return StringUtils.fromList(map(chars.rebinder(), obj));
109 }
110 };
111 }

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26