1// BEGIN LICENSE BLOCK
2// Version: CMPL 1.1
3//
4// The contents of this file are subject to the Cisco-style Mozilla Public
5// License Version 1.1 (the "License"); you may not use this file except
6// in compliance with the License.  You may obtain a copy of the License
7// at www.eclipse-clp.org/license.
8//
9// Software distributed under the License is distributed on an "AS IS"
10// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11// the License for the specific language governing rights and limitations
12// under the License.
13//
14// The Original Code is  The ECLiPSe Constraint Logic Programming System.
15// The Initial Developer of the Original Code is  Cisco Systems, Inc.
16// Portions created by the Initial Developer are
17// Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18//
19// Contributor(s):
20//
21// END LICENSE BLOCK
22
23package com.parctechnologies.eclipse.visualisation;
24import com.parctechnologies.eclipse.*;
25import java.util.*;
26import java.io.Serializable;
27
28/**
29 * Class to represent the type of a viewable.
30 */
31public class ViewableType implements Serializable
32{
33  static List atomListToStringList(Collection atomList) {
34    // copy the Atoms into Strings
35    List stringList = new ArrayList(atomList.size());
36    for(Iterator it = atomList.iterator(); it.hasNext(); ) {
37      Atom fix = (Atom)it.next();
38      stringList.add(fix.functor());
39    }
40    return stringList;
41  }
42
43  /**
44   * Given a CompoundTerm, try to create a ViewableType object from it, and
45   * throw a VisException otherwise.
46   */
47  static ViewableType parseFromCompoundTerm(CompoundTerm term)
48    throws VisException
49  {
50    if(term.functor().equals("array") &&
51       term.arity() == 2)
52    {
53      // copy the Atoms into Strings
54//        Collection fixityListAtoms = (Collection) term.arg(1);
55//        Collection fixityListStrings = new ArrayList(fixityListAtoms.size());
56//        for(Iterator it = fixityListAtoms.iterator(); it.hasNext(); ) {
57//            Atom fix = (Atom)it.next();
58//            fixityListStrings.add(fix.functor());
59//        }
60
61      Collection fixityListStrings =
62        atomListToStringList((Collection) term.arg(1));
63     CompoundTerm elementType = (CompoundTerm) term.arg(2);
64
65      return(
66        new ArrayType(
67          new LinkedList(fixityListStrings),
68          ElementType.parseFromCompoundTerm(elementType)));
69    }
70    else if ("multi_array".equals(term.functor()) &&
71             term.arity() == 3) {
72      Collection fixityListList = (Collection) term.arg(1);
73      Collection elementTypeList = (Collection) term.arg(2);
74      Collection subArrayNameList = (Collection) term.arg(3);
75
76      Iterator fixIt = fixityListList.iterator();
77      Iterator elemIt = elementTypeList.iterator();
78
79      List subViewableTypes = new ArrayList(fixityListList.size());
80
81      while(fixIt.hasNext()) {
82        Collection fixityListStrings =
83          atomListToStringList((Collection) fixIt.next());
84        CompoundTerm elementType = (CompoundTerm) elemIt.next();
85        ViewableType subType =
86          new ArrayType(new LinkedList(fixityListStrings),
87                                ElementType.parseFromCompoundTerm(elementType));
88        subViewableTypes.add(subType);
89      }
90      return new MultiArrayType(subViewableTypes,
91                                atomListToStringList(subArrayNameList));
92    }
93    else
94    {
95      throw(
96        new VisException("Could not parse viewable type from term "+
97                         term));
98    }
99
100  }
101
102
103
104
105  public static class ArrayType extends ViewableType {
106    private ElementType elementType;
107    private List fixityList;
108
109    ArrayType(List fixityList, ElementType elementType)
110    {
111      setElementType(elementType);
112      setFixityList(fixityList);
113    }
114
115    protected void setElementType(ElementType elementType)
116    {
117      this.elementType = elementType;
118    }
119
120    protected void setFixityList(List fixityList)
121    {
122      this.fixityList = fixityList;
123    }
124
125    public int getNDimensions()
126    {
127      return(fixityList.size());
128    }
129
130    public List getFixityList()
131    {
132      return(fixityList);
133    }
134
135    public ElementType getElementType()
136    {
137      return(elementType);
138    }
139  }
140
141  public static class MultiArrayType extends ViewableType {
142    List subTypes;
143    List subNames;
144
145    MultiArrayType(List subTypes, List subNames) {
146      this.subTypes = subTypes;
147      this.subNames = subNames;
148    }
149
150    public ArrayType getSubType(int i) {
151      return (ArrayType) (subTypes.get(i));
152    }
153
154    public String getSubTypeName(int i) {
155      return (String) (subNames.get(i));
156    }
157  }
158}
159
160