1+ import java .util .Vector ;
2+
3+ /******************************
4+ * Copyright (c) 2003-2023 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 =
42+ (InPatternElement ) elements .get (0 );
43+ return ipe .getType ();
44+ }
45+ return null ;
46+ }
47+
48+ public Entity firstEntity ()
49+ { if (elements .size () > 0 )
50+ { InPatternElement ipe =
51+ (InPatternElement ) elements .get (0 );
52+ if (ipe .variable == null )
53+ { return null ; }
54+ Type typ = ipe .variable .getType ();
55+ if (typ .isEntity ())
56+ { return typ .getEntity (); }
57+ }
58+ return null ;
59+ }
60+
61+ public boolean hasType (String typ )
62+ { // typ occurs as a type of the pattern, or superclass of such a type
63+
64+ for (int i = 0 ; i < elements .size (); i ++)
65+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
66+ if (typ .equals (ipe .variable .getType () + "" ))
67+ { return true ; }
68+ }
69+ return false ;
70+ }
71+
72+ public Expression toExpression (UseCase uc )
73+ { // pre: elements.size() > 0
74+ if (elements .size () == 0 )
75+ { return new BasicExpression (true ); }
76+
77+ InPatternElement ipe0 = (InPatternElement ) elements .get (0 );
78+ Expression res = ipe0 .condition ;
79+ for (int i = 1 ; i < elements .size (); i ++)
80+ { InPatternElement ipe =
81+ (InPatternElement ) elements .get (i );
82+ res = Expression .simplify ("&" ,res ,
83+ ipe .getFullCondition (uc ),null );
84+ }
85+ return res ;
86+ }
87+
88+ public Vector allVariables ()
89+ { Vector res = new Vector ();
90+ for (int i = 0 ; i < elements .size (); i ++)
91+ { InPatternElement ipe =
92+ (InPatternElement ) elements .get (i );
93+ if (ipe .variable != null )
94+ { res .add (ipe .variable ); }
95+ }
96+ return res ;
97+ }
98+
99+ public Vector allVariablesAsExpressions ()
100+ { Vector res = new Vector ();
101+ for (int i = 0 ; i < elements .size (); i ++)
102+ { InPatternElement ipe =
103+ (InPatternElement ) elements .get (i );
104+ if (ipe .variable != null )
105+ { res .add (new BasicExpression (ipe .variable )); }
106+ }
107+ return res ;
108+ }
109+
110+ public int complexity ()
111+ { int result = 0 ;
112+ for (int i = 0 ; i < elements .size (); i ++)
113+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
114+ result = result + ipe .complexity ();
115+ }
116+ return result ;
117+ }
118+
119+ public int cyclomaticComplexity ()
120+ { int result = 0 ;
121+ for (int i = 0 ; i < elements .size (); i ++)
122+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
123+ result = result + ipe .cyclomaticComplexity ();
124+ }
125+ return result ;
126+ }
127+
128+ public Vector operationsUsedIn ()
129+ { Vector res = new Vector ();
130+ for (int i = 0 ; i < elements .size (); i ++)
131+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
132+ if (ipe .condition != null )
133+ { res .addAll (ipe .condition .allOperationsUsedIn ()); }
134+ }
135+ return res ;
136+ }
137+
138+ public Vector getUses (String data )
139+ { Vector res = new Vector ();
140+ for (int i = 0 ; i < elements .size (); i ++)
141+ { InPatternElement ipe = (InPatternElement ) elements .get (i );
142+ if (ipe .condition != null )
143+ { res .addAll (ipe .condition .getUses (data )); }
144+ }
145+ return res ;
146+ }
147+
148+ }
0 commit comments