KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > constraint > Constraint


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.constraint;
15
16 import java.io.*;
17
18
19 public class Constraint
20 {
21   private PropertySchema m_schema;
22   private Expression m_expr;
23   private String JavaDoc m_constraint;
24
25
26   private Constraint()
27   {
28   }
29
30
31   public Constraint(PropertySchema schema)
32   {
33     m_schema = schema;
34   }
35
36
37   public void parse(String JavaDoc constraint)
38     throws ParseException
39   {
40       if( constraint.length() == 0 )
41       constraint = "TRUE";
42
43     StringReader reader = new StringReader(constraint);
44     Lex lex = new Lex(reader);
45
46     m_constraint = constraint;
47
48     m_expr = new Expression(m_schema);
49
50     ValueType type = m_expr.parse(lex);
51
52       // make sure that the type of the expression is boolean, and is not
53
// a sequence
54
if (! ValueType.isCompatible(type.getId(), ValueType.BOOLEAN) ||
55         type.isSequence())
56       throw new ParseException("constraint expression must be boolean");
57   }
58
59
60   public String JavaDoc getConstraint()
61   {
62     return m_constraint;
63   }
64
65
66   public boolean evaluate(PropertySource source)
67   {
68     boolean result = false;
69
70     Value nv = m_expr.evaluate(source);
71     if (nv != null) {
72       Boolean JavaDoc b = (Boolean JavaDoc)nv.getValue();
73       result = b.booleanValue();
74     }
75
76     return result;
77   }
78
79
80   /************** comment out this line to enable main()
81
82   public static void main(String[] args)
83   {
84     if (args.length < 1) {
85       System.err.println("Usage: Constraint expr");
86       System.exit(1);
87     }
88
89     Constraint constr = new Constraint(null);
90     try {
91       constr.parse(args[0]);
92       boolean result = constr.evaluate(null);
93       System.out.println("result = " + result);
94     }
95     catch (ParseException e) {
96       System.err.println("Parse error: " + e.getMessage());
97     }
98   }
99
100   /************** comment out this line to enable main() */

101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Popular Tags