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 operation: intersection()
022: */
023: public class OclIntersection extends TermSymbol {
024:
025: public OclIntersection() {
026: super (new Name("$intersection"), 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: || !(term.sub(1).sort() instanceof CollectionSort)) {
050: return false;
051: }
052: CollectionSort collSort0 = (CollectionSort) term.sub(0).sort();
053: CollectionSort collSort1 = (CollectionSort) term.sub(1).sort();
054: if (collSort0.getCollectionKind() == CollectionSort.SEQUENCE
055: || collSort1.getCollectionKind() == CollectionSort.SEQUENCE) {
056: return false;
057: }
058: if (!collSort0.getElemSort().extendsTrans(
059: collSort1.getElemSort())
060: && !collSort1.getElemSort().extendsTrans(
061: collSort0.getElemSort())) {
062: return false;
063: }
064: return true;
065: }
066:
067: public Sort sort(Term[] subTerm) {
068: if (subTerm.length != arity()) {
069: throw new IllegalArgumentException(
070: "Cannot determine sort of "
071: + "invalid term (Wrong arity).");
072: }
073: if (!(subTerm[0].sort() instanceof CollectionSort)
074: || !(subTerm[1].sort() instanceof CollectionSort)) {
075: throw new IllegalArgumentException(
076: "Cannot determine sort of "
077: + "invalid term (Both args must be Collections).");
078: }
079: int resultCollKind = -1;
080: int collKind0 = ((CollectionSort) subTerm[0].sort())
081: .getCollectionKind();
082: int collKind1 = ((CollectionSort) subTerm[1].sort())
083: .getCollectionKind();
084: if (collKind0 == CollectionSort.BAG
085: && collKind1 == CollectionSort.BAG) {
086: resultCollKind = CollectionSort.BAG;
087: } else if (collKind0 == CollectionSort.SET
088: || collKind1 == CollectionSort.SET) {
089: resultCollKind = CollectionSort.SET;
090: }
091:
092: OclSort resultElemSort = null;
093: OclSort elemSort0 = ((CollectionSort) subTerm[0].sort())
094: .getElemSort();
095: OclSort elemSort1 = ((CollectionSort) subTerm[1].sort())
096: .getElemSort();
097: if (elemSort0.extendsTrans(elemSort1)) {
098: resultElemSort = elemSort1;
099: } else {
100: resultElemSort = elemSort0;
101: }
102:
103: return CollectionSort.getCollectionSort(resultCollKind,
104: resultElemSort);
105: }
106:
107: public String toString() {
108: return (name() + ":" + OclSort.OCLGENERIC);
109: }
110:
111: public String proofToString() {
112: String s = OclSort.OCLGENERIC + " " + name();
113: s += "(" + OclSort.BOOLEAN + "," + OclSort.OCLGENERIC + ","
114: + OclSort.OCLGENERIC;
115: s += ");\n";
116: return s;
117: }
118: }
|