01: package de.uka.ilkd.key.ocl.gf;
02:
03: import java.util.logging.Level;
04: import java.util.logging.Logger;
05:
06: /**
07: * asks GF if the given commands leads to a situation, where
08: * something could be filled in automatically.
09: * This class is meant for self and result.
10: * @author daniels
11: */
12: class SelfResultProber extends AbstractProber {
13: /**
14: * This field is true in the beginning of each run, and
15: * set to false, if the focus position when checking is found
16: * to be open.
17: */
18: protected boolean autocompleted = true;
19:
20: protected static Logger nogger = Logger
21: .getLogger(SelfResultProber.class.getName());
22:
23: /**
24: * A constructor which sets some fields
25: * @param gfCapsule The encapsulation of the running GF process
26: */
27: public SelfResultProber(GfCapsule gfCapsule) {
28: super (gfCapsule);
29: }
30:
31: /**
32: * asks GF if the given commands leads to a situation, where
33: * something could be filled in automatically.
34: * This function is meant for self and result.
35: * IMPORTANT: Must be called <b>after</b> </gfedit>
36: * when no other method reads sth. from GF.
37: * It uses the same GF as everything else, since it tests if
38: * sth. is possible there.
39: * @param gfCommand the command to be tested.
40: * One has to chain a mp command to make GF go to the right place afterwards
41: * @param chainCount The number of chained commands in gfCommand.
42: * So many undos are done to clean up afterwards.
43: * @return true iff sth. could be filled in automatically
44: */
45: public boolean isAutoCompletable(String gfCommand, int chainCount) {
46: this .autocompleted = true;
47: send(gfCommand);
48: readGfedit();
49: final boolean result = this .autocompleted;
50: this .autocompleted = true;
51: //clean up and undo
52: send("u " + chainCount);
53: readAndIgnore();
54: if (nogger.isLoggable(Level.FINE)) {
55: nogger.fine(result + " is the result for: '" + gfCommand
56: + "'");
57: }
58: return result;
59: }
60:
61: /**
62: * Reads the tree child of the XML from beginning to end.
63: * Sets autocompleted to false, if the focus position is open.
64: */
65: protected void readTree() {
66: String treeString = gfCapsule.readTree();
67: String[] treeArray = treeString.split("\\n");
68: for (int i = 0; i < treeArray.length; i++) {
69: String result = treeArray[i].trim();
70: if (result.startsWith("*")) {
71: result = result.substring(1).trim();
72: if (result.startsWith("?")) {
73: //the normal case, focus position open
74: this .autocompleted = false;
75: } else if ((i >= 6) //that we could be at the instance argument at all
76: && (treeArray[i - 6].indexOf("coerce") > -1) //we are below a coerce
77: && (treeArray[i - 3].trim().startsWith("?")) //the Subtype argument is not filled in
78: // The super argument cannot be OclAnyC or even unrefined, because then this
79: // method wouldn't have been called. Thus, the Subtype argument is unique.
80: ) {
81: //we are below a coerce, but self would have a non-suiting subtype
82: this .autocompleted = false;
83: }
84: }
85:
86: }
87: }
88: }
|