001    /*
002     * The contents of this file are subject to the Mozilla Public License
003     * Version 1.1 (the "License");  you may not use this file except in 
004     * compliance with the License.  You may obtain a copy of the License at
005     * http://www.mozilla.org/MPL/
006     *
007     * Software distributed under the License is distributed on an "AS IS" basis,
008     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009     * the specific language governing rights and limitations under the License.
010     *
011     * The Original Code is Protege-2000.
012     *
013     * The Initial Developer of the Original Code is Stanford University. Portions
014     * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
015     *
016     * Protege was developed by Stanford Medical Informatics
017     * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
018     * with support from the National Library of Medicine, the National Science
019     * Foundation, and the Defense Advanced Research Projects Agency.  Current
020     * information about Protege can be obtained at http://protege.stanford.edu.
021     *
022     */
023    
024    package edu.stanford.smi.protegex.owl.model.impl;
025    
026    import edu.stanford.smi.protege.model.FrameID;
027    import edu.stanford.smi.protege.model.KnowledgeBase;
028    import edu.stanford.smi.protegex.owl.model.*;
029    
030    import java.util.*;
031    
032    /**
033     * The base class of DefaultOWLIntersectionClass and DefaultOWLUnionClass.
034     *
035     * @author Holger Knublauch  <holger@knublauch.com>
036     */
037    public abstract class AbstractOWLNAryLogicalClass extends AbstractOWLLogicalClass
038            implements OWLNAryLogicalClass {
039    
040    
041        protected AbstractOWLNAryLogicalClass(KnowledgeBase kb, FrameID id) {
042            super(kb, id);
043        }
044    
045    
046        protected AbstractOWLNAryLogicalClass() {
047        }
048    
049    
050        public void addOperand(RDFSClass operand) {
051            RDFList list = (RDFList) getPropertyValue(getOperandsProperty());
052            if (list == null || getOWLModel().getRDFNil().equals(list)) {
053                list = getOWLModel().createRDFList();
054                setOwnSlotValue(getOperandsProperty(), list);
055            }
056            list.append(operand);
057        }
058    
059    
060        public boolean equalsStructurally(RDFObject object) {
061            if (object instanceof AbstractOWLNAryLogicalClass) {
062                AbstractOWLNAryLogicalClass compCls = (AbstractOWLNAryLogicalClass) object;
063                if (getOperatorSymbol() == compCls.getOperatorSymbol()) {
064                    return OWLUtil.equalsStructurally(getOperands(), compCls.getOperands());
065                }
066            }
067            return false;
068        }
069    
070        /*public String getBrowserText() {
071           final Collection operands = getOperands();
072           char operator = getOperatorSymbol();
073           if (operands.size() == 0) {
074               return "<empty " + getClass().getName() + ">";
075           }
076           String text = "";
077           for (Iterator it = operands.iterator(); it.hasNext();) {
078               Cls cls = (Cls) it.next();
079               String clsText = cls instanceof RDFSClass ?
080                       ((RDFSClass) cls).getNestedBrowserText() :
081                       cls.getBrowserText();
082               text += clsText;
083               if (it.hasNext()) {
084                   text += " " + operator + " ";
085               }
086           }
087           return text;
088       } */
089    
090    
091        public Collection<RDFSNamedClass> getNamedOperands() {
092            Collection<RDFSNamedClass> result = new HashSet<RDFSNamedClass>();
093            for (RDFSClass operand : getOperands()) {
094                if (operand instanceof RDFSNamedClass) {
095                    result.add((RDFSNamedClass) operand);
096                }
097            }
098            return result;
099        }
100    
101    
102        public String getNestedBrowserText() {
103            return "(" + getBrowserText() + ")";
104        }
105    
106    
107        @SuppressWarnings("unchecked")
108        public Collection<RDFSClass> getOperands() {
109            RDFList list = (RDFList) getPropertyValue(getOperandsProperty());
110            if (list == null) {
111                return Collections.emptyList();
112            }
113            else {
114                return list.getValues();
115            }
116        }
117    
118    
119        public boolean hasOperandWithBrowserText(String browserText) {
120            for (Iterator it = getOperands().iterator(); it.hasNext();) {
121                RDFSClass operand = (RDFSClass) it.next();
122                if (browserText.equals(operand.getBrowserText())) {
123                    return true;
124                }
125            }
126            return false;
127        }
128    
129    
130        public boolean hasSameOperands(OWLNAryLogicalClass other) {
131            Set setA = new HashSet();
132            for (Iterator it = getOperands().iterator(); it.hasNext();) {
133                RDFSClass operand = (RDFSClass) it.next();
134                setA.add(operand.getBrowserText());
135            }
136            Set setB = new HashSet();
137            for (Iterator it = other.getOperands().iterator(); it.hasNext();) {
138                RDFSClass operand = (RDFSClass) it.next();
139                setB.add(operand.getBrowserText());
140            }
141            if (setA.size() == setB.size()) {
142                setA.removeAll(setB);
143                return setA.isEmpty();
144            }
145            else {
146                return false;
147            }
148        }
149    
150    
151        public Iterator listOperands() {
152            return getOperands().iterator();
153        }
154    
155    
156        public void removeOperand(RDFSClass operand) {
157            DefaultRDFList.removeListValue(this, getOperandsProperty(), operand);
158        }
159    }
160