Skip to main content
summaryrefslogtreecommitdiffstats
blob: cc03dc79b59c9e6828518708ae78ed3c5bbf2b9f (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.dsl.integration.internal;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.dsl.integration.ArtifactDataProvider.ArtifactProxy;
import org.eclipse.osee.framework.core.dsl.oseeDsl.CompareOp;
import org.eclipse.osee.framework.core.dsl.oseeDsl.CompoundCondition;
import org.eclipse.osee.framework.core.dsl.oseeDsl.Condition;
import org.eclipse.osee.framework.core.dsl.oseeDsl.MatchField;
import org.eclipse.osee.framework.core.dsl.oseeDsl.SimpleCondition;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XArtifactMatcher;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XLogicOperator;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactMatchInterpreter {

   public boolean matches(XArtifactMatcher matcher, Collection<ArtifactProxy> proxies) throws OseeCoreException {
      boolean matched = false;
      Iterator<ArtifactProxy> iterator = proxies.iterator();
      while (iterator.hasNext() && !matched) {
         matched = matches(matcher, iterator.next());
         if (matched) {
            break;
         }
      }
      return matched;
   }

   public boolean matches(XArtifactMatcher matcher, ArtifactProxy proxy) throws OseeCoreException {
      List<Condition> conditions = matcher.getConditions();
      List<XLogicOperator> operators = matcher.getOperators();
      return evaluate(conditions, operators, proxy);
   }

   boolean evaluate(List<? extends Condition> conditions, List<? extends XLogicOperator> operators, ArtifactProxy proxy) throws OseeCoreException {
      boolean result = false;
      Iterator<? extends Condition> iteratorConds = conditions.iterator();
      Iterator<? extends XLogicOperator> iteratorOps = operators.iterator();
      if (iteratorConds.hasNext()) {
         Condition lastCondition = iteratorConds.next();
         if (iteratorOps.hasNext()) {
            while (iteratorOps.hasNext() && iteratorConds.hasNext()) {
               XLogicOperator op = iteratorOps.next();
               Condition condition = iteratorConds.next();
               result = evaluate(op, lastCondition, condition, proxy);
            }
         } else {
            result = evaluate(lastCondition, proxy);
         }
      }
      return result;
   }

   boolean evaluate(XLogicOperator op, Condition conditionA, Condition conditionB, ArtifactProxy proxy) throws OseeCoreException {
      boolean result = evaluate(conditionA, proxy);
      if (op == XLogicOperator.AND) {
         return result && evaluate(conditionB, proxy);
      } else if (op == XLogicOperator.OR) {
         return result || evaluate(conditionB, proxy);
      }
      throw new OseeArgumentException("Invalid op defined: %s", op);
   }

   boolean evaluate(Condition condition, ArtifactProxy proxy) throws OseeCoreException {
      if (condition instanceof SimpleCondition) {
         return evaluate((SimpleCondition) condition, proxy);
      } else if (condition instanceof CompoundCondition) {
         CompoundCondition group = (CompoundCondition) condition;
         return evaluate(group.getConditions(), group.getOperators(), proxy);
      }
      throw new OseeArgumentException("Invalid Condition defined: %s", condition);
   }

   boolean evaluate(SimpleCondition condition, ArtifactProxy proxy) throws OseeCoreException {
      String expression = Strings.unquote(condition.getExpression());
      Conditions.checkNotNullOrEmpty(expression, "expression");

      String input = null;
      MatchField field = condition.getField();
      switch (field) {
         case ARTIFACT_GUID:
            input = proxy.getGuid();
            Conditions.checkExpressionFailOnTrue(!GUID.isValid(input), "guid");
            break;
         case BRANCH_GUID:
            input = proxy.getBranch().getGuid();
            Conditions.checkExpressionFailOnTrue(!GUID.isValid(input), "guid");
            break;
         case ARTIFACT_NAME:
            input = proxy.getName();
            break;
         case BRANCH_NAME:
            input = proxy.getBranch().getName();
            break;
         default:
            throw new OseeArgumentException("Invalid field [%s]", field);
      }
      Conditions.checkNotNullOrEmpty(input, field.getName());
      CompareOp op = condition.getOp();
      boolean result = false;
      if (op == CompareOp.EQ) {
         result = input.equals(expression);
      } else if (op == CompareOp.LIKE) {
         Matcher matcher = expressions.get(expression);
         if (matcher == null) {
            Pattern pattern = Pattern.compile(expression);
            matcher = pattern.matcher("");
            expressions.put(expression, matcher);
         }
         synchronized (matcher) {
            matcher.reset(input);
            result = matcher.find();
         }
      } else {
         throw new OseeArgumentException("Invalid CompareOp [%s]", op);
      }
      return result;
   }
   private final Map<String, Matcher> expressions = new ConcurrentHashMap<String, Matcher>();

}

Back to the top