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: including(), excluding()
022: */
023: public class OclSetOp extends TermSymbol {
024:
025: public OclSetOp(Name name) {
026: super (name, OclSort.COLLECTION_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: OclSort elemSort = (OclSort) term.sub(1).sort();
053: if (!collSort.getElemSort().extendsTrans(elemSort)
054: && !elemSort.extendsTrans(collSort.getElemSort())) {
055: return false;
056: }
057: return true;
058: }
059:
060: public Sort sort(Term[] subTerm) {
061: if (subTerm.length != arity()) {
062: throw new IllegalArgumentException(
063: "Cannot determine sort of "
064: + "invalid term (Wrong arity).");
065: }
066: if (!(subTerm[0].sort() instanceof CollectionSort)) {
067: throw new IllegalArgumentException(
068: "Cannot determine sort of "
069: + "invalid term (First argument must be a Collection).");
070: }
071:
072: int resultCollKind = ((CollectionSort) subTerm[0].sort())
073: .getCollectionKind();
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(resultCollKind,
090: resultElemSort);
091: }
092:
093: public String toString() {
094: return (name() + ":" + OclSort.COLLECTION_OF_OCLANY);
095: }
096:
097: public String proofToString() {
098: String s = OclSort.COLLECTION_OF_OCLANY + " " + name();
099: s += "(" + OclSort.COLLECTION_OF_OCLANY + ", " + OclSort.OCLANY;
100: s += ");\n";
101: return s;
102: }
103: }
|