001    package nl.cwi.sen1.visbase.rstorecontainer.datatypes;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import nl.cwi.sen1.visbase.rstorecontainer.RStoreContainer;
007    import aterm.ATermList;
008    import aterm.pure.PureFactory;
009    
010    /**
011     * This class represents the list of 'RStore Fact-identification data' being
012     * communicated over the ToolBus.
013     * 
014     * It can be used to automatically convert the ATerm(List) that is send over the
015     * ToolBus to the FactBrowser.
016     * 
017     * Example: <code>
018     * // The ATerm received by another Java tool<br />
019     * ATerm incomingAterm;<br />
020     * <br />
021     * // Use the constructor that accepts an ATermList as argument <br />
022     * FactInfoList factInfoList = new FactInfoList((ATermList) incomingAterm); <br />
023     * <br />
024     * //Iterate through FactInfo list <br />
025     * for (FactInfo factInfo : factInfoList.getFactInfos()) <br />
026     * { <br />
027     * &nbsp;&nbsp;// Access Fact information <br />
028     * &nbsp;&nbsp;factInfo.getId(); <br />
029     * &nbsp;&nbsp;factInfo.getName(); <br />
030     * &nbsp;&nbsp;factInfo.getType(); <br />
031     * }  <br />
032     * </code>
033     * 
034     * @see FactInfo
035     * 
036     * @author Ricardo Lindooren
037     * @author Arend van Beelen (reviewer)
038     * @date 2007-02-14
039     */
040    public class FactInfoList {
041        private List<FactInfo> m_factInfoList;
042    
043        /**
044         * Default constructor.
045         * 
046         * Creates an empty list.
047         * 
048         * @author Ricardo Lindooren
049         * @author Arend van Beelen (reviewer)
050         * @date 2007-02-14
051         */
052        public FactInfoList() {
053            super();
054    
055            m_factInfoList = new ArrayList<FactInfo>();
056        }
057    
058        /**
059         * Constructor that initializes the list from an ATermList.
060         * 
061         * @param termList ATermList containing fact data.
062         * 
063         * @throws ATermParseException if parsing of ATermList fails.
064         * @throws RuntimeException if ATermList input is null.
065         * 
066         * @author Ricardo Lindooren
067         * @author Arend van Beelen (reviewer)
068         * @date 2007-02-14
069         */
070        public FactInfoList(ATermList termList)
071                throws ATermParseException {
072            if (termList == null) {
073                throw new RuntimeException("ATermList input should not be null");
074            }
075    
076            m_factInfoList = new ArrayList<FactInfo>();
077    
078            try {
079                int numFactInfoObjects = termList.getLength();
080                for (int i = 0; i < numFactInfoObjects; i++) {
081    
082                    ATermList factData = (ATermList) termList.elementAt(i);
083            
084                    FactInfo factInfo = new FactInfo(factData);
085    
086                    m_factInfoList.add(factInfo);
087                }
088            } catch (Exception exception) {
089                throw new ATermParseException("Exception while parsing ATermList containing List of FactInfo (see cause) ",
090                        exception);
091            }
092        }
093    
094        /**
095         * Adds a FactInfo object to this list.
096         * 
097         * Another option is to use the list directly with getFactInfos().
098         * 
099         * @param rStoreFactData
100         * 
101         * @author Ricardo Lindooren
102         * @author Arend van Beelen (reviewer)
103         * @date 2007-02-14
104         */
105        public void addFactInfoToList(FactInfo rStoreFactData) {
106            if (rStoreFactData == null) {
107                throw new RuntimeException("RStoreFactData should not be null");
108            }
109    
110            m_factInfoList.add(rStoreFactData);
111        }
112    
113        /**
114         * Getter for the intern List instance
115         * 
116         * @return
117         * 
118         * @author Ricardo Lindooren
119         * @author Arend van Beelen (reviewer)
120         * @date 2007-02-14
121         */
122        public List<FactInfo> getFactInfos() {
123            return m_factInfoList;
124        }
125    
126        /**
127         * Creates a new ATermList containing the values in this datatype
128         * 
129         * @return new instance of ATermList
130         * 
131         * @author Ricardo Lindooren
132         * @author Arend van Beelen (reviewer)
133         * @date 2007-02-14
134         */
135        public ATermList toATermList() {
136            
137            PureFactory pureFactory = RStoreContainer.getPureFactory();
138            ATermList list = pureFactory.makeList();
139    
140            for (FactInfo factInfo : m_factInfoList) {        
141                ATermList factTermList = factInfo.toAtermList();
142                list = list.append(factTermList);
143            }
144            return list;
145        }
146    }