1   package org.jaxen.expr;
2   
3   import java.util.Iterator;
4   import java.util.List;
5   
6   import org.jaxen.Context;
7   import org.jaxen.JaxenException;
8   import org.jaxen.Navigator;
9   import org.openrdf.model.Value;
10  
11  import eu.fbk.knowledgestore.data.Data;
12  import eu.fbk.knowledgestore.data.XPath;
13  
14  abstract class DefaultRelationalExpr extends DefaultTruthExpr implements RelationalExpr {
15  
16      /**
17       *
18       */
19      private static final long serialVersionUID = -3565329669240565813L;
20  
21      DefaultRelationalExpr(final Expr lhs, final Expr rhs) {
22          super(lhs, rhs);
23      }
24  
25      @Override
26      public String toString() {
27          return "[(DefaultRelationalExpr): " + getLHS() + ", " + getRHS() + "]";
28      }
29  
30      @Override
31      public Object evaluate(final Context context) throws JaxenException {
32          final Object lhsValue = getLHS().evaluate(context);
33          final Object rhsValue = getRHS().evaluate(context);
34          final Navigator nav = context.getNavigator();
35  
36          if (bothAreSets(lhsValue, rhsValue)) {
37              return evaluateSetSet((List) lhsValue, (List) rhsValue, nav);
38          }
39  
40          if (eitherIsSet(lhsValue, rhsValue)) {
41              if (isSet(lhsValue)) {
42                  return evaluateSetSet((List) lhsValue, convertToList(rhsValue), nav);
43              } else {
44                  return evaluateSetSet(convertToList(lhsValue), (List) rhsValue, nav);
45              }
46          }
47  
48          return evaluateObjectObject(lhsValue, rhsValue, nav) ? Boolean.TRUE : Boolean.FALSE;
49      }
50  
51      private Object evaluateSetSet(final List lhsSet, final List rhsSet, final Navigator nav) {
52          if (setIsEmpty(lhsSet) || setIsEmpty(rhsSet)) // return false if either is null or empty
53          {
54              return Boolean.FALSE;
55          }
56  
57          for (final Iterator lhsIterator = lhsSet.iterator(); lhsIterator.hasNext();) {
58              final Object lhs = lhsIterator.next();
59  
60              for (final Iterator rhsIterator = rhsSet.iterator(); rhsIterator.hasNext();) {
61                  final Object rhs = rhsIterator.next();
62  
63                  if (evaluateObjectObject(lhs, rhs, nav)) {
64                      return Boolean.TRUE;
65                  }
66              }
67          }
68  
69          return Boolean.FALSE;
70      }
71  
72      private boolean evaluateObjectObject(final Object lhs, final Object rhs, final Navigator nav) {
73  
74          if (lhs == null || rhs == null) {
75              return false;
76          }
77  
78          final Value lhsValue = Data.convert(XPath.unwrap(lhs), Value.class);
79          final Value rhsValue = Data.convert(XPath.unwrap(rhs), Value.class);
80  
81          final int result = Data.getTotalComparator().compare(lhsValue, rhsValue);
82  
83          switch (getOperator()) {
84          case "<":
85              return result < 0;
86          case ">":
87              return result > 0;
88          case "=":
89              return result == 0;
90          case "<=":
91              return result <= 0;
92          case ">=":
93              return result >= 0;
94          case "!=":
95              return result != 0;
96          default:
97              return false;
98          }
99          //
100         // final Double lhsNum = NumberFunction.evaluate(lhs, nav);
101         // final Double rhsNum = NumberFunction.evaluate(rhs, nav);
102         //
103         // if (NumberFunction.isNaN(lhsNum) || NumberFunction.isNaN(rhsNum)) {
104         // return false;
105         // }
106         //
107         // return evaluateDoubleDouble(lhsNum, rhsNum);
108     }
109 
110     protected abstract boolean evaluateDoubleDouble(Double lhs, Double rhs);
111 
112 }