001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: aschultz $'
006 * '$Date: 2011-04-08 23:06:46 +0000 (Fri, 08 Apr 2011) $' 
007 * '$Revision: 27484 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.objectmanager.library;
031
032import java.util.Stack;
033import java.util.Vector;
034
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037
038import ptolemy.kernel.ComponentEntity;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NamedObj;
042import ptolemy.kernel.util.Workspace;
043import ptolemy.moml.EntityLibrary;
044
045/**
046 * This class is used for generating the Component Library from the prebuilt
047 * LibIndex. It's purpose is to separate out the functionality of generating an
048 * entire library using the index from the function of dynamically changing the
049 * library contents one item or node at a time, as the functions of the
050 * LibraryManager do. This will keep confusion about the API to a minimum
051 * (before they were all in the same class).
052 * 
053 * @author Aaron Schultz
054 */
055public class LibraryGenerator {
056
057        private static final Log log = LogFactory.getLog(LibraryGenerator.class
058                        .getName());
059        private static final boolean isDebugging = log.isDebugEnabled();
060
061        /**
062         * Convenience reference.
063         */
064        private LibraryManager _libMan;
065
066        public LibraryGenerator() {
067                _libMan = LibraryManager.getInstance();
068        }
069
070        public CompositeEntity generate(Workspace workspace, LibIndex libIndex) {
071                CompositeEntity _root = null;
072                try {
073                        _root = (CompositeEntity) new EntityLibrary(workspace);
074                        _root.setName("kepler actor library");
075                } catch (ptolemy.kernel.util.NameDuplicationException nde) {
076                        // do nothing, just leave the name blank
077                        System.out.println("the name 'kepler actor library' is already in use");
078                } catch (IllegalActionException e) {
079                        e.printStackTrace();
080                }
081                if (_root == null) return null;
082
083                Stack<LibItem> s = new Stack<LibItem>();
084                Stack<NamedObj> treeObjs = new Stack<NamedObj>();
085                treeObjs.push(_root);
086
087                Vector<LibItem> items = libIndex.getItems();
088                System.gc();
089                for (LibItem item : items) {
090                        // if (isDebugging) log.debug(item.debugString());
091                        try {
092                                // if (isDebugging) log.debug("s.size(): " + s.size());
093                                // if (isDebugging) log.debug("treeObjs.size(): " +
094                                // treeObjs.size());
095                                while (s.size() > 0) {
096                                        int lastRight = s.elementAt(s.size() - 1).getRight();
097                                        int thisRight = item.getRight();
098                                        if (lastRight < thisRight) {
099                                                // if (isDebugging)
100                                                // log.debug(s.elementAt(s.size()-1).getRight() + " < "
101                                                // + item.getRight());
102                                                s.pop();
103                                                treeObjs.pop();
104                                        } else {
105                                                break;
106                                        }
107                                }
108                                if (isDebugging) log.debug( s.toString() );
109                                if (isDebugging) log.debug( treeObjs.toString() );
110                                NamedObj parent = treeObjs.elementAt(treeObjs.size() - 1);
111                                if (isDebugging) log.debug(parent.getName() + " " + parent.getClass().getName());
112                                if (parent instanceof CompositeEntity) {
113                                        ComponentEntity current = _libMan.createAndAddTreeItem(
114                                                        (CompositeEntity) parent, item);
115                                        if (isDebugging) {
116                                                String indent = " ";
117                                                for (int i = 0; i < treeObjs.size(); i++) {
118                                                        indent += "+-";
119                                                }
120                                                log.debug(indent + " " + current.getName() + "     "
121                                                                + current.getClass().getName());
122                                        }
123                                        s.push(item);
124                                        treeObjs.push(current);
125                                } else {
126                                        log
127                                                        .error("Parent is something other than CompositeEntity!  Should never happen.");
128                                        // Try refreshing the order
129                                        
130                                        // Try complete reubild
131                                        return _root;
132                                }
133                        } catch (Exception e) {
134                                e.printStackTrace();
135                        }
136                }
137                System.gc();
138                return _root;
139        }
140
141}