Package: POFunctionDefinitionContext

POFunctionDefinitionContext

nameinstructionbranchcomplexitylinemethod
POFunctionDefinitionContext(AExplicitFunctionDefinition, boolean)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
POFunctionDefinitionContext(AImplicitFunctionDefinition, boolean, IPogAssistantFactory)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
calculateTypes(AFunctionType, boolean)
M: 0 C: 32
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getContext()
M: 102 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
getContextNode(PExp)
M: 0 C: 46
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
handleCurries(PType)
M: 0 C: 36
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
makeBinds()
M: 0 C: 61
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 14
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2008 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.pog.contexts;
25:
26: import java.util.Collection;
27: import java.util.Iterator;
28: import java.util.LinkedList;
29: import java.util.List;
30: import java.util.Vector;
31:
32: import org.overture.ast.definitions.AExplicitFunctionDefinition;
33: import org.overture.ast.definitions.AImplicitFunctionDefinition;
34: import org.overture.ast.expressions.AForAllExp;
35: import org.overture.ast.expressions.AImpliesBooleanBinaryExp;
36: import org.overture.ast.expressions.PExp;
37: import org.overture.ast.factory.AstExpressionFactory;
38: import org.overture.ast.intf.lex.ILexNameToken;
39: import org.overture.ast.patterns.AIgnorePattern;
40: import org.overture.ast.patterns.ATypeMultipleBind;
41: import org.overture.ast.patterns.PMultipleBind;
42: import org.overture.ast.patterns.PPattern;
43: import org.overture.ast.types.ABooleanBasicType;
44: import org.overture.ast.types.AFunctionType;
45: import org.overture.ast.types.PType;
46: import org.overture.pog.pub.IPogAssistantFactory;
47:
48: public class POFunctionDefinitionContext extends POContext {
49:         public final ILexNameToken name;
50:         public final AFunctionType deftype;
51:         public final List<List<PPattern>> paramPatternList;
52:         public final List<PType> argtypes;
53:         public final boolean addPrecond;
54:         public final PExp precondition;
55:
56:         public POFunctionDefinitionContext(AExplicitFunctionDefinition definition,
57:                         boolean precond) {
58:                 this.name = definition.getName();
59:                 this.deftype = (AFunctionType) definition.getType();
60:                 this.paramPatternList = definition.getParamPatternList();
61:                 this.addPrecond = precond;
62:                 this.precondition = definition.getPrecondition();
63:                 this.argtypes = calculateTypes(deftype,definition.getIsCurried());
64:         }
65:
66:         private List<PType> calculateTypes(AFunctionType ftype, boolean curried) {
67:                 List<PType> r = new LinkedList<PType>();
68:
69:•                for (PType t : ftype.getParameters()) {
70:                         r.add(t.clone());
71:                 }
72:•                if (curried) {
73:                         r.addAll(handleCurries(ftype.getResult()));
74:                 }
75:                 return r;
76:         }
77:
78:         private Collection<? extends PType> handleCurries(PType result) {
79:                 List<PType> r = new LinkedList<PType>();
80:
81:•                if (result instanceof AFunctionType) {
82:                         AFunctionType ft = (AFunctionType) result;
83:•                        for (PType p : ft.getParameters()) {
84:                                 r.add(p.clone());
85:                         }
86:                         r.addAll(handleCurries(ft.getResult()));
87:                 }
88:
89:                 return r;
90:         }
91:
92:         public POFunctionDefinitionContext(AImplicitFunctionDefinition definition,
93:                         boolean precond, IPogAssistantFactory assistantFactory) {
94:                 this.name = definition.getName();
95:                 this.deftype = (AFunctionType) definition.getType();
96:                 this.addPrecond = precond;
97:                 this.paramPatternList = assistantFactory
98:                                 .createAImplicitFunctionDefinitionAssistant()
99:                                 .getParamPatternList(definition);
100:                 this.precondition = definition.getPrecondition();
101:                 this.argtypes = calculateTypes(deftype, false);
102:         }
103:
104:         @Override
105:         public PExp getContextNode(PExp stitch) {
106:                 AForAllExp forAllExp = new AForAllExp();
107:                 forAllExp.setBindList(makeBinds());
108:                 forAllExp.setType(new ABooleanBasicType());
109:•                if (deftype.getParameters().isEmpty()) {
110:                         return stitch;
111:                 }
112:
113:•                if (addPrecond && precondition != null) {
114:
115:                         AImpliesBooleanBinaryExp implies = AstExpressionFactory
116:                                         .newAImpliesBooleanBinaryExp(precondition.clone(), stitch);
117:                         implies.setType(new ABooleanBasicType());
118:                         
119:                         forAllExp.setPredicate(implies);
120:                 } else {
121:                         forAllExp.setPredicate(stitch);
122:                 }
123:
124:                 return forAllExp;
125:         }
126:
127:
128:
129:         private List<PMultipleBind> makeBinds() {
130:                 List<PMultipleBind> result = new LinkedList<PMultipleBind>();
131:
132:                 Iterator<PType> types = argtypes.iterator();
133:
134:•                for (List<PPattern> params : paramPatternList) {
135:
136:•                        for (PPattern param : params) {
137:                                 ATypeMultipleBind typeBind = new ATypeMultipleBind();
138:                                 List<PPattern> one = new Vector<PPattern>();
139:                                 one.add(param.clone());
140:                                 typeBind.setPlist(one);
141:                                 PType type = types.next();
142:                                 typeBind.setType(type.clone());
143:                                 result.add(typeBind);
144:                         }
145:                 }
146:
147:                 return result;
148:         }
149:
150:         @Override
151:         public String getContext() {
152:                 StringBuilder sb = new StringBuilder();
153:
154:•                if (!deftype.getParameters().isEmpty()) {
155:                         sb.append("forall ");
156:                         String sep = "";
157:                         AFunctionType ftype = deftype;
158:
159:•                        for (List<PPattern> pl : paramPatternList) {
160:                                 Iterator<PType> types = ftype.getParameters().iterator();
161:
162:•                                for (PPattern p : pl) {
163:•                                        if (!(p instanceof AIgnorePattern)) {
164:                                                 sb.append(sep);
165:                                                 sb.append(p.toString());
166:                                                 sb.append(":");
167:                                                 sb.append(types.next());
168:                                                 sep = ", ";
169:                                         }
170:                                 }
171:
172:•                                if (ftype.getResult() instanceof AFunctionType) {
173:                                         ftype = (AFunctionType) ftype.getResult();
174:                                 } else {
175:                                         break;
176:                                 }
177:                         }
178:
179:                         sb.append(" &");
180:
181:•                        if (addPrecond && precondition != null) {
182:                                 sb.append(" ");
183:                                 sb.append(precondition);
184:                                 sb.append(" =>");
185:                         }
186:                 }
187:
188:                 return sb.toString();
189:         }
190: }