Class AccessorCodeGenerator
- java.lang.Object
-
- ptolemy.kernel.util.NamedObj
-
- ptolemy.kernel.util.Attribute
-
- ptolemy.cg.kernel.generic.GenericCodeGenerator
-
- ptolemy.cg.kernel.generic.RunnableCodeGenerator
-
- ptolemy.cg.kernel.generic.accessor.AccessorCodeGenerator
-
- All Implemented Interfaces:
java.lang.Cloneable
,Changeable
,Debuggable
,DebugListener
,Decorator
,Derivable
,ModelErrorHandler
,MoMLExportable
,Moveable
,Nameable
- Direct Known Subclasses:
AccessorSSHCodeGenerator
public class AccessorCodeGenerator extends RunnableCodeGenerator
Generate a JavaScript Accessor for a model.Accessors are a technology, developed by the TerraSwarm Research Center, for composing heterogeneous devices and services in the Internet of Things (IoT). For more information, see http://accessors.org.
The model can only contain JavaScript and JSAccessor actors.
To generate an Accessor version of a model, use:
java -classpath $PTII ptolemy.cg.kernel.generic.accessor.AccessorCodeGenerator -language accessor $PTII/ptolemy/cg/kernel/generic/accessor/demo/TestComposite/TestComposite.xml; cat $PTII/org/terraswarm/accessor/accessors/web/cg/TestComposite.js
which is shorthand for:java -classpath $PTII ptolemy.cg.kernel.generic.accessor.AccessorCodeGenerator -generatorPackage ptolemy.cg.kernel.generic.accessor -generatorPackageList generic.accessor $PTII/ptolemy/cg/adapter/generic/accessor/adapters/org/test/auto/TestComposite.xml; cat ~/cg/TestComposite.js
For more information, see https://accessors.org/wiki/Main/CapeCodeHost#CodeGeneration.
- Since:
- Ptolemy II 11.0
- Version:
- $Id$
- Author:
- Christopher Brooks. Contributor: Edward A. Lee. Based on HTMLCodeGenerator by Man-Kit Leung, Bert Rodiers
- Pt.AcceptedRating:
- red (cxh)
- Pt.ProposedRating:
- red (cxh)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ptolemy.kernel.util.NamedObj
NamedObj.ContainedObjectsIterator
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
_checkForLocalModules
If true, the_setupCommands()
will check for modules in the local directory.StringParameter
modules
A comma separated list of modules to be installed.Parameter
npmInstall
If true, then search codeDirectory and its parent directories for a node_modules/ directory.-
Fields inherited from class ptolemy.cg.kernel.generic.RunnableCodeGenerator
_runCommandDefault, _substituteMap, run, runCommand
-
Fields inherited from class ptolemy.cg.kernel.generic.GenericCodeGenerator
_codeFileName, _eol, _executeCommands, _generateInSubdirectory, _model, _sanitizedModelName, codeDirectory, generateInSubdirectory, generatorPackage, generatorPackageList, INDENT1, INDENT2, INDENT3, overwriteFiles
-
Fields inherited from class ptolemy.kernel.util.NamedObj
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _deferChangeRequests, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS
-
-
Constructor Summary
Constructors Constructor Description AccessorCodeGenerator(NamedObj container, java.lang.String name)
Create a new instance of the AccessorCodeGenerator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
_generateCode(java.lang.StringBuffer code)
Generate Accessor code and append it to the given string buffer.protected java.lang.Class<?>
_getAdapterClassFilter()
Return the filter class to find adapters.protected java.util.List<java.lang.String>
_setupCommands()
Return a list of setup commands to be invoked before the run command.protected void
_updateSubstituteMap()
Update the substitute map for the setup and run commands The base class adds @codeDirectory@, @modelName@ and @PTII@ to the map.java.lang.String
comment(java.lang.String comment)
Return a formatted comment containing the specified string.-
Methods inherited from class ptolemy.cg.kernel.generic.RunnableCodeGenerator
_executeCommands, _reset, _runCommand, clone, updateCommandOptions
-
Methods inherited from class ptolemy.cg.kernel.generic.GenericCodeGenerator
_copyCFilesTosrc, _copyCFileTosrc, _getAdapter, _getAutoGeneratedAdapter, _getOutputFilename, _instantiateAdapter, _isTopLevel, _printTimeAndMemory, _resetAll, _writeCode, _writeCodeFileName, attributeChanged, createDecoratorAttributes, decoratedObjects, generateCode, generateCode, generateCode, generateCopyright, getAdapter, getCodeFileName, getComponent, getExecuteCommands, getMethodExceptionString, getMethodVisibilityString, isGlobalDecorator, main, setCodeGenerator, setContainer, setExecuteCommands
-
Methods inherited from class ptolemy.kernel.util.Attribute
_checkContainer, _getContainedObject, _propagateExistence, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, setName, updateContent
-
Methods inherited from class ptolemy.kernel.util.NamedObj
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _containedDecorators, _copyChangeRequestList, _debug, _debug, _debug, _debug, _debug, _description, _executeChangeRequests, _exportMoMLContents, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _notifyHierarchyListenersAfterChange, _notifyHierarchyListenersBeforeChange, _propagateValue, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, addHierarchyListener, attributeDeleted, attributeList, attributeList, attributeTypeChanged, clone, containedObjectsIterator, decorators, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getElementName, getFullName, getModelErrorHandler, getName, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, notifyOfNameChange, propagateExistence, propagateValue, propagateValues, removeAttribute, removeChangeListener, removeDebugListener, removeHierarchyListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, uniqueName, validateSettables, workspace
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface ptolemy.kernel.util.Nameable
description, getContainer, getDisplayName, getFullName, getName, getName, setName
-
-
-
-
Field Detail
-
modules
public StringParameter modules
A comma separated list of modules to be installed.
-
npmInstall
public Parameter npmInstall
If true, then search codeDirectory and its parent directories for a node_modules/ directory. If it is found, then search for the modules listed in the modules parameter in that directory. If any module is not found, then install all the modules using npm install. If all the modules are found, then don't bother installing.The reason to set this to false is if the host is not connected to the internet or if the host already has the modules installed. Setting this to false means that the composite accessor will be deployed more quickly because
npm install
will not be run. The default value is false, indicating thatnpm install modules
should not be run.
-
_checkForLocalModules
protected boolean _checkForLocalModules
If true, the_setupCommands()
will check for modules in the local directory. Derived classes like AccessorSSHCodeGenerator set this to false.
-
-
Constructor Detail
-
AccessorCodeGenerator
public AccessorCodeGenerator(NamedObj container, java.lang.String name) throws IllegalActionException, NameDuplicationException
Create a new instance of the AccessorCodeGenerator. The value of the generatorPackageList parameter of the base class is set togeneric.accessor
- Parameters:
container
- The container.name
- The name of the AccessorCodeGenerator.- Throws:
IllegalActionException
- If the super class throws the exception or error occurs when setting the file path.NameDuplicationException
- If the super class throws the exception or an error occurs when setting the file path.
-
-
Method Detail
-
comment
public java.lang.String comment(java.lang.String comment)
Return a formatted comment containing the specified string. In this base class, the comments is a Accessor-style comment, which begins with "//" followed by the platform dependent end of line character(s): under Unix: "\n", under Windows: "\n\r".o Subclasses may override this produce comments that match the code generation language.- Overrides:
comment
in classGenericCodeGenerator
- Parameters:
comment
- The string to put in the comment.- Returns:
- A formatted comment.
-
_generateCode
protected int _generateCode(java.lang.StringBuffer code) throws KernelException
Generate Accessor code and append it to the given string buffer. Write the code to the directory specified by the codeDirectory parameter. The file name is a sanitized version of the model name with a suffix that is based on last package name of the generatorPackage parameter. Thus if the codeDirectory is$HOME/cg
, the name of the model isFoo
and the generatorPackage isptolemy.cg.kernel.generic.accessor
, then the file that is written will be$HOME/cg/Foo.js
This method is the main entry point to generate js.
This method invokes the top level generateAccessor(), which is typically defined in ptolemy/cg/adapter/generic/accessor/adapters/ptolemy/actor/lib/jjs/JavaScript.java
- Overrides:
_generateCode
in classGenericCodeGenerator
- Parameters:
code
- The given string buffer.- Returns:
- The return value of the last subprocess that was executed. or -1 if no commands were executed.
- Throws:
KernelException
- If the target file cannot be overwritten or write-to-file throw any exception.
-
_getAdapterClassFilter
protected java.lang.Class<?> _getAdapterClassFilter()
Return the filter class to find adapters. All adapters have to extend this class.- Overrides:
_getAdapterClassFilter
in classGenericCodeGenerator
- Returns:
- The base class for the adapters.
-
_setupCommands
protected java.util.List<java.lang.String> _setupCommands() throws IllegalActionException
Return a list of setup commands to be invoked before the run command. In this class, the "npm install" command is added if the value of the npmInstall parameter is true and the value of the modules parameter is not empty.- Overrides:
_setupCommands
in classRunnableCodeGenerator
- Returns:
- The list of commands.
- Throws:
IllegalActionException
- If there is a problem getting the value of the modules parameter
-
_updateSubstituteMap
protected void _updateSubstituteMap() throws IllegalActionException
Update the substitute map for the setup and run commands The base class adds @codeDirectory@, @modelName@ and @PTII@ to the map.- Overrides:
_updateSubstituteMap
in classRunnableCodeGenerator
- Throws:
IllegalActionException
- If the @stopTime@ parameter cannot be parsed as a Double.
-
-