KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MedorExpressionExample


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2002 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 import org.objectweb.medor.api.Field;
25 import org.objectweb.medor.api.MedorException;
26 import org.objectweb.medor.api.TupleStructure;
27 import org.objectweb.medor.expression.api.BinaryOperator;
28 import org.objectweb.medor.expression.api.Comparator;
29 import org.objectweb.medor.expression.api.Operand;
30 import org.objectweb.medor.expression.api.ParameterOperand;
31 import org.objectweb.medor.expression.api.Expression;
32 import org.objectweb.medor.expression.api.ExpressionException;
33 import org.objectweb.medor.expression.lib.BasicOperand;
34 import org.objectweb.medor.expression.lib.BasicParameterOperand;
35 import org.objectweb.medor.expression.lib.Minus;
36 import org.objectweb.medor.expression.lib.Plus;
37 import org.objectweb.medor.expression.lib.Greater;
38 import org.objectweb.medor.expression.lib.Substring;
39 import org.objectweb.medor.filter.api.FieldOperand;
40 import org.objectweb.medor.filter.lib.BasicFieldOperand;
41 import org.objectweb.medor.lib.BasicField;
42 import org.objectweb.medor.lib.BasicTupleStructure;
43 import org.objectweb.medor.tuple.api.Tuple;
44 import org.objectweb.medor.tuple.lib.ExplicitTupleCollection;
45 import org.objectweb.medor.tuple.lib.MemoryTuple;
46 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
47
48 /**
49  * In this example, we construct Fields, TupleStructure, TupleCollection and
50  * Expressions...
51  */

52 public class MedorExpressionExample {
53     public static void main(String JavaDoc[] args)
54             throws MedorException, ExpressionException {
55         //Creating Fields
56
Field[] fields = new Field[4];
57         fields[0] = new BasicField("NSS", PTypeSpaceMedor.INT);
58         fields[1] = new BasicField("NAME", PTypeSpaceMedor.STRING);
59         fields[2] = new BasicField("ADRESS", PTypeSpaceMedor.STRING);
60         fields[3] = new BasicField("SEX", PTypeSpaceMedor.CHAR);
61
62         // Creating a TupleStructure Object
63
TupleStructure ts = new BasicTupleStructure(fields);
64
65         // Creating an arrays of VariableOperand
66
Operand[] vops1 = new Operand[4];
67         // Creating a simple variableOperand
68
vops1[0] = new BasicOperand(201);
69         vops1[1] = new BasicOperand("MEDOR");
70         vops1[2] = new BasicOperand("MARS");
71         vops1[3] = new BasicOperand('M');
72
73         Operand[] vops2 = new Operand[4];
74         vops2[0] = new BasicOperand(101);
75         vops2[1] = new BasicOperand("JORM");
76         vops2[2] = new BasicOperand("MARS");
77         vops2[3] = new BasicOperand('M');
78
79         // Creating Tuples
80
Tuple t1 = new MemoryTuple(vops1);
81         Tuple t2 = new MemoryTuple(vops2);
82
83         // Creating an ExplicitTupleCollection
84
ExplicitTupleCollection tc = new ExplicitTupleCollection(ts);
85
86         //Inserting Tuples
87
tc.insertTuple(t1);
88         tc.insertTuple(t2);
89         tc.first();
90         tc.next();tc.next();tc.next();tc.previous();tc.previous();tc.previous();
91         System.out.println(tc.getRow());
92         //Displaying the content of the TupleCollection
93
tc.display();
94
95         // Creating an Expression = f(x,y) = ((x+13)-y)>74 + fieldOperand
96
Operand value13 = new BasicOperand(13);
97         Operand value74 = new BasicOperand(74);
98         // Creating a FieldOperand
99
FieldOperand fop = new BasicFieldOperand(fields[0]);
100         fop.setIndex(1);
101         ParameterOperand x = new BasicParameterOperand(PTypeSpaceMedor.INT, "x");
102         ParameterOperand y = new BasicParameterOperand(PTypeSpaceMedor.INT, "y");
103         BinaryOperator plus = new Plus();
104         plus.setExpression(0, value13);
105         plus.setExpression(1, x);
106         BinaryOperator minus = new Minus();
107         minus.setExpression(0, plus);
108         minus.setExpression(1, y);
109         ParameterOperand[] parameters = new BasicParameterOperand[2];
110         parameters[0] = x;
111         parameters[1] = y;
112         x.setValue(12);
113         y.setValue(99);
114         Expression plus1 = new Plus(value74, fop);
115         Comparator comp = new Greater(minus, plus1);
116         comp.compileExpression();
117         comp.evaluate(parameters, t1);
118         System.out.println("The result of the expression (12+13)-99 : " + minus.compileExpression().getInt());
119         System.out.println("The result of the expression 74 + fop : " + plus1.compileExpression().getInt());
120         System.out.println("The result of the expression ((12+13)-99)>74 : " + comp.compileExpression().getBoolean());
121         Substring subStr = new Substring(new BasicOperand("medorobjectwebforge"),minus,new BasicOperand(3));
122         subStr.compileExpression();
123         subStr.evaluate(parameters,null);
124         System.out.println("The result of the expression substring(medorobjectwebforge,13,3) = " + subStr.compileExpression().getString());
125     }
126 }
127
Popular Tags