001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.logic.op.oclop;
012:
013: import de.uka.ilkd.key.logic.Name;
014: import de.uka.ilkd.key.logic.Term;
015: import de.uka.ilkd.key.logic.op.TermSymbol;
016: import de.uka.ilkd.key.logic.sort.Sort;
017: import de.uka.ilkd.key.logic.sort.oclsort.CollectionSort;
018: import de.uka.ilkd.key.logic.sort.oclsort.OclSort;
019:
020: /**
021: * Represents the OCL operations: append(), prepend()
022: */
023: public class OclSequenceOp extends TermSymbol {
024:
025: public OclSequenceOp(Name name) {
026: super (name, OclSort.SEQUENCE_OF_OCLANY);
027: }
028:
029: /** @return arity of the Function as int */
030: public int arity() {
031: return 2;
032: }
033:
034: /*
035: * checks if the given term is syntactically valid at its top
036: * level assumed the top level operator were this, i.e. if the
037: * direct subterms can be subterms of a term with this top level
038: * operator, the method returns true. Furthermore, it is checked
039: * that no variables are bound for none of the subterms.
040: * @param the Term to be checked.
041: * @return true iff the given term has
042: * subterms that are suitable for this function.
043: */
044: public boolean validTopLevel(Term term) {
045: if (term.arity() != arity()) {
046: return false;
047: }
048: if (!(term.sub(0).sort() instanceof CollectionSort)) {
049: return false;
050: }
051: CollectionSort collSort = (CollectionSort) term.sub(0).sort();
052: if (!collSort.extendsTrans(OclSort.SEQUENCE_OF_OCLANY)) {
053: return false;
054: }
055: OclSort elemSort = (OclSort) term.sub(1).sort();
056: if (!collSort.getElemSort().extendsTrans(elemSort)
057: && !elemSort.extendsTrans(collSort.getElemSort())) {
058: return false;
059: }
060: return true;
061: }
062:
063: public Sort sort(Term[] subTerm) {
064: if (subTerm.length != arity()) {
065: throw new IllegalArgumentException(
066: "Cannot determine sort of "
067: + "invalid term (Wrong arity).");
068: }
069: if (!(subTerm[0].sort() instanceof CollectionSort)) {
070: throw new IllegalArgumentException(
071: "Cannot determine sort of "
072: + "invalid term (First argument must be a Collection).");
073: }
074:
075: OclSort resultElemSort = null;
076: OclSort elemSort0 = ((CollectionSort) subTerm[0].sort())
077: .getElemSort();
078: OclSort elemSort1 = (OclSort) subTerm[1].sort();
079: if (elemSort0.extendsTrans(elemSort1)) {
080: resultElemSort = elemSort1;
081: } else if (elemSort1.extendsTrans(elemSort0)) {
082: resultElemSort = elemSort0;
083: } else {
084: throw new IllegalArgumentException(
085: "Cannot determine sort of "
086: + "invalid term (Arguments must have a common supersort).");
087: }
088:
089: return CollectionSort.getCollectionSort(
090: CollectionSort.SEQUENCE, resultElemSort);
091: }
092:
093: public String toString() {
094: return (name() + ":" + OclSort.SEQUENCE_OF_OCLANY);
095: }
096:
097: public String proofToString() {
098: String s = OclSort.SEQUENCE_OF_OCLANY + " " + name();
099: s += "(" + OclSort.SEQUENCE_OF_OCLANY + ", " + OclSort.OCLANY;
100: s += ");\n";
101: return s;
102: }
103: }
|