TimeInterpreter | Line # 39 | 40 | 16 | 87% |
0.8703704
|
No Tests | |||
1 | /* | |
2 | * Copyright (c) 2000-2005, University of Salford | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * Neither the name of the University of Salford nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | package issrg.pba.rbac; | |
33 | ||
34 | import issrg.pba.rbac.xmlpolicy.ifstatement.*; | |
35 | ||
36 | /** | |
37 | * This class interprets comparisons of Time values in IF-statements. | |
38 | */ | |
39 | public class TimeInterpreter implements Interpreter { | |
40 | private int mode; | |
41 | ||
42 | private final static int EQ_MODE=0; | |
43 | private final static int GE_MODE=1; | |
44 | private final static int LE_MODE=2; | |
45 | private final static int GT_MODE=3; | |
46 | private final static int LT_MODE=4; | |
47 | ||
48 | /** | |
49 | * This method registers as an interpreter for specific comparison | |
50 | * operators. | |
51 | */ | |
52 | 21 | public static void register(){ |
53 | 21 | EqNode.registerInterpreter(new TimeInterpreter(EQ_MODE)); |
54 | 21 | GeNode.registerInterpreter(new TimeInterpreter(GE_MODE)); |
55 | 21 | LeNode.registerInterpreter(new TimeInterpreter(LE_MODE)); |
56 | 21 | GtNode.registerInterpreter(new TimeInterpreter(GT_MODE)); |
57 | 21 | LtNode.registerInterpreter(new TimeInterpreter(LT_MODE)); |
58 | } | |
59 | ||
60 | 105 | protected TimeInterpreter(int mode){ |
61 | 105 | this.mode=mode; |
62 | } | |
63 | ||
64 | /** | |
65 | * This method tells if the Terms can be evaluated by this interpreter. | |
66 | * | |
67 | * @param t - the array of Terms to evaluate | |
68 | * | |
69 | * @return Types.BOOLEAN_TYPE, if the array of Terms contains only two | |
70 | * elements, and both are of type Time.TIME_TYPE; otherwise returns | |
71 | * null, i.e. can't evaluate | |
72 | */ | |
73 | 186 | public String canEvaluate(Term [] t){ |
74 | 186 | if (t!=null && t.length==2 && |
75 | t[0].getType().intern()==Time.TIME_TYPE && | |
76 | t[1].getType().intern()==t[0].getType().intern() | |
77 | ){ | |
78 | 186 | return Types.BOOLEAN_TYPE; |
79 | } | |
80 | ||
81 | 0 | return null; |
82 | } | |
83 | ||
84 | /** | |
85 | * This method evaluates the expression specified by an array of Terms, | |
86 | * given the Environment. | |
87 | * | |
88 | * @param env - the Environment of evaluation | |
89 | * @param t - the array of Terms, each containing the actual value; | |
90 | * it must contain only 2 elements, both of type Time.TIME_TYPE | |
91 | * | |
92 | * @return Boolean, the value of which tells whether the result is | |
93 | * true or false | |
94 | * | |
95 | * @throws EvaluationException, if the expression cannot be evaluated | |
96 | */ | |
97 | 122 | public Object evaluate(Environment env, Term [] t) throws EvaluationException{ |
98 | 122 | if (canEvaluate(t)==null){ |
99 | 0 | throw new EvaluationException("Cannot evaluate expression: valid time expression expected"); |
100 | } | |
101 | ||
102 | //System.out.println("\t\tevaluating time now"); | |
103 | ||
104 | //System.out.print("Left operand: "+t[0]); | |
105 | //System.out.println("\t"+t[0].evaluate(env)); | |
106 | //System.out.print("Right operand: "+t[1]); | |
107 | //System.out.println("\t"+t[1].evaluate(env)); | |
108 | ||
109 | 122 | int [] r0=((Time)(t[0].evaluate(env))).getEvaluationTime(); |
110 | 122 | int [] r1=((Time)(t[1].evaluate(env))).getEvaluationTime(); |
111 | ||
112 | 122 | boolean b=true; |
113 | 122 | boolean complete=false; // comparison is complete for GE and LE, if the operand in the sequence is strictly GT or LT |
114 | ||
115 | 508 | for (int i=0; i<r0.length; i++){ |
116 | // System.out.println(r0[i]+" cmp "+r1[i]); | |
117 | ||
118 | 502 | if (r0[i]!=-1 && r1[i]!=-1){ |
119 | 136 | switch(mode){ // the mode is specified at construction time - see the registration procedure |
120 | 0 | case EQ_MODE: |
121 | 0 | b=r0[i]==r1[i]; |
122 | 0 | break; |
123 | 49 | case GE_MODE: |
124 | 49 | complete=r0[i]>r1[i]; |
125 | 49 | b=r0[i]>=r1[i]; |
126 | 49 | break; |
127 | 17 | case LE_MODE: |
128 | 17 | complete=r0[i]<r1[i]; |
129 | 17 | b=r0[i]<=r1[i]; |
130 | 17 | break; |
131 | 35 | case GT_MODE: |
132 | 35 | b=r0[i]>r1[i]; |
133 | 35 | complete=b; |
134 | 35 | break; |
135 | 35 | case LT_MODE: |
136 | 35 | b=r0[i]<r1[i]; |
137 | 35 | complete=b; |
138 | 35 | break; |
139 | } | |
140 | ||
141 | 136 | if (complete || !b){ |
142 | 116 | break; |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | //System.out.println(b); | |
148 | ||
149 | 122 | return new Boolean(b); |
150 | } | |
151 | } |
|