Package: PPatternAssistantInterpreter

PPatternAssistantInterpreter

nameinstructionbranchcomplexitylinemethod
PPatternAssistantInterpreter(IInterpreterAssistantFactory, String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
findIdentifiers(PPattern)
M: 5 C: 6
55%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
getAllNamedValues(PPattern, Value, Context)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getLength(PPattern)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
getNamedValues(PPattern, Value, Context)
M: 0 C: 65
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
isConstrained(PPattern)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
static {...}
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.interpreter.assistant.pattern;
2:
3: import java.util.List;
4: import java.util.Vector;
5:
6: import org.overture.ast.analysis.AnalysisException;
7: import org.overture.ast.assistant.IAstAssistant;
8: import org.overture.ast.intf.lex.ILexNameToken;
9: import org.overture.ast.patterns.AIdentifierPattern;
10: import org.overture.ast.patterns.PPattern;
11: import org.overture.interpreter.assistant.IInterpreterAssistantFactory;
12: import org.overture.interpreter.runtime.Context;
13: import org.overture.interpreter.utilities.pattern.AllNamedValuesLocator;
14: import org.overture.interpreter.values.NameValuePairList;
15: import org.overture.interpreter.values.Value;
16: import org.overture.typechecker.assistant.pattern.PPatternAssistantTC;
17:
18: public class PPatternAssistantInterpreter extends PPatternAssistantTC implements IAstAssistant
19: {
20:         protected static IInterpreterAssistantFactory af;
21:
22:         @SuppressWarnings("static-access")
23:         public PPatternAssistantInterpreter(IInterpreterAssistantFactory af, String fromModule)
24:         {
25:                 super(af, fromModule);
26:                 this.af = af;
27:         }
28:
29:         /** A value for getLength meaning "any length" */
30:         public static int ANY = -1;
31:
32:         public NameValuePairList getNamedValues(PPattern p, Value expval,
33:                         Context ctxt) throws AnalysisException
34:         {
35:                 List<AIdentifierPattern> ids = af.createPPatternAssistant(fromModule).findIdentifiers(p);
36:
37:                 // Go through the list of IDs, marking duplicate names as constrained. This is
38:                 // because we have to permute sets that contain duplicate variables, so that
39:                 // we catch permutations that match constrained values of the variable from
40:                 // elsewhere in the pattern.
41:
42:                 int count = ids.size();
43:
44:•                for (int i = 0; i < count; i++)
45:                 {
46:                         ILexNameToken iname = ids.get(i).getName();
47:
48:•                        for (int j = i + 1; j < count; j++)
49:                         {
50:•                                if (iname.equals(ids.get(j).getName()))
51:                                 {
52:                                         ids.get(i).setConstrained(true);
53:                                         ids.get(j).setConstrained(true);
54:                                 }
55:                         }
56:                 }
57:
58:                 List<NameValuePairList> all = getAllNamedValues(p, expval, ctxt);
59:                 return all.get(0); // loose choice here!
60:         }
61:
62:         public List<AIdentifierPattern> findIdentifiers(PPattern pattern)
63:         {
64:                 try
65:                 {
66:                         return pattern.apply(af.getIdentifierPatternFinder());// FIXME: should we handle exceptions like this
67:                 } catch (AnalysisException e)
68:                 {
69:                         return new Vector<AIdentifierPattern>(); // Most have none
70:                 }
71:
72:         }
73:
74:         public List<NameValuePairList> getAllNamedValues(PPattern pattern,
75:                         Value expval, Context ctxt) throws AnalysisException
76:         {
77:
78:                 return pattern.apply(af.getAllNamedValuesLocator(fromModule), new AllNamedValuesLocator.Newquestion(expval, ctxt));
79:         }
80:
81:         /**
82:          * @param pattern
83:          * @return The "length" of the pattern (eg. sequence and set patterns).
84:          */
85:
86:         public int getLength(PPattern pattern)
87:         {
88:                 try
89:                 {
90:                         return pattern.apply(af.getLengthFinder());// FIXME: should we handle exceptions like this
91:                 } catch (AnalysisException e)
92:                 {
93:                         return 1; // Most only identify one member
94:                 }
95:         }
96:
97:         /**
98:          * @param pattern
99:          * @return True if the pattern has constraints, such that matching values should be permuted, where necessary, to
100:          * find a match.
101:          */
102:         public boolean isConstrained(PPattern pattern)
103:         {
104:                 try
105:                 {
106:                         return pattern.apply(af.getConstrainedPatternChecker());// FIXME: should we handle exceptions like this
107:                 } catch (AnalysisException e)
108:                 {
109:                         return true;
110:                 }
111:
112:         }
113: }