Application Interface
Topics covered in this chapter:
The functions for interacting with the Insight server are provided by the xpressinsight.AppInterface class. The methods of that class can be used, e.g., for downloading and uploading attachments, and for getting scenario and application meta data.
You can access the application interface functions via the insight property of the Insight application. This property is inherited from the base class xpressinsight.AppBase. The following example code, can be used to upload an attachment from within the Insight application class:
self.insight.put_scen_attach("attachment.txt")
Note that the Python keyword self represents the current instance of the Insight application class. The following example shows how to integrate this line of code in an application.
import xpressinsight as xi @xi.AppConfig(name="Attachment Example") class App(xi.AppBase): @xi.ExecModeLoad(descr="Uploads attachment.") def load(self): with open("attachment.txt", "w") as text_file: text_file.write("Hello!") self.insight.put_scen_attach("attachment.txt") print('Upload status:', self.insight.attach_status) @xi.ExecModeRun(descr="Downloads attachment.") def run(self): self.insight.get_scen_attach("attachment.txt") print('Download status:', self.insight.attach_status) if self.insight.attach_status == xi.AttachStatus.OK: with open("attachment.txt", "r") as text_file: print(text_file.readline())
App Interface Classes
This class represents the Xpress Insight application interface. Use this interface to access attachments and meta data like the scenario ID.
|
|
Retrieves information about a given app attachment.
|
|
Property for the id of the Xpress Insight application which is the parent of the model.
|
|
Property for the name of the Xpress Insight application which is the parent of the model.
|
|
Read-only property indicating the status of the most recent attempt to access or modify an attachment.
|
|
Deletes a scenario attachment.
|
|
Delete the internal working directory of the xpressinsight package.
|
|
Property for the execution mode in which Xpress Insight is running the model.
|
|
Retrieves an app attachment from the Insight server, placing it in the Python working directory where it can be read by the model.
|
|
Gets Insight attachments by tag
|
|
Gets Insight attachments by tag
|
|
Retrieves the the 'rules' used to validate attachments and attachment meta-data.
|
|
Gets Insight attachments by tag
|
|
Get information for a repository item with the supplied path.
|
|
Get information for items in the folder with the supplied path.
|
|
Retrieves an attachment from the Insight server either for a given scenario, placing it in the Python working directory where it can be read by the model.
|
|
Retrieves a list of all the files attached to the app.
|
|
Retrieves a list of all the files attached to the app with the given tag.
|
|
Retrieves a list of the attachment tags defined in the companion file.
|
|
Retrieves a list of all the files attached to a given scenario.
|
|
Retrieves a list of all the files attached to a scenario with the given tag.
|
|
Uploads a scenario attachment to the Insight server, reading it from the Python working directory.
|
|
Renames an existing scenario attachment.
|
|
Resets the progress state for each progress metric back to zero.
|
|
Retrieves information about a given scenario attachment.
|
|
Property for the id of the Xpress Insight scenario.
|
|
Property for the name of the Xpress Insight scenario.
|
|
Property for the path of the Xpress Insight scenario.
|
|
Sets the 'rules' used to validate attachments and attachment meta-data.
|
|
Sets the list of tags that can be used in attachments
|
|
Update the description of an existing scenario attachment.
|
|
Mark an existing scenario attachment as hidden or visible in the Xpress Insight UI.
|
|
Update the tags of an existing scenario attachment.
|
|
Property for the path to use for the attachments directory of the current app, when in test mode.
|
|
Property for the location to store mock attachments for app and scenario, when in test mode.
|
|
Property for the location of the app companion file to parse, when in test mode.
|
|
Read-only property to check whether the Insight application is running in test mode or in Insight.
|
|
Property for the path to use for the scenario attachments directory of the current scenario.
|
|
Sends a progress update notification for a single metric from the model to the Xpress Insight system.
|
|
Property for the username of the Insight user that initiated the current scenario execution.
|
|
Read-only property for the internal working directory of the xpressinsight package.
|
|
Indicates the type of metric a progress update is providing.
|
|
Indicates the direction of optimization.
|
xpressinsight.AppInterface |
xpressinsight.AppInterface.app_attach_info |
app_attach_info(self, filename: str) -> Optional[xpressinsight.Attachment]
filename
|
The filename of the app attachment to request
|
>>> my_attachment = insight.app_attach_info('my_attach.dat') ... if insight.attach_status == AttachStatus.OK: ... print("Attachment description: ", my_attachment.description) ... else: ... print("Error querying attachment")
xpressinsight.AppInterface.app_id |
app_id: str
>>> insight.app_id = 'xyzzy'
>>> print('app id = ', insight.app_id) app id = xyzzy
xpressinsight.AppInterface.app_name |
app_name: str
>>> insight.app_name = 'My App'
>>> print('app name = ', insight.app_name) app name = My App
xpressinsight.AppInterface.attach_status |
attach_status: AttachStatus
xpressinsight.AppInterface.delete_scen_attach |
delete_scen_attach(self, filename: str) -> None
filename
|
The filename of the attachment to be deleted.
|
>>> insight.delete_scen_attach('my_attach.dat') ... if insight.attach_status == AttachStatus.OK: ... print("Attachment deleted") ... else: ... print("Error deleting attachment")
xpressinsight.AppInterface.delete_work_dir |
delete_work_dir(self)
xpressinsight.AppInterface.exec_mode |
exec_mode: str
>>> insight.exec_mode = 'CALCULATE_STATS'
>>> print('execution mode = ', insight.exec_mode) execution mode = CALCULATE_STATS
xpressinsight.AppInterface.get_app_attach |
get_app_attach(self, filename: str) -> None
filename
|
The filename of the attachment to be retrieved.
|
>>> insight.get_app_attach('my_attach.dat') ... if insight.attach_status == AttachStatus.OK: ... with open('my_attach.dat') as f: ... pass # process the file ... else: ... print("Error retrieving attachment")
xpressinsight.AppInterface.get_attach_by_tag |
get_attach_by_tag(self, tag: str) -> Optional[xpressinsight.Attachment]
tag
|
The tag to search for
|
>>> attachment = insight.get_attach_by_tag('mytag1') ... if insight.attach_status != AttachStatus.OK: ... print("Error searching for attachments") ... else: ... with open(attachment.filename) as f: ... pass # process the file
xpressinsight.AppInterface.get_attach_filenames_by_tag |
get_attach_filenames_by_tag(self, tag: str) -> List[str]
tag
|
The tag to search for
|
>>> filenames = insight.get_attach_by_tag('mytag1') ... if insight.attach_status != AttachStatus.OK: ... print("Error searching for attachments") ... else: ... for f in filenames: ... print(f)
xpressinsight.AppInterface.get_attach_rules |
get_attach_rules(self) -> xpressinsight.AttachmentRules
>>> rules = insight.get_attach_rules()
xpressinsight.AppInterface.get_attachs_by_tag |
get_attachs_by_tag(self, tag: str) -> Optional[List[xpressinsight.Attachment]]
tag
|
The tag to search for
|
>>> attachments = insight.get_attachs_by_tag('mytag1') ... if insight.attach_status != AttachStatus.OK: ... print("Error searching for attachments") ... else: ... for a in attachments: ... print(a.filename)
xpressinsight.AppInterface.get_item_info |
get_item_info(self, path: str) -> xpressinsight.ItemInfo
path: str
|
Path to the repository item.
|
>>> info = insight.get_item_info('/my_app/my_scenario')
>>> @xi.ExecModeLoad() >>> def load(self): >>> try: >>> info = self.insight.get_item_info(".") >>> print(info) >>> except xi.InterfaceError as ex: >>> print(ex)
xpressinsight.AppInterface.get_item_infos |
get_item_infos(self, folder_path: str) -> List[xpressinsight.ItemInfo]
folder_path: str
|
Path to the repository folder.
|
>>> info = insight.get_item_infos('/my_app/my_folder')
>>> @xi.ExecModeLoad() >>> def load(self): >>> try: >>> infos = self.insight.get_item_infos(".") >>> print(infos) >>> except xi.InterfaceError as ex: >>> print(ex)
xpressinsight.AppInterface.get_scen_attach |
get_scen_attach(self, filename: str, scenario_path: str = None) -> None
filename
|
The filename of the attachment to be retrieved.
|
scenario_path
|
The path of a scenario. A scenario path is the full path to a scenario name starting from the repository root and including the app name. E.g.
/myapp/DirA/myscenario If the scenario path is not specified, the attachment is retrieved for the current scenario.
|
>>> insight.get_scen_attach('my_attach.dat') ... if insight.attach_status == AttachStatus.OK: ... with open('my_attach.dat') as f: ... pass # process the file ... else: ... print("Error retrieving attachment")
>>> insight.get_scen_attach('my_attach.dat', '/myapp/DirA/myscenario') ... if insight.attach_status == AttachStatus.OK: ... with open('my_attach.dat') as f: ... pass # process the file ... else: ... print("Error retrieving attachment")
xpressinsight.AppInterface.list_app_attach |
list_app_attach(self) -> List[xpressinsight.Attachment]
>>> atts = insight.list_app_attach() ... if insight.attach_status == AttachStatus.OK: ... print("Attachments: ", atts) ... else: ... print("Error listing attachments")
xpressinsight.AppInterface.list_app_attach_by_tag |
list_app_attach_by_tag(self, tag: str) -> List[xpressinsight.Attachment]
tag
|
The tag to search for
|
>>> atts = insight_list_app_attach_by_tag('mytag1') ... if insight.attach_status == AttachStatus.OK: ... print("Attachments: ", atts) ... else: ... print("Error listing attachments")
xpressinsight.AppInterface.list_attach_tags |
list_attach_tags(self) -> List[xpressinsight.AttachTag]
>>> tags = insight.list_attach_tags() ... if insight.attach_status == AttachStatus.OK: ... print("Defined tags: ", tagslist) ... else: ... print("Error retrieving tags list")
xpressinsight.AppInterface.list_scen_attach |
list_scen_attach(self, scenario_path: str = None) -> List[xpressinsight.Attachment]
scenario_path
|
The path of a scenario. A scenario path is the full path to a scenario name starting from the repository root and including the app name. E.g.
/myapp/DirA/myscenario If the scenario path is not specified, the attachment is retrieved for the current scenario
|
>>> atts = insight.list_scen_attach() ... if insight.attach_status == AttachStatus.OK: ... print("Attachments: ", atts) ... else: ... print("Error listing attachments")
>>> atts = insight.list_scen_attach('/myapp/DirA/myscenario') ... if insight.attach_status == AttachStatus.OK: ... print("Attachments: ", atts) ... else: ... print("Error listing attachments")
xpressinsight.AppInterface.list_scen_attach_by_tag |
list_scen_attach_by_tag(self, tag: str, scenario_path: str = None) -> List[xpressinsight.Attachment]
tag
|
The tag to search for
|
scenario_path
|
The path of a scenario. A scenario path is the full path to a scenario name starting from the repository root and including the app name. E.g.
/myapp/DirA/myscenario. If the scenario path is not specified, the attachment is retrieved for the current scenario.
|
>>> atts = insight.list_scen_attach_by_tag('mytag1') ... if insight.attach_status == AttachStatus.OK: ... print("Attachments: ", atts) ... else: ... print("Error listing attachments")
>>> atts = insight.list_scen_attach_by_tag('mytag1', ... '/myapp/DirA/myscenario') ... if insight.attach_status == AttachStatus.OK: ... print("Attachments: ", atts) ... else: ... print("Error listing attachments")
xpressinsight.AppInterface.put_scen_attach |
put_scen_attach(self, filename: str, overwrite: bool = True) -> None
filename
|
The filename of the attachment to be uploaded
|
overwrite
|
If
True, will overwrite attachment if it already exists. If
False and attachment already exists, will fail with insight.attach_status returning AttachStatus.ALREADY_EXISTS. Defaults to
True if not given.
|
>>> insight.put_scen_attach('my_attach.dat', False) ... if insight.attach_status == AttachStatus.OK: ... print("Attachment added ok") ... elif insight.attach_status == AttachStatus.ALREADY_EXISTS: ... print("Attachment already exists") ... else: ... print("Error adding attachment:", insight.attach_status)
xpressinsight.AppInterface.rename_scen_attach |
rename_scen_attach(self, old_name: str, new_name: str) -> None
old_name
|
The existing filename of the attachment to be renamed
|
new_name
|
The new filename of the attachment. Must not already be used for a scenario attachment.
|
>>> insight.rename_scen_attach('my_attach.dat', 'my_attach-2015.dat') ... if insight.attach_status == AttachStatus.OK: ... print("Attachment renamed ok") ... else: ... print("Error renaming attachment")
xpressinsight.AppInterface.reset_progress |
reset_progress(self) -> None
>>> insight.reset_progress()
xpressinsight.AppInterface.scen_attach_info |
scen_attach_info(self, filename: str) -> Optional[xpressinsight.Attachment]
filename
|
The filename of the scenario attachment to request
|
>>> my_attachment = insight.scen_attach_info('my_attach.dat') ... if insight.attach_status == AttachStatus.OK: ... print("Attachment description: ", my_attachment.description) ... else: ... print("Error querying attachment")
xpressinsight.AppInterface.scenario_id |
scenario_id: str
>>> insight.scenario_id = 'xyzzy'
>>> print('scenario id = ', insight.scenario_id) scenario id = xyzzy
xpressinsight.AppInterface.scenario_name |
scenario_name: str
>>> insight.scenario_name = 'Scenario B'
>>> print('scenario name = ', insight.scenario_name) scenario name = Scenario B
xpressinsight.AppInterface.scenario_path |
scenario_path: str
>>> insight.scenario_path = '/myapp/DirA/myscenario'
>>> print('scenario path = ', insight.scenario_path) scenario path = /myapp/DirA/myscenario
xpressinsight.AppInterface.set_attach_rules |
set_attach_rules(self, new_rules: xpressinsight.AttachmentRules)
new_rules
|
Populated
insightattachmentrules record
|
>>> insight.set_attach_rules(AttachmentRules( ... max_size=1*1024*1024, ... max_attach_count=25, ... max_filename_len=32, ... invalid_filename_chars=['/', r'', ' '], ... max_description_len=128, ... ))
xpressinsight.AppInterface.set_attach_tags |
set_attach_tags(self, new_tags: List[xpressinsight.AttachTag])
new_tags
|
List of populated
insightattachmenttag records.
|
>>> insight.set_attach_tags([ ... AttachTag(name='first', usage=AttachTagUsage.SINGLE_FILE), ... AttachTag(name='test', usage=AttachTagUsage.MULTI_FILE), ... ])
xpressinsight.AppInterface.set_scen_attach_desc |
set_scen_attach_desc(self, filename: str, description: str) -> None
filename
|
The filename of the scenario attachment to update
|
description
|
The new description of the attachment
|
>>> insight.set_scen_attach_desc('my_attach.dat', ... 'This is my attachment') ... if insight.attach_status == AttachStatus.OK: ... print("Attachment description updated ok") ... else: ... print("Error updating attachment")
xpressinsight.AppInterface.set_scen_attach_hidden |
set_scen_attach_hidden(self, filename: str, hidden: bool) -> None
filename
|
The filename of the scenario attachment to hide or show.
|
hidden
|
If
True, the attachment will be hidden in the Xpress Insight UI; if
False, it will be visible.
|
>>> insight.set_scen_attach_hidden('my_attach.dat', True) ... if insight.attach_status == AttachStatus.OK: ... print("Attachment hidden ok") ... else: ... print("Error hiding attachment")
xpressinsight.AppInterface.set_scen_attach_tags |
set_scen_attach_tags(self, filename: str, tags: List[str]) -> None
filename
|
The filename of the scenario attachment to update.
|
tags
|
The new tags to apply to the attachment. Any existing tags will be removed.
|
>>> insight.set_scen_attach_tags('my_attach.dat', ... ['mytag1', 'mytag2']) ... if insight.attach_status == AttachStatus.OK: ... print("Attachment tags updated ok") ... else: ... print("Error updating attachment")
xpressinsight.AppInterface.test_app_attach_dir |
test_app_attach_dir: str
>>> print(insight.test_app_attach_dir)
>>> insight.test_app_attach_dir = 'C:/dev/appattachments'
xpressinsight.AppInterface.test_attach_dir |
test_attach_dir: str
>>> insight.test_attach_dir = 'C:/dev/appattachments'
>>> print(insight.test_attach_dir)
xpressinsight.AppInterface.test_cfile_path |
test_cfile_path: str
>>> insight.test_cfile_path = 'C:/dev/app/application.xml'
>>> print(insight.test_cfile_path)
xpressinsight.AppInterface.test_mode |
test_mode: bool
xpressinsight.AppInterface.test_scen_attach_dir |
test_scen_attach_dir: str
>>> insight.test_scen_attach_dir = 'C:/dev/scenattachments'
>>> print(insight.test_scen_attach_dir)
xpressinsight.AppInterface.update |
update(self, metric: xpressinsight.Metric, value: Union[float, int, xpressinsight.ObjSense]) -> None
metric
|
The type of metric to update.
|
value
|
The value of the metric to update.
|
>>> insight.update(Metric.OBJVAL, 51.9)
>>> def on_gap_notify(prob, app): ... ... num_sol = prob.attributes.mipsols ... app.insight.update(xi.Metric.NUMSOLS, num_sol) ... ... if num_sol == 0: ... # Can only occur when mipabsgapnotifybound is used. ... # Don't call gapnotify again. ... return None, None, None, None ... ... objective = prob.attributes.mipobjval ... best_bound = prob.attributes.bestbound ... ... if best_bound != 0 or objective != 0: ... gap = abs(objective - best_bound) / \ ... max(abs(best_bound), abs(objective)) ... else: ... gap = 0 ... ... app.insight.update(xi.Metric.OBJVAL, objective) ... app.insight.update(xi.Metric.GAP, gap) ... ... if gap > 1e-6: ... new_rel_gap_notify_target = gap - 1e-6 ... else: ... # Don't call gapnotify again. ... new_rel_gap_notify_target = -1 ... ... return new_rel_gap_notify_target, None, None, None
>>> prob = xp.problem() ... ... # TODO: Define the optimization problem ... ... # Optionally reset progress and set the objective sense ... self.insight.reset_progress() ... self.insight.update(xi.Metric.OBJSENSE, prob.attributes.objsense) ... ... prob.controls.miprelgapnotify = 1e20 ... prob.addcbgapnotify(on_gap_notify, self, 0) ... ... prob.solve()
xpressinsight.AppInterface.username |
username: str
>>> insight.username = 'LouisXIV'
>>> print('user name = ', insight.username) user name = LouisXIV
xpressinsight.AppInterface.work_dir |
work_dir: str
xpressinsight.Metric |
class xi.Metric(Enum)
xi.Metric.GAP
|
The gap to the optimal solution, as a percentage.
|
xi.Metric.OBJVAL
|
The best solution value found so far.
|
xi.Metric.NUMSOLS
|
The count of feasible solutions found so far.
|
xi.Metric.OBJSENSE
|
The direction of the solve.
|
xpressinsight.ObjSense |
class xi.ObjSense(Enum)
xi.ObjSense.MINIMIZE
|
This is a minimization problem.
|
xi.ObjSense.MAXIMIZE
|
This is a maximization problem.
|
Attachments Classes
An object containing information about a single attachment.
|
|
A class containing information about the rules used by Insight when verifying attachments.
|
|
Indicates the status of the most recent attempt to access or modify an attachment.
|
|
A class containing information about a tag defined in the app's companion file.
|
|
Possible values for attachment tag usage.
|
xpressinsight.AttachStatus |
class xi.AttachStatus(Enum)
xi.AttachStatus.OK
|
The operation completed successfully.
|
xi.AttachStatus.NOT_FOUND
|
The specified attachment does not exist.
|
xi.AttachStatus.INVALID_FILENAME
|
An attachment could not be created or renamed because the specified filename is invalid. It may be too long, too short, or contain invalid characters.
|
xi.AttachStatus.INVALID_DESCRIPTION
|
The specified description is invalid. The description can be a maximum of 2500 characters in length.
|
xi.AttachStatus.ALREADY_EXISTS
|
An attachment could not be created because another attachment with the same name already exists.
|
xi.AttachStatus.TOO_LARGE
|
An attachment could not be created because the attached file is too large. Attachments can be a maximum of 150Mb in size.
|
xi.AttachStatus.TOO_MANY
|
An attachment could not be created because the maximum number of attachments (250) has been reached for the app or scenario.
|
xi.AttachStatus.INVALID_TAGS
|
Invalid tags were provided.
|
xi.AttachStatus.SEVERAL_FOUND
|
Several attachments match the given tag but the proxpressinsight
|
xpressinsight.Attachment |
filename: str
|
filename of the attachment
|
description: str
|
description of the attachment
|
tags: List[str]
|
collection of tags associated with the attachment
|
size: int
|
size of the attachment, in bytes
|
last_modified_user: str
|
name of the last Insight user to modify the attachment
|
last_modified_date: datetime.datetime
|
date and time of last modification to attachment
|
hidden: bool
|
whether the attachment is hidden from the UI
|
xpressinsight.AttachmentRules |
max_size: int
|
The maximum size, in bytes, that an attachment may have.
|
max_attach_count: int
|
The maximum number of attachments that can be attached to a single scenario.
|
max_filename_len: int
|
The maximum permitted length, in characters, of an attachment filename.
|
invalid_filename_chars: List[str]
|
A list of characters that are not permitted in attachment filenames. Must be a list of single-character string values.
|
max_description_len: int
|
The maximum permitted length, in characters, of an attachment description.
|
xpressinsight.AttachTag |
name: str
|
Name of the tag.
|
description: str
|
Description of the tag.
|
mandatory: bool
|
Whether the tag is mandatory.
|
usage: AttachTagUsage
|
Tag usage restrictions, either
AttachTagUsage.SINGLE_FILE or
AttachTagUsage.MULTI_FILE.
|
xpressinsight.AttachTagUsage |
class xi.AttachTagUsage(Enum)
xi.AttachTagUsage.SINGLE_FILE: str
|
This tag may only be assigned to at most one attachment file.
|
xi.AttachTagUsage.MULTI_FILE: str
|
This tag may only be assigned to any number of attachment files.
|
Repository Classes
An exception that occurred during a call to certain AppInterface methods.
|
|
A class containing information for a repository item.
|
xpressinsight.InterfaceError |
message: str
|
The error message.
|
>>> @xi.ExecModeLoad() >>> def load(self): >>> try: >>> info = self.insight.get_item_info(".") >>> print(info) >>> except xi.InterfaceError as ex: >>> print(ex) # Print error type and message. >>> # print("ERROR:", ex.message) # Print error message.
xpressinsight.ItemInfo |
id: str
|
Item id.
|
type: str
|
Item type (
"FOLDER" or scenario type identifier).
|
name: str
|
Item name.
|
path: str
|
Item path.
|
parent_path: str
|
Item parent path.
|
© 2001-2021 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.