01: // This file is part of KeY - Integrated Deductive Software Design
02: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
03: // Universitaet Koblenz-Landau, Germany
04: // Chalmers University of Technology, Sweden
05: //
06: // The KeY system is protected by the GNU General Public License.
07: // See LICENSE.TXT for details.
08: //
09: //
10:
11: package de.uka.ilkd.key.logic.op.oclop;
12:
13: import de.uka.ilkd.key.logic.Name;
14: import de.uka.ilkd.key.logic.Term;
15: import de.uka.ilkd.key.logic.op.TermSymbol;
16: import de.uka.ilkd.key.logic.sort.Sort;
17: import de.uka.ilkd.key.logic.sort.oclsort.CollectionSort;
18: import de.uka.ilkd.key.logic.sort.oclsort.OclSort;
19:
20: /**
21: * Represents the OCL operations: at(), first(), last()
22: */
23: public class OclSequenceElems extends TermSymbol {
24:
25: private int arity;
26:
27: public OclSequenceElems(Name name, int arity) {
28: super (name, OclSort.OCLANY);
29: this .arity = arity;
30: }
31:
32: /** @return arity of the Function as int */
33: public int arity() {
34: return arity;
35: }
36:
37: /*
38: * checks if the given term is syntactically valid at its top
39: * level assumed the top level operator were this, i.e. if the
40: * direct subterms can be subterms of a term with this top level
41: * operator, the method returns true. Furthermore, it is checked
42: * that no variables are bound for none of the subterms.
43: * @param the Term to be checked.
44: * @return true iff the given term has
45: * subterms that are suitable for this function.
46: */
47: public boolean validTopLevel(Term term) {
48: if (term.arity() != arity()) {
49: return false;
50: }
51: if (!(term.sub(0).sort() instanceof CollectionSort)) {
52: return false;
53: }
54: CollectionSort collSort = (CollectionSort) term.sub(0).sort();
55: if (!(collSort.extendsTrans(OclSort.SEQUENCE_OF_OCLANY))) {
56: return false;
57: }
58: if (arity() == 2 && (term.sub(1).sort() != OclSort.INTEGER)) {
59: return false;
60: }
61: return true;
62: }
63:
64: public Sort sort(Term[] subTerm) {
65: if (subTerm.length != arity()) {
66: throw new IllegalArgumentException(
67: "Cannot determine sort of "
68: + "invalid term (Wrong arity).");
69: }
70: if (!(subTerm[0].sort() instanceof CollectionSort)) {
71: throw new IllegalArgumentException(
72: "Cannot determine sort of "
73: + "invalid term (First argument must be a Collection).");
74: }
75:
76: return ((CollectionSort) subTerm[0].sort()).getElemSort();
77: }
78:
79: public String toString() {
80: return (name() + ":" + OclSort.COLLECTION_OF_OCLANY);
81: }
82:
83: public String proofToString() {
84: String s = OclSort.COLLECTION_OF_OCLANY + " " + name();
85: s += "(" + OclSort.SEQUENCE_OF_OCLANY;
86: s += ");\n";
87: return s;
88: }
89: }
|