KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MedorTCQuery


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2004 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.eval.api.ConnectionResources;
28 import org.objectweb.medor.eval.api.QueryEvaluator;
29 import org.objectweb.medor.eval.lib.MedorEvaluator;
30 import org.objectweb.medor.expression.api.Expression;
31 import org.objectweb.medor.expression.api.VariableOperand;
32 import org.objectweb.medor.expression.lib.BasicOperand;
33 import org.objectweb.medor.expression.lib.BasicVariableOperand;
34 import org.objectweb.medor.expression.lib.Lower;
35 import org.objectweb.medor.filter.api.FieldOperand;
36 import org.objectweb.medor.filter.lib.BasicFieldOperand;
37 import org.objectweb.medor.lib.BasicField;
38 import org.objectweb.medor.lib.BasicTupleStructure;
39 import org.objectweb.medor.optim.api.ExecPlanGenerator;
40 import org.objectweb.medor.optim.lib.IndexesGenerator;
41 import org.objectweb.medor.query.api.QueryTree;
42 import org.objectweb.medor.query.api.QueryTreeField;
43 import org.objectweb.medor.query.api.TCQueryLeaf;
44 import org.objectweb.medor.query.lib.MedorTCQueryLeaf;
45 import org.objectweb.medor.tuple.api.Tuple;
46 import org.objectweb.medor.tuple.api.TupleCollection;
47 import org.objectweb.medor.tuple.lib.ExplicitTupleCollection;
48 import org.objectweb.medor.tuple.lib.MemoryTuple;
49 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
50
51 /**
52  * This is an example of a simple query over a TupleCollection Object data store...
53  */

54 public class MedorTCQuery {
55     public static void main(String JavaDoc[] args) throws MedorException{
56         // Creating a TupleCollection QueryLeaf
57
//Creating Fields
58
Field[] fields = new Field[4];
59         fields[0] = new BasicField("NSS", PTypeSpaceMedor.INT);
60         fields[1] = new BasicField("NAME", PTypeSpaceMedor.STRING);
61         fields[2] = new BasicField("ADRESS", PTypeSpaceMedor.STRING);
62         fields[3] = new BasicField("SEX", PTypeSpaceMedor.CHAR);
63
64         // Creating a TupleStructure
65
TupleStructure ts = new BasicTupleStructure(fields);
66
67         // Creating an arrays of VariableOperand
68
VariableOperand[] vops1 = new VariableOperand[4];
69         // Creating a simple variableOperand
70
vops1[0] = new BasicVariableOperand(201);
71         vops1[1] = new BasicVariableOperand("MEDOR");
72         vops1[2] = new BasicVariableOperand("MARS");
73         vops1[3] = new BasicVariableOperand('M');
74
75         VariableOperand[] vops2 = new VariableOperand[4];
76         vops2[0] = new BasicVariableOperand(101);
77         vops2[1] = new BasicVariableOperand("JORM");
78         vops2[2] = new BasicVariableOperand("MARS");
79         vops2[3] = new BasicVariableOperand('M');
80
81         // Creating Tuples
82
Tuple t1 = new MemoryTuple(vops1);
83         Tuple t2 = new MemoryTuple(vops2);
84
85         // Creating an ExplicitTupleCollection
86
ExplicitTupleCollection tc = new ExplicitTupleCollection(ts);
87
88         //Inserting Tuples
89
tc.insertTuple(t1);
90         tc.insertTuple(t2);
91
92         TCQueryLeaf medorQTLeaf = new MedorTCQueryLeaf("", "jorm_medor", tc);
93         QueryTreeField mfield1 = medorQTLeaf.addField("NSS", PTypeSpaceMedor.INT, fields[0]);
94         QueryTreeField mfield2 = medorQTLeaf.addField("NAME", PTypeSpaceMedor.STRING, fields[1]);
95         QueryTreeField mfield3 = medorQTLeaf.addField("ADRESS", PTypeSpaceMedor.STRING, fields[2]);
96
97         // The filter expression : a filtering....
98
FieldOperand mfieldOp = new BasicFieldOperand(new BasicField("NSS", PTypeSpaceMedor.INT));
99         BasicOperand mvalue = new BasicOperand(1000);
100         Expression mFilter = new Lower(mfieldOp, mvalue);
101
102         // Settign the filter
103
medorQTLeaf.setQueryFilter(mFilter);
104
105         // Preparing execution, generate indexes and setting TupleLoader
106
ExecPlanGenerator indexesGen = new IndexesGenerator();
107
108         System.out.print(" Generating indexes...");
109         QueryTree query = null;
110         try {
111             query = indexesGen.transform(medorQTLeaf);
112         }
113         catch (Exception JavaDoc e) {
114             System.out.println("Fail:");
115             e.printStackTrace();
116         }
117         System.out.println("success");
118
119         // Creates an evaluator associated to the QueryTree
120
QueryEvaluator evaluator = new MedorEvaluator(query, 0);
121
122         // Calculates and gets the required connection resources for this query
123
ConnectionResources connRess = evaluator.getRequiredConnectionResources();
124
125         // Launching Medor Evaluator...
126
// No connections and no parameters...
127
TupleCollection queryResult = evaluator.evaluate(null, null, null);
128
129         // Browsing results
130
System.out.println("RESULTS :");
131         int cpt = 0;
132         if (!queryResult.isEmpty()) {
133             do {
134                 cpt++;
135                 System.out.println("Tuple " + cpt + " :");
136                 System.out.println(queryResult.getMetaData().getField(1).getName() +
137                         " = " + queryResult.getTuple().getObject(1));
138                 System.out.println(queryResult.getMetaData().getField(2).getName() +
139                         " = " + queryResult.getTuple().getObject(2));
140                 System.out.println();
141             } while (queryResult.next());
142         } else {
143             System.out.println("No result for this query ");
144         }
145     }
146 }
147
Popular Tags