001    package nl.tudelft.tbm.eeni.owl2java.model.jmodel;
002    
003    import nl.tudelft.tbm.eeni.owl2java.model.jmodel.utils.LogUtils;
004    import nl.tudelft.tbm.eeni.owl2java.utils.IReporting;
005    import nl.tudelft.tbm.eeni.owl2java.utils.StringUtils;
006    import org.apache.commons.logging.Log;
007    import org.apache.commons.logging.LogFactory;
008    
009    import java.util.ArrayList;
010    import java.util.List;
011    
012    
013    public class JRestrictionsContainer implements IReporting {
014    
015    //      Restrictions
016    //      - Multiple allValues restrictions are handled independently as multiple
017    // restrictions of type JAllValuesRestriction 
018    
019    
020        private static Log log = LogFactory.getLog(JRestrictionsContainer.class);
021    
022        private JClass onClass;
023        private JProperty onProperty;
024    
025        private JCardinalityRestriction cardinalityRestriction;
026        private List<JAllValuesRestriction> allValuesRestrictions = new ArrayList<JAllValuesRestriction>();
027        private JOtherRestriction otherRestriction;
028    
029        public JRestrictionsContainer(JClass onClass, JProperty onProperty) {
030            this.onClass = onClass;
031            this.onProperty = onProperty;
032            cardinalityRestriction = new JCardinalityRestriction(onClass, onProperty);
033            otherRestriction = new JOtherRestriction(onClass, onProperty);
034    
035            onClass.addDomainRestrictionsContainer(onProperty, this);
036            onProperty.addRestrictionsContainer(onClass, this);
037        }
038    
039        public boolean hasCardinalityRestriction() {
040            if (cardinalityRestriction == null)
041                return false;
042            return true;
043        }
044    
045        public boolean hasOtherRestriction() {
046            if (otherRestriction == null)
047                return false;
048            return true;
049        }
050    
051        public JOtherRestriction getOtherRestriction() {
052            return otherRestriction;
053        }
054    
055        public List<JAllValuesRestriction> listAllValuesRestrictions() {
056            return allValuesRestrictions;
057        }
058    
059        public void aggregateRestrictions(List<JClass> parentClasses) {
060            for (JClass cls : parentClasses) {
061                log.debug(LogUtils.toLogName(onClass, onProperty)
062                        + ": Aggregating restrictions of parent class " + LogUtils.toLogName(cls));
063                JRestrictionsContainer restrictions = cls.getAggregatedRestrictionsContainer(onProperty);
064                // continue if we have no restrictions for this property on the parent class
065                if (restrictions == null)
066                    continue;
067                // cardinality
068                cardinalityRestriction.mergeParent(restrictions.getCardinalityRestriction());
069                // otherRestriction
070                otherRestriction.mergeParent(restrictions.getOtherRestriction());
071                // allValues
072                for (JAllValuesRestriction restriction : restrictions.listAllValuesRestrictions()) {
073                    if (!allValuesRestrictions.contains(restriction))
074                        allValuesRestrictions.add(restriction.clone());
075                }
076            }
077        }
078    
079        public JCardinalityRestriction getCardinalityRestriction() {
080            return cardinalityRestriction;
081        }
082    
083        public JRestrictionsContainer clone() {
084            JRestrictionsContainer rc = new JRestrictionsContainer(onClass, onProperty);
085            rc.cardinalityRestriction = cardinalityRestriction.clone();
086            rc.otherRestriction = otherRestriction.clone();
087            for (JAllValuesRestriction avr : allValuesRestrictions) {
088                JAllValuesRestriction r = avr.clone();
089                rc.allValuesRestrictions.add(r);
090            }
091            return rc;
092        }
093    
094        public String getJModelReport() {
095            String report = LogUtils.toLogName(onClass, onProperty) + " Restriction Container:\n";
096            report += StringUtils.indentText(cardinalityRestriction.getJModelReport() + "\n", 1);
097            report += StringUtils.indentText(otherRestriction.getJModelReport(), 1);
098            if (!allValuesRestrictions.isEmpty())
099                report += "\n";
100            for (JAllValuesRestriction r : allValuesRestrictions) {
101                report += StringUtils.indentText(r.getJModelReport() + "\n", 1);
102            }
103            return report;
104        }
105    
106        public void addAllValuesRestriction(JClass allValuesJClass) {
107            JAllValuesRestriction avr = new JAllValuesRestriction(onClass, onProperty);
108            avr.setAllValues(allValuesJClass);
109            allValuesRestrictions.add(avr);
110        }
111    
112    }