001    package nl.cwi.sen1.visbase.rstorecontainer.datatypes;
002    
003    import nl.cwi.sen1.relationstores.Factory;
004    import nl.cwi.sen1.relationstores.types.RTuple;
005    import nl.cwi.sen1.relationstores.types.RType;
006    import nl.cwi.sen1.visbase.rstorecontainer.RStoreContainer;
007    import aterm.ATermAppl;
008    import aterm.ATermInt;
009    import aterm.ATermList;
010    import aterm.pure.ATermApplImpl;
011    import aterm.pure.PureFactory;
012    
013    /**
014     * This class represents the 'RStore Fact-identification data' being
015     * communicated over the ToolBus
016     *
017     * @see FactInfoList
018     *
019     * @author Ricardo Lindooren
020     * @author Arend van Beelen (reviewer)
021     * @date 2007-02-14
022     */
023    public class FactInfo {
024    
025        private int m_id;
026        private String m_name;
027        private RType m_rType;
028    
029        /**
030         * Contructor that can be used to retrieve RStore FactInfo data from an
031         * ATermList representing this data.
032         *
033         * @param aTermList
034         *            the ATermList containing the FactInfo Data (Format:
035         *            (int,str,term)
036         *            <code>[1,"SIMPLE_GRAPH",relation([str,str])]</code>)
037         *
038         * @throws ATermParseException if parsing of ATermList fails.
039         * @throws RuntimeException if ATermList input is null.
040         *
041         * @author Ricardo Lindooren
042         * @author Arend van Beelen (reviewer)
043         * @date 2007-02-14
044         */
045        public FactInfo(final ATermList aTermList)
046                throws ATermParseException {
047            if (aTermList == null) {
048                throw new RuntimeException("ATermList input should not be null");
049            }
050    
051            try {
052                PureFactory pureFactory = (PureFactory) aTermList.getFactory();
053                Factory factory = Factory.getInstance(pureFactory);
054    
055                m_id = ((ATermInt) aTermList.elementAt(0)).getInt();
056                m_name = ((ATermAppl) aTermList.elementAt(1)).getName();
057                m_rType = factory.RTypeFromTerm(aTermList.elementAt(2));
058    
059            } catch (Exception exception) {
060                throw new ATermParseException(
061                        "Exception while parsing ATermList containing FactData (see cause) ",
062                        exception);
063            }
064        }
065    
066        /**
067         * Contructor that can be used to retrieve RStore Fact data from an RTuple
068         * representing this data.
069         *
070         * @param factId ID of the fact. This ID is used to identify facts
071         *               between the visualisation tools.
072         * @param factRTuple RTuple containing the fact data.
073         *
074         * @author Ricardo Lindooren
075         * @author Arend van Beelen (reviewer)
076         * @date 2007-02-14
077         */
078        public FactInfo(final int factId, final RTuple factRTuple) {
079            if (factRTuple == null) {
080                throw new RuntimeException("RTuple input should not be null");
081            }
082    
083            m_id = factId;
084            m_name = ((ATermApplImpl) factRTuple.getVariable().getArgument(0)).getName();
085            m_rType = factRTuple.getRtype();
086        }
087    
088        /**
089         * Returns the numeric ID of the fact.
090         *
091         * @return The ID.
092         *
093         * @author Ricardo Lindooren
094         * @author Arend van Beelen (reviewer)
095         * @date 2007-02-14
096         */
097        public int getId() {
098            return m_id;
099        }
100    
101        /**
102         * Gets the name of the fact.
103         *
104         * @return Name of the fact.
105         *
106         * @author Ricardo Lindooren
107         * @author Arend van Beelen (reviewer)
108         * @date 2007-02-14
109         */
110        public String getName() {
111            return m_name;
112        }
113    
114        /**
115         * Gets the relation type of the fact.
116         *
117         * @return A string representing the relation type, like
118         *         "relation([str,str])".
119         *
120         * @author Ricardo Lindooren
121         * @author Arend van Beelen (reviewer)
122         * @date 2007-02-14
123         */
124        public String getType() {
125            if(m_rType == null) return "";
126            
127            // TODO is it wise to use the toString() method
128            return m_rType.toString();
129        }
130    
131        /**
132         * Gets the RType instance that represents the relation type of the fact.
133         *
134         * @return The relation type of the fact.
135         *
136         * @author Ricardo Lindooren
137         * @author Arend van Beelen (reviewer)
138         * @date 2007-02-21
139         */
140        public RType getRType() {
141            return m_rType;
142        }
143    
144        /**
145         * Creates a new ATermList containing the values in this datatype.
146         *
147         * @return A new ATermList instance in the format (int,str,term),
148         *         like @c [1,"SIMPLE_GRAPH",relation([str,str])].
149         *
150         * @author Ricardo Lindooren
151         * @author Arend van Beelen (reviewer)
152         * @date 2007-02-14
153         */
154        public ATermList toAtermList() {
155    
156            PureFactory pureFactory = RStoreContainer.getPureFactory();
157    
158            ATermList list = pureFactory.makeList();
159    
160            list = list.insertAt(pureFactory.makeInt(getId()), 0);
161            list = list.insertAt(pureFactory.make("<str>", getName()), 1);
162            list = list.insertAt(getRType().toTerm(), 2);
163    
164            return list;
165        }
166    }