summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.booleanexpr-model/src/main/java/org/genivi/booleanexpr/impl/ComparisonPredicateImpl.java
blob: b8cb7faac288d7cd713aaba82a6bdd3c162d4045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/**
 * 
 * Copyright (C) 2013 TomTom International B.V.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 */
package org.genivi.booleanexpr.impl;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.genivi.booleanexpr.BooleanLiteral;
import org.genivi.booleanexpr.ClassAttribute;
import org.genivi.booleanexpr.ComparisonOperator;
import org.genivi.booleanexpr.ComparisonPredicate;
import org.genivi.booleanexpr.FilterexprPackage;
import org.genivi.booleanexpr.Literal;
import org.genivi.booleanexpr.SignedIntegerLiteral;
import org.genivi.booleanexpr.StringLiteral;
import org.genivi.booleanexpr.util.FilterExprUtil;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Comparison Predicate</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.genivi.booleanexpr.impl.ComparisonPredicateImpl#getLiteral <em>Literal</em>}</li>
 *   <li>{@link org.genivi.booleanexpr.impl.ComparisonPredicateImpl#getAttribute <em>Attribute</em>}</li>
 *   <li>{@link org.genivi.booleanexpr.impl.ComparisonPredicateImpl#getComparisonOperator <em>Comparison Operator</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ComparisonPredicateImpl extends PredicateImpl implements ComparisonPredicate {
  private final static Logger LOGGER = Logger.getLogger(ComparisonPredicateImpl.class.getName());
  
  /**
	 * The cached value of the '{@link #getLiteral() <em>Literal</em>}' containment reference.
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @see #getLiteral()
	 * @generated
	 * @ordered
	 */
  protected Literal literal;

  /**
	 * The cached value of the '{@link #getAttribute() <em>Attribute</em>}' containment reference.
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @see #getAttribute()
	 * @generated
	 * @ordered
	 */
  protected ClassAttribute attribute;

  /**
	 * The default value of the '{@link #getComparisonOperator() <em>Comparison Operator</em>}' attribute.
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @see #getComparisonOperator()
	 * @generated
	 * @ordered
	 */
  protected static final ComparisonOperator COMPARISON_OPERATOR_EDEFAULT = ComparisonOperator.EQUAL;

  /**
	 * The cached value of the '{@link #getComparisonOperator() <em>Comparison Operator</em>}' attribute.
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @see #getComparisonOperator()
	 * @generated
	 * @ordered
	 */
  protected ComparisonOperator comparisonOperator = COMPARISON_OPERATOR_EDEFAULT;
  
  static {
    LOGGER.setLevel(Level.SEVERE);
  }

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  protected ComparisonPredicateImpl() {
		super();
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  @Override
  protected EClass eStaticClass() {
		return FilterexprPackage.Literals.COMPARISON_PREDICATE;
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public ComparisonOperator getComparisonOperator() {
		return comparisonOperator;
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public void setComparisonOperator(ComparisonOperator newComparisonOperator) {
		ComparisonOperator oldComparisonOperator = comparisonOperator;
		comparisonOperator = newComparisonOperator == null ? COMPARISON_OPERATOR_EDEFAULT : newComparisonOperator;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FilterexprPackage.COMPARISON_PREDICATE__COMPARISON_OPERATOR, oldComparisonOperator, comparisonOperator));
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public Literal getLiteral() {
		return literal;
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public NotificationChain basicSetLiteral(Literal newLiteral, NotificationChain msgs) {
		Literal oldLiteral = literal;
		literal = newLiteral;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, FilterexprPackage.COMPARISON_PREDICATE__LITERAL, oldLiteral, newLiteral);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public void setLiteral(Literal newLiteral) {
		if (newLiteral != literal) {
			NotificationChain msgs = null;
			if (literal != null)
				msgs = ((InternalEObject)literal).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - FilterexprPackage.COMPARISON_PREDICATE__LITERAL, null, msgs);
			if (newLiteral != null)
				msgs = ((InternalEObject)newLiteral).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - FilterexprPackage.COMPARISON_PREDICATE__LITERAL, null, msgs);
			msgs = basicSetLiteral(newLiteral, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FilterexprPackage.COMPARISON_PREDICATE__LITERAL, newLiteral, newLiteral));
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public ClassAttribute getAttribute() {
		return attribute;
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public NotificationChain basicSetAttribute(ClassAttribute newAttribute, NotificationChain msgs) {
		ClassAttribute oldAttribute = attribute;
		attribute = newAttribute;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE, oldAttribute, newAttribute);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  public void setAttribute(ClassAttribute newAttribute) {
		if (newAttribute != attribute) {
			NotificationChain msgs = null;
			if (attribute != null)
				msgs = ((InternalEObject)attribute).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE, null, msgs);
			if (newAttribute != null)
				msgs = ((InternalEObject)newAttribute).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE, null, msgs);
			msgs = basicSetAttribute(newAttribute, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE, newAttribute, newAttribute));
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  @Override
  public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case FilterexprPackage.COMPARISON_PREDICATE__LITERAL:
				return basicSetLiteral(null, msgs);
			case FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE:
				return basicSetAttribute(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  @Override
  public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case FilterexprPackage.COMPARISON_PREDICATE__LITERAL:
				return getLiteral();
			case FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE:
				return getAttribute();
			case FilterexprPackage.COMPARISON_PREDICATE__COMPARISON_OPERATOR:
				return getComparisonOperator();
		}
		return super.eGet(featureID, resolve, coreType);
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  @Override
  public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case FilterexprPackage.COMPARISON_PREDICATE__LITERAL:
				setLiteral((Literal)newValue);
				return;
			case FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE:
				setAttribute((ClassAttribute)newValue);
				return;
			case FilterexprPackage.COMPARISON_PREDICATE__COMPARISON_OPERATOR:
				setComparisonOperator((ComparisonOperator)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  @Override
  public void eUnset(int featureID) {
		switch (featureID) {
			case FilterexprPackage.COMPARISON_PREDICATE__LITERAL:
				setLiteral((Literal)null);
				return;
			case FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE:
				setAttribute((ClassAttribute)null);
				return;
			case FilterexprPackage.COMPARISON_PREDICATE__COMPARISON_OPERATOR:
				setComparisonOperator(COMPARISON_OPERATOR_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

  /**
	 * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
	 * @generated
	 */
  @Override
  public boolean eIsSet(int featureID) {
		switch (featureID) {
			case FilterexprPackage.COMPARISON_PREDICATE__LITERAL:
				return literal != null;
			case FilterexprPackage.COMPARISON_PREDICATE__ATTRIBUTE:
				return attribute != null;
			case FilterexprPackage.COMPARISON_PREDICATE__COMPARISON_OPERATOR:
				return comparisonOperator != COMPARISON_OPERATOR_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated NOT
   */
  @Override
  public String toString() {
    StringBuffer result = new StringBuffer();
    
    // Attribute
    if (attribute != null) {
      result.append(attribute.toString());
    } else {
      result.append("<attribute is null>");
    }
    result.append(' ');
    
    // ComparisonOperator
    result.append(FilterExprUtil.comparisonOperatorToString(comparisonOperator));
    result.append(' ');
    
    // Literal
    if (literal != null) {
      result.append(literal.toString());
    } else {
      result.append("<literal is null>");
    }
    
    return result.toString();
  }


  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated NOT
   */
  public String format(String indent, String indentSize) {
    StringBuffer result = new StringBuffer();
    
    result.append(indent);
    result.append(toString());
    
    return result.toString();
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated NOT
   */
  public boolean evaluate(Object object) {
    LOGGER.info("evaluate => " + toString());
    boolean returnValue = false;
    Object attributeObjectValue = null;
    if (attribute != null) {
      attributeObjectValue = attribute.getValue(object);
    } else {
      attributeObjectValue = object;
    }
    
    if (attributeObjectValue == null) {
      returnValue = evaluateNull();
    } else if (attributeObjectValue instanceof Boolean) {
      returnValue = evaluateBoolean(attributeObjectValue);
    } else if (attributeObjectValue instanceof Integer) {
      returnValue = evaluateInteger(attributeObjectValue);
    } else if (attributeObjectValue instanceof Long) {
      returnValue = evaluateInteger(attributeObjectValue);
    } else if (attributeObjectValue instanceof String) {
      returnValue = evaluateString(attributeObjectValue);
    } else {
      throw new IllegalArgumentException("Unsupported attribute type in ComparisonPredicate. Attribute value = " + attributeObjectValue);
    }
    
    LOGGER.info("evaluate <= " + returnValue);
    
    return returnValue;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated NOT
   */
  private boolean evaluateNull() {
    LOGGER.info("=>");
        
    Object literalValue = null;
    if (literal instanceof BooleanLiteral) {
      literalValue = ((BooleanLiteral) literal).isValue();
    } else if (literal instanceof SignedIntegerLiteral) {
      literalValue = ((SignedIntegerLiteral) literal).getValue();
    } else if (literal instanceof StringLiteral) {
      literalValue = ((StringLiteral) literal).getValue();
    } else {
      throw new IllegalArgumentException("Unsupported literal type in ComparisonPredicate. literal = " + literal);
    }
    boolean returnValue = false;
    
    LOGGER.info("Applying: " + comparisonOperator + " on literalValue: " + literalValue);
    switch (comparisonOperator) {
    case EQUAL:
      if (literalValue == null) {
        returnValue = true;
      }
      break;
      
    case GREATER_OR_EQUAL_THAN:
      break;
      
    case GREATER_THEN:
      break;
      
    case LESS_OR_EQUAL_THAN:
      break;
      
    case LESS_THAN:
      break;
      
    case NOT_EQUAL:
      if (literalValue != null) {
        returnValue = true;
      }
      break;
    }
    
    LOGGER.info("<= " + returnValue);
    
    return returnValue;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated NOT
   */
  private boolean evaluateBoolean(Object attributeObjectValue) {
    LOGGER.info("=>");
    
    if (!(literal instanceof BooleanLiteral)) {
      throw new IllegalArgumentException("Incompatible types in ComparisonPredicate. Attribute value is Boolean, while literal is not a Boolean. Literal = " + literal);
    }
    
    boolean attributeValue = (Boolean) attributeObjectValue;
    boolean literalValue = ((BooleanLiteral) literal).isValue();
    boolean returnValue = false;
    
    switch (comparisonOperator) {
    case EQUAL:
      LOGGER.info("Applying EQUAL");
      returnValue = (attributeValue == literalValue);
      break;
      
    case GREATER_OR_EQUAL_THAN:
    case GREATER_THEN:
    case LESS_OR_EQUAL_THAN:
    case LESS_THAN:
      throw new IllegalArgumentException("Illegal comparisonOperator in ComparisonPredicate. comparisonOperator = " + comparisonOperator);
            
    case NOT_EQUAL:
      LOGGER.info("Applying NOT_EQUAL");
      returnValue = (attributeValue != literalValue);
      break;
    }
    
    LOGGER.info("<= " + returnValue);
    
    return returnValue;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated NOT
   */
  private boolean evaluateInteger(Object attributeObjectValue) {
    LOGGER.info("=>");
    
    if (!(literal instanceof SignedIntegerLiteral)) {
      throw new IllegalArgumentException("Incompatible types in ComparisonPredicate. Attribute value is Integer, while literal is not a Integer. Literal = " + literal);
    }
    
    // TODO PRIO 2: Do we have to support null in expressions? I think so!
    long attributeValue;
    if (attributeObjectValue != null) {
      if (attributeObjectValue instanceof Long) {
        attributeValue = (Long) attributeObjectValue;
      } else {
        attributeValue = new Long(((Integer) attributeObjectValue).intValue());
      }
    } else {
      throw new IllegalArgumentException("attributeObjectValue is null, which is currently not allowed");
    }
    
    long literalValue = ((SignedIntegerLiteral) literal).getValue();
    boolean returnValue = false;
    
    LOGGER.fine("Applying: " + comparisonOperator + " on attributevalue: " + attributeValue + ", and literalValue: " + literalValue);

    switch (comparisonOperator) {
    case EQUAL:
      returnValue = (attributeValue == literalValue);
      break;
      
    case GREATER_OR_EQUAL_THAN:
      returnValue = (attributeValue >= literalValue);
      break;
      
    case GREATER_THEN:
      returnValue = (attributeValue > literalValue);
      break;
      
    case LESS_OR_EQUAL_THAN:
      returnValue = (attributeValue <= literalValue);
      break;
      
    case LESS_THAN:
      returnValue = (attributeValue < literalValue);
      break;
      
    case NOT_EQUAL:
      returnValue = (attributeValue != literalValue);
      break;
    }
    
    LOGGER.info("<= " + returnValue);
    
    return returnValue;
  }

  /**
   * <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * @generated NOT
   */
  private boolean evaluateString(Object attributeObjectValue) {
    LOGGER.info("=>");
    
    if (!(literal instanceof StringLiteral)) {
      throw new IllegalArgumentException("Incompatible types in ComparisonPredicate. Attribute value is String, while literal is not a String. Literal = " + literal);
    }
    
    String attributeValue = (String) attributeObjectValue;  // TODO PRIO 2: Do we have to support null in expressions? I think so!
    String literalValue = ((StringLiteral) literal).getValue();
    boolean returnValue = false;
    
    LOGGER.info("Applying: " + comparisonOperator);
    switch (comparisonOperator) {
    case EQUAL:
      returnValue = attributeValue.equals(literalValue);
      break;
      
    case GREATER_OR_EQUAL_THAN:
    case GREATER_THEN:
    case LESS_OR_EQUAL_THAN:
    case LESS_THAN:
      throw new IllegalArgumentException("Illegal comparisonOperator in ComparisonPredicate. comparisonOperator = " + comparisonOperator);
      
    case NOT_EQUAL:
      returnValue = !attributeValue.equals(literalValue);
      break;
    }
    
    LOGGER.info("<= " + returnValue);
    
    return returnValue;
  }
} //ComparisonPredicateImpl