Class IProperties
Patchworks is provided with the BeanShell script engine, but others such as ECMAScript, Groovy and Jython (Python in Java) are also available. Script engines are provisioned by adding the JSR223 jar file to the SPSHOME/Patchworks/lib folder. When a script engine is available if can be accessed by using commands to
- set and retrieve global variables (available to all script engines
(
putGlobal(String,Object)
,getGlobal(String)
), - evaluate commands (
eval(String,String)
), and - load and run scripts (
eval(File,String)
).
In addition this class contains static methods to coordinate access to stored properties, contained in a file named 'patchworks.ini' in each users home directory.
As well, the class manages the loading of script libraries, typically from the scripts folder in the Patchworks installation. After loading the system scripts, this class will try to load a custom initialization file in your home directory, if one exists. The custom file is written the language that the system is being initialized in, and has the name .patchworksrc.<language>
This class also contains methods for synchronizing access to volatile
data structures (suspend()
, resume()
).
In most cases these methods are not intended for internal application use only. The exception for this guidance is for the methods:
Method | Purpose |
---|---|
logEvent(String,String) | Add annotations to the event logs |
putGlobal(String,Object) | Save a value to a global variable shared between script engines. All active script engines can then access the variable typically by name as a regular variable. |
showURL(String) | Show a web page in the system default web browser |
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
about()
Return a string containing information about the Patchworks version and the current execution environment.static String
banner()
Return a banner string that is displayed in the script engine console windows.static String
escapeQuotes(String path)
Escape quotes in a string.static void
Evaluate a script that is referenced by theFile
in the specified language using the system default character encoding.static void
Evaluate a script that is referenced by theFile
in the specified language, using the specified file encoding.static Object
Evaluate a script that is provided by theReader
in the specified language.static Object
Evaluate a statement in the context of a languagestatic boolean
Test if a global variable existsstatic boolean
getBoolean(String name)
Get the boolean value of a global variable.static PWConsole
getConsole(String language)
Get aPWConsole
for the given language.static ScriptEngine
getEngineByExtension(String ext)
Get the scripting engine that will be used to process files of the given extension.static ScriptEngine
getEngineByLanguage(String language)
Get a scripting engine by the name of the languagestatic ScriptEngine
getEngineByName(String name)
Get a scripting engine by using an alternate language name.static Object
Get a global variable object.static Object
Get a global variable object.static int
Get the integer value of a global variable.static String
Get the name of the language that will process files having the given extension.Get a list of currently available language by names.static boolean
Test if the active simulation is a suspended state.static Properties
Load the saved properties from the 'patchworks.ini' file that is located in the personal Patchworks folder.static void
Write a message to the event log.static void
Start up a Beanshell interpreter in interactive mode.static void
moveToGlobalProperties(ScriptEngine engine)
Promote the engine variables into the global context.static void
Set a global interpreter value.static void
resume()
Resume the active simulation.static void
saveProps(Properties prop)
Save the current set of command properties to the 'patchworks.ini' file that is located in the personal Patchworks folder.static void
setEventLogger(EventLogger logger)
Set anEventLogger
object that will record events that happen during program execution.static void
setSynchronizationObject(Suspendable object)
Set the synchronization object that mediates the interaction of map displays and report production with the scheduler.static void
Attempt to display a web page by URL in the system default web browser.static boolean
suspend()
Suspend the active simulation.
-
Field Details
-
HOME
The read-only location of the Patchworks install folder. This is typically within the "Program Files/Spatial Planning Systems" folder. -
userPwDirectory
The read-only location of the personal Patchworks folder. This is usually a Patchworks folder within the Documents folder.
-
-
Constructor Details
-
IProperties
public IProperties()
-
-
Method Details
-
loadProps
Load the saved properties from the 'patchworks.ini' file that is located in the personal Patchworks folder. These properties keep track of previous inputs to wizards, pervious command settings, etc. -
saveProps
Save the current set of command properties to the 'patchworks.ini' file that is located in the personal Patchworks folder. -
setEventLogger
Set anEventLogger
object that will record events that happen during program execution. Typically events will be recorded to the ScenarioEvent report.This method is called internally from the Patchworks model and should not be called from user code.
-
logEvent
Write a message to the event log.IProperties.logEvent("Note", "Adjust weight values");
- Parameters:
type
- Specifies the category of the message.detail
- A detailed message.
-
setSynchronizationObject
Set the synchronization object that mediates the interaction of map displays and report production with the scheduler.This method is called internally from the Patchworks model and should not be called from user code.
-
suspend
public static boolean suspend()Suspend the active simulation. This method is called prior to accessing internal simulation data structures.This method is called internally from the Patchworks model and generally does not need to be called from user code.
-
isSuspended
public static boolean isSuspended()Test if the active simulation is a suspended state. -
resume
public static void resume()Resume the active simulation.This method is called internally from the Patchworks model and generally does not need to be called from user code.
-
putGlobal
Set a global interpreter value. This value will show up as a variable within all interpreters that are active (e.g. BeanShell, Groovy).- Parameters:
name
- the variable namevalue
- the value to assign to the variable
-
exists
Test if a global variable exists- Parameters:
name
- the variable name
-
getGlobal
Get a global variable object. If the variable does not exist return null.- Parameters:
name
- the variable name
-
getGlobal
Get a global variable object. If the variable does not exist then return the default value.- Parameters:
name
- the variable namedef
- the default value if the variable does not exist
-
getInt
Get the integer value of a global variable. If the variable does not exist then return 0. If the varaible is not a number then return 0. This method does not attempt to convert string variables to an integer value.- Parameters:
name
- the variable name
-
getBoolean
Get the boolean value of a global variable. If the variable does not exist then return false. If the variable does not have a boolean value then no conversion is attempted and false is returned.- Parameters:
name
- the variable name
-
getScriptLanguages
Get a list of currently available language by names. Languages are available if the JSR223 provider jar file is found in the classpath when Patchworks is started. The easiest way to provide a languages is to add the JSR223 jar file to the Patchworks\lib folder in the Patchworks home folder. -
getLanguageForExtension
Get the name of the language that will process files having the given extension.- Parameters:
ext
- the extension name
-
getEngineByExtension
Get the scripting engine that will be used to process files of the given extension.- Parameters:
ext
- the extension name
-
getEngineByLanguage
Get a scripting engine by the name of the language- Parameters:
language
- the name of the language
-
getEngineByName
Get a scripting engine by using an alternate language name. Scripting engines are allowed to provide multiple alternate names (such as BeanShell which hs alternate names of Beanshell, bsh and java).- Parameters:
name
- an alternate name for the engine
-
eval
Evaluate a statement in the context of a language- Parameters:
statement
- the statement to evaluatelanguage
- the language of the statement- Throws:
ScriptException
-
eval
Evaluate a script that is provided by theReader
in the specified language.- Parameters:
reader
- the Reader object that provides the scriptlanguage
- the language used to process the script- Throws:
ScriptException
-
eval
Evaluate a script that is referenced by theFile
in the specified language using the system default character encoding.- Parameters:
file
- the File object that references the scriptlanguage
- the language used to process the script- Throws:
ScriptException
-
eval
Evaluate a script that is referenced by theFile
in the specified language, using the specified file encoding.- Parameters:
file
- the File object that references the scriptlanguage
- the language used to process the scriptencoding
- the character encoding of the file- Throws:
ScriptException
-
escapeQuotes
Escape quotes in a string. Any double quote characters that are within the string will be replaced with and encoded \" characters. This is often required to pass quoted strings in to scripts.- Parameters:
path
- the string to be checked for embedded double quote characters.
-
getConsole
Get aPWConsole
for the given language. If the language is available this will return a console window running with an interpreter for the given language. The first time the console is requested the engine will be initialized. On subsequent calls the same console window will be reused with the same engine.- Parameters:
language
- the name of the language.
-
moveToGlobalProperties
Promote the engine variables into the global context. This will find all variables defined in the script engine context, and promote them in to the global context. The variables in the engine context will then be removed.This method allows all variables in an engine context to be shared with all other engines.
-
showURL
Attempt to display a web page by URL in the system default web browser. The URL should include the protocol to be used, for exampleshowURL("http://localhost:8081/current/index.html")
will display ReportWriter index for the current simulation andshowURL("file:///C:/Project_1/Data/Dictionary.html")
will show a web page stored in a file.- Parameters:
url
- the URL to show
-
about
Return a string containing information about the Patchworks version and the current execution environment. -
banner
Return a banner string that is displayed in the script engine console windows. -
main
Start up a Beanshell interpreter in interactive mode. If a script argument is not provided then the iterpreter starts immediately in interactive mode.This method requires a first parameter that specifies the scripting language that is to be used. The BeanShell language is almost always available, and other languages may also be available depending on how the installation has been customized. (Note that available languages may be determined usin the
getScriptLanguages()
command.If a script filename is provided then use it as the initial source of commands (as though "source(file);" was executed). After the script has completed the method will exit.
If no script filename is provided then an interactive shell is invoked. To leave interactive mode and end the interpreter use the "exit();" command.
This command is usually set up as the default action for double-clicking on a ".bsh" file from the file explorer.
- Parameters:
args
- A list of arguments to this commnd. The first argument (required) is the language to be used. The second argument (optional) is the script source file to be evaluated.
-