Skip to content

Commit

Permalink
Importer instantiates only what it needs
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny authored Sep 29, 2020
2 parents da99dd9 + 739d586 commit e9c21b6
Show file tree
Hide file tree
Showing 29 changed files with 1,250 additions and 228 deletions.
11 changes: 11 additions & 0 deletions src/api/libcellml/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ class LIBCELLML_EXPORT Component: public ComponentEntity, public ImportedEntity
*/
ComponentPtr clone() const;

/**
* @brief Determines whether this component or its descendants
* contain imported components.
*
* Determines whether this component or its descendants contain
* imported components.
*
* @return @c true when imports are required, @c false otherwise.
*/
bool requiresImports();

private:
Component(); /**< Constructor @private*/
explicit Component(const std::string &name); /**< Constructor named @private */
Expand Down
20 changes: 20 additions & 0 deletions src/api/libcellml/importedentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ class LIBCELLML_EXPORT ImportedEntity
*/
void setImportReference(const std::string &reference);

/**
* @brief Test whether this imported entity has been resolved.
*
* Returns @c true if the import and any dependencies are resolved, otherwise @c false.
*
* @return @c true if the import is resolved, @c false otherwise.
*/
bool isResolved() const;

/**
* @brief Set the resolution status of this imported entity.
*
* Set the resolution status of this imported entity. When @c true, this
* indicates that the item and all its dependent children have been resolved.
* Otherwise, @c false.
*
* @param status A boolean indicating import resolution status.
*/
void setResolved(bool status);

protected:
ImportedEntity(); /**< Constructor */

Expand Down
39 changes: 14 additions & 25 deletions src/api/libcellml/importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ class LIBCELLML_EXPORT Importer: public Logger
* the full path to the source model relative to this one.
* @param model The @c Model whose imports need resolution.
* @param baseFile The @c std::string location on local disk of the source @c Model.
*
* @return @c true if all imports have been resolved successfully, @c false otherwise.
*/
void resolveImports(ModelPtr &model, const std::string &baseFile);
bool resolveImports(ModelPtr &model, const std::string &baseFile);

/**
* @brief Return the number of models present in the importer's library.
Expand Down Expand Up @@ -102,6 +104,17 @@ class LIBCELLML_EXPORT Importer: public Logger
*/
ModelPtr library(const size_t &index);

/**
* @brief Get the key string under which a model is stored in the library, at the given @p index.
*
* Get the key string under which a model is stored in the library, at the given @p index.
*
* @param index The index of the key to return.
*
* @return If successful, a string under which the model has been stored, or an empty string otherwise.
*/
std::string key(const size_t &index);

/**
* @brief Manually add a local @c ModelPtr model instance to the importer library,
* using the given @p key as a reference.
Expand Down Expand Up @@ -137,30 +150,6 @@ class LIBCELLML_EXPORT Importer: public Logger
*/
bool replaceModel(const ModelPtr &model, const std::string &key);

/**
* @brief Retrieve the pair of URL key and import reference at the given index.
*
* This is taken from the list of dependencies for the models which have been resolved,
* and is what will break if those external files are ever moved or renamed.
*
* The first attribute of the returned pair is the URL at which the imported model was
* accessed and under which it is now stored in the library as its key, and the second attribute
* is the import reference.
*
* @return a @c std::pair of @c std::strings.
*/
std::pair<std::string, std::string> externalDependency(size_t index) const;

/**
* @brief Get the number of external dependencies in the library.
*
* Return the number of dependencies for the models which have been resolved by this
* importer.
*
* @return the number of external dependencies.
*/
size_t externalDependencyCount() const;

/**
* @brief Clear the links with other models from all import sources.
*
Expand Down
5 changes: 5 additions & 0 deletions src/bindings/interface/component.i
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ resets and False otherwise.";
%feature("docstring") libcellml::Component::clone
"Create a copy of this component.";

%feature("docstring") libcellml::Component::requiresImports
"Determines whether this component relies on any imports. If this component
or any of its encapsulated components are imported, returns @c true,
otherwise @c false.";

#if defined(SWIGPYTHON)
// Treat negative size_t as invalid index (instead of unknown method)
%extend libcellml::Component {
Expand Down
13 changes: 4 additions & 9 deletions src/bindings/interface/importer.i
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@ Returns True if the model was added, and False if the URL key already exists."
Returns True if the URL key is found in the library and the model is added,
and False if the URL key does not exist."

%feature("docstring") libcellml::Importer::externalDependencyCount
"Returns the number of external dependencies accessed while resolving all
the models passed to this importer."

%feature("docstring") libcellml::Importer::externalDependency
"Returns a pairs of strings at the given index. The first item in the pair is the external URL
at which imports for models which have been resolved by the importer were accessed (and under
which are now stored in the library). The second item is the import reference."

%feature("docstring") libcellml::Importer::clearImports
"Clears the links with other models from all import sources."

%feature("docstring") libcellml::Importer::key
"Returns a string corresponding to the key at which a model is stored in the
library by index, or an empty string if the index is out of range."

%{
#include "libcellml/importer.h"
%}
Expand Down
18 changes: 18 additions & 0 deletions src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,22 @@ ComponentPtr Component::clone() const
return c;
}

bool doRequiresImport(const ComponentPtr &thisComponent)
{
if (thisComponent->isImport()) {
return true;
}
for (size_t c = 0; c < thisComponent->componentCount(); ++c) {
if (doRequiresImport(thisComponent->component(c))) {
return true;
}
}
return false;
}

bool Component::requiresImports()
{
return doRequiresImport(shared_from_this());
}

} // namespace libcellml
13 changes: 11 additions & 2 deletions src/importedentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ struct ImportedEntity::ImportedEntityImpl
{
ImportSourcePtr mImportSource;
std::string mImportReference;
bool mIsResolved = false;
};

ImportedEntity::ImportedEntity()
: mPimpl(new ImportedEntityImpl())
{
mPimpl->mImportSource.reset();
mPimpl->mImportReference = "";
}

ImportedEntity::~ImportedEntity()
Expand Down Expand Up @@ -71,4 +70,14 @@ void ImportedEntity::setImportReference(const std::string &reference)
mPimpl->mImportReference = reference;
}

bool ImportedEntity::isResolved() const
{
return mPimpl->mIsResolved;
}

void ImportedEntity::setResolved(bool status)
{
mPimpl->mIsResolved = status;
}

} // namespace libcellml
Loading

0 comments on commit e9c21b6

Please sign in to comment.