1+ import java .util .Vector ;
2+
3+ /******************************
4+ * Copyright (c) 2003,2019 Kevin Lano
5+ * This program and the accompanying materials are made available under the
6+ * terms of the Eclipse Public License 2.0 which is available at
7+ * http://www.eclipse.org/legal/epl-2.0
8+ *
9+ * SPDX-License-Identifier: EPL-2.0
10+ * *****************************/
11+ /* Package: ATL */
12+
13+ public class InPattern
14+ { Vector elements ; // of InPatternElement
15+
16+ public InPattern ()
17+ { elements = new Vector (); }
18+
19+ public void setElements (Vector elems )
20+ { elements = elems ; }
21+
22+ public void addElement (InPatternElement ipe )
23+ { elements .add (ipe ); }
24+
25+ public int size ()
26+ { return elements .size (); }
27+
28+ public String toString ()
29+ { String res = "from " ;
30+ for (int i = 0 ; i < elements .size (); i ++)
31+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
32+ res = res + " " + ipe ;
33+ if (i < elements .size () - 1 )
34+ { res = res + ",\n " ; }
35+ }
36+ return res ;
37+ }
38+
39+ public String firstType () // Will become the owner of the constraint of the rule
40+ { if (elements .size () > 0 )
41+ { InPatternElement ipe = (InPatternElement ) elements .get (0 );
42+ return ipe .getType ();
43+ }
44+ return null ;
45+ }
46+
47+ public Entity firstEntity ()
48+ { if (elements .size () > 0 )
49+ { InPatternElement ipe = (InPatternElement ) elements .get (0 );
50+ Type typ = ipe .variable .getType ();
51+ if (typ .isEntity ())
52+ { return typ .getEntity (); }
53+ }
54+ return null ;
55+ }
56+
57+ public boolean hasType (String typ )
58+ { // typ occurs as a type of the pattern, or superclass of such a type
59+
60+ for (int i = 0 ; i < elements .size (); i ++)
61+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
62+ if (typ .equals (ipe .variable .getType () + "" ))
63+ { return true ; }
64+ }
65+ return false ;
66+ }
67+
68+ public Expression toExpression (UseCase uc )
69+ { // pre: elements.size() > 0
70+ if (elements .size () == 0 )
71+ { return new BasicExpression (true ); }
72+
73+ InPatternElement ipe0 = (InPatternElement ) elements .get (0 );
74+ Expression res = ipe0 .condition ;
75+ for (int i = 1 ; i < elements .size (); i ++)
76+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
77+ res = Expression .simplify ("&" ,res ,ipe .getFullCondition (uc ),null );
78+ }
79+ return res ;
80+ }
81+
82+ public Vector allVariables ()
83+ { Vector res = new Vector ();
84+ for (int i = 0 ; i < elements .size (); i ++)
85+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
86+ if (ipe .variable != null )
87+ { res .add (ipe .variable ); }
88+ }
89+ return res ;
90+ }
91+
92+ public int complexity ()
93+ { int result = 0 ;
94+ for (int i = 0 ; i < elements .size (); i ++)
95+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
96+ result = result + ipe .complexity ();
97+ }
98+ return result ;
99+ }
100+
101+ public int cyclomaticComplexity ()
102+ { int result = 0 ;
103+ for (int i = 0 ; i < elements .size (); i ++)
104+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
105+ result = result + ipe .cyclomaticComplexity ();
106+ }
107+ return result ;
108+ }
109+
110+ public Vector operationsUsedIn ()
111+ { Vector res = new Vector ();
112+ for (int i = 0 ; i < elements .size (); i ++)
113+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
114+ if (ipe .condition != null )
115+ { res .addAll (ipe .condition .allOperationsUsedIn ()); }
116+ }
117+ return res ;
118+ }
119+
120+ public Vector getUses (String data )
121+ { Vector res = new Vector ();
122+ for (int i = 0 ; i < elements .size (); i ++)
123+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
124+ if (ipe .condition != null )
125+ { res .addAll (ipe .condition .getUses (data )); }
126+ }
127+ return res ;
128+ }
129+
130+ }
0 commit comments