1 package org.andromda.translation.ocl.syntax;
2
3 import org.andromda.core.translation.TranslationUtils;
4
5 /**
6 * Contains the patterns matching constructs within the OCL language.
7 *
8 * @author Chad Brandon
9 */
10 public class OCLPatterns
11 {
12 /**
13 * Matches on the pattern of a scope path (i.e. java :: lang :: Integer).
14 */
15 static final String SCOPE_PATH = "[\\w+|::]+";
16
17 /**
18 * Matches on the pattern of a navigational path (i.e. person.name.first)
19 */
20 static final String NAVIGATIONAL_PATH = "[\\w*[\\.]*]+";
21
22 /**
23 * Indicates if this <code>expression</code> is an operation.
24 *
25 * @param expression the expression to match.
26 * @return true/false
27 */
28 public static boolean isOperation(Object expression)
29 {
30 return TranslationUtils.deleteWhitespace(expression).matches(OPERATION);
31 }
32
33 private static final String OPERATION = ".+\\(.*\\).*";
34
35 /**
36 * Indicates if this <code>expression</code> is a collection (->) operation call.
37 *
38 * @param expression the expression to match.
39 * @return true/false
40 */
41 public static boolean isCollectionOperationCall(Object expression)
42 {
43 return TranslationUtils.deleteWhitespace(expression).matches(COLLECTION_CALL);
44 }
45
46 private static final String COLLECTION_CALL = ".+->.+";
47
48 /**
49 * Indicates if this <code>expression</code> is a collection operation result navigational path
50 * (->someOperation().some.path)
51 *
52 * @param expression the expression to match.
53 * @return true/false
54 */
55 public static boolean isCollectionOperationResultNavigationalPath(Object expression)
56 {
57 return TranslationUtils.deleteWhitespace(expression).matches(COLLECTION_CALL_RESULT_NAVIGATIONAL_PATH);
58 }
59
60 /**
61 * Indicates if this <code>expression</code> is a navigational path
62 * (some.path)
63 *
64 * @param expression the expression to match.
65 * @return true/false
66 */
67 public static boolean isNavigationalPath(Object expression)
68 {
69 return TranslationUtils.deleteWhitespace(expression).matches(NAVIGATIONAL_PATH);
70 }
71
72 private static final String COLLECTION_CALL_RESULT_NAVIGATIONAL_PATH = ".+->" + OPERATION + NAVIGATIONAL_PATH;
73
74 /**
75 * Pattern for <em>and</em> and <em>or</em> expression matching.
76 */
77 private static final String AND_OR_OR_OPERATOR = "or\\s+.*|and\\s+.*";
78
79 /**
80 * Indicates if this <code>expression</code> is an <em>and</em>
81 * or <em>or</em> expression (or some.path, and some.path, etc)
82 *
83 * @param expression the expression to match.
84 * @return true/false
85 */
86 public static boolean isAndOrOrExpression(Object expression)
87 {
88 return TranslationUtils.trimToEmpty(expression).matches(AND_OR_OR_OPERATOR);
89 }
90 }