View Javadoc

1   package cz.cuni.amis.pogamut.shady;
2   
3   import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
4   import java.math.BigDecimal;
5   import java.util.Collections;
6   import java.util.List;
7   
8   /**
9    * Query call is used to call some function in outside of engine with some
10   * parameters and return the value. {@link NodeCall} just descend further into
11   * a tree.
12   *
13   * TODO: explain, how target is determined
14   * @see NodeCall
15   * @author Honza
16   */
17  public class QueryCall implements IQuery {
18  
19      private final String name;
20      private final List<IArgument> argsUm;
21  
22      public QueryCall(String name, List<IArgument> args) {
23          this.name = name;
24          this.argsUm = Collections.unmodifiableList(args);
25      }
26  
27      /**
28       * Get name of called entity
29       * @return name of called entity, whatever {@link IWorkExecutor} will make
30       * out of it.
31       */
32      public String getName() {
33          return name;
34      }
35  
36      /**
37       * Get arguments passed to the called entity
38       * @return unmodifiable list of arguments
39       */
40      public List<IArgument> getArgs() {
41          return argsUm;
42      }
43  
44      /**
45       * Execute the primitive with name and proper arguments and return value.
46       * @return the value that query returned
47       */
48      @Override
49      public BigDecimal execute(IWorkExecutor executor) {
50          throw new UnsupportedOperationException("Not implemented yet.");
51      }
52  }
53  
54  /**
55   * Dummy query that always returns number passed in constructor.
56   * @author Honza
57   */
58  class QueryInt implements IQuery {
59  
60      private final BigDecimal number;
61  
62      public QueryInt(int number) {
63          this.number = new BigDecimal(number);
64      }
65  
66      @Override
67      public BigDecimal execute(IWorkExecutor executor) {
68          return number;
69      }
70  }
71  
72  /**
73   * Dummy query that always returns number passed in the constructor.
74   * @author Honza
75   */
76  class QueryFloat implements IQuery {
77  
78      private final BigDecimal number;
79  
80      QueryFloat(double number) {
81          this.number = new BigDecimal(number);
82      }
83  
84      @Override
85      public BigDecimal execute(IWorkExecutor executor) {
86          return number;
87      }
88  }
89  
90  /**
91   * Query operator not, when queried for value, execute passed query and return
92   * negative of that answer.
93   * @author Honza
94   */
95  class QueryNot implements IQuery {
96  
97      private final IQuery query;
98  
99      QueryNot(IQuery query) {
100         this.query = query;
101     }
102 
103     @Override
104     public BigDecimal execute(IWorkExecutor executor) {
105         BigDecimal queryResult = query.execute(executor);
106         if (queryResult.compareTo(BigDecimal.ZERO) == 0) {
107             return BigDecimal.ONE;
108         } else {
109             return BigDecimal.ZERO;
110         }
111     }
112 }
113 
114 /**
115  * Make an "and" operation for many {@link IQuery} arguments.
116  * @author Honza
117  */
118 class QueryAnd implements IQuery {
119 
120     private final List<IQuery> args;
121 
122     QueryAnd(List<IQuery> args) {
123         this.args = args;
124     }
125 
126     @Override
127     public BigDecimal execute(IWorkExecutor executor) {
128         boolean result = true;
129         for (IQuery arg : args) {
130             result &= arg.execute(executor).compareTo(BigDecimal.ZERO) != 0;
131         }
132         return result ? BigDecimal.ONE : BigDecimal.ZERO;
133     }
134 }
135 
136 /**
137  * Make an "or" operation for many {@link IQuery} arguments.
138  * @author Honza
139  */
140 class QueryOr implements IQuery {
141 
142     private final List<IQuery> args;
143 
144     QueryOr(List<IQuery> args) {
145         this.args = args;
146     }
147 
148     @Override
149     public BigDecimal execute(IWorkExecutor executor) {
150         boolean result = false;
151         for (IQuery arg : args) {
152             result |= arg.execute(executor).compareTo(BigDecimal.ZERO) != 0;
153         }
154         return result ? BigDecimal.ONE : BigDecimal.ZERO;
155     }
156 }
157 
158 abstract class QueryCmp implements IQuery {
159 
160     private final IQuery op1;
161     private final IQuery op2;
162 
163     protected QueryCmp(IQuery op1, IQuery op2) {
164         this.op1 = op1;
165         this.op2 = op2;
166     }
167 
168     /**
169      * Specify comparison operation.
170      * @param cmp -1 if op1 < op2, 0 if op1 == op2 and 1 if op1 > op2
171      * @return result of your comparison operation
172      */
173     abstract boolean cmp(int cmp);
174 
175     @Override
176     final public BigDecimal execute(IWorkExecutor executor) {
177         BigDecimal res1 = op1.execute(executor);
178         BigDecimal res2 = op2.execute(executor);
179         return cmp(res1.compareTo(res2)) ? BigDecimal.ONE : BigDecimal.ZERO;
180     }
181 }
182 
183 class QueryGt extends QueryCmp {
184 
185     QueryGt(IQuery op1, IQuery op2) {
186         super(op1, op2);
187     }
188 
189     @Override
190     protected boolean cmp(int cmp) {
191         return cmp > 0;
192     }
193 }
194 
195 class QueryGe extends QueryCmp {
196 
197     QueryGe(IQuery op1, IQuery op2) {
198         super(op1, op2);
199     }
200 
201     @Override
202     protected boolean cmp(int cmp) {
203         return cmp >= 0;
204     }
205 }
206 
207 class QueryEq extends QueryCmp {
208 
209     QueryEq(IQuery op1, IQuery op2) {
210         super(op1, op2);
211     }
212 
213     @Override
214     protected boolean cmp(int cmp) {
215         return cmp == 0;
216     }
217 }
218 
219 class QueryNe extends QueryCmp {
220 
221     QueryNe(IQuery op1, IQuery op2) {
222         super(op1, op2);
223     }
224 
225     @Override
226     protected boolean cmp(int cmp) {
227         return cmp != 0;
228     }
229 }
230 
231 class QueryLe extends QueryCmp {
232 
233     QueryLe(IQuery op1, IQuery op2) {
234         super(op1, op2);
235     }
236 
237     @Override
238     protected boolean cmp(int cmp) {
239         return cmp <= 0;
240     }
241 }
242 
243 class QueryLt extends QueryCmp {
244 
245     QueryLt(IQuery op1, IQuery op2) {
246         super(op1, op2);
247     }
248 
249     @Override
250     protected boolean cmp(int cmp) {
251         return cmp < 0;
252     }
253 }