在ADE中处理模型、模块和文件

Revision as of 07:00, 4 October 2015 by Jhuawen (talk | contribs) (Created page with "上面的代码使用 SetDataByLabels将<tt>position Index2="SomeLabelInIndex2", Index1="SomeLabelInIndex1"</tt>位置上的元素设定给'''Element''...")
Other languages:
English • ‎中文

ADE 用户指南 >

这部分包含了一些你可能在Analytica模型中执行的常见操作和处理示例。

模型和模块

注意:在VBScript、VBA,和pre-dot-NET等Visual Basic版本语言环境中,在将一个对象赋值给一个变量的时候关键字Set是必须的。在VB.NET中关键字Set不在需要了。在下面的示例中没有使用关键字Set。

建立一个新模型:

If ADE. CreateModel("NewModelName") Then
’Model successfully created
End If

CreateModel 方法仅需要一个参数,包含模型名称的一个字符串。

打开一个现有Analytica模型:

Dim ModName as String
ModName = ADE. OpenModel("C:\ ... \Anamodel.ana")
If ModName="" then
’ Handle Error condition here
End if

如果已经有一个模型打开,该模型将在新模型建立之前自动关闭。如果指定的文件名是不合法的, OpenModel将返回一个空的字符串。在此情况下,使用 CAEngine ErrorCode属性来确定错误的原因。注意即使载入成功,通常也会返回一条ErrorCode=2警告信息。关于导致错误或者警告信息的具体原因可使用 CAEngine OutputBuffer属性来确定。在使用ADE时,你必须在路劲定界符中使用反斜杠(\)。ADE不支持斜杠(/)。

将一个文件中的模块添加到当前打开的模型中:

Dim Merge as Boolean = True
Dim ModName as String
ModName = ADE. AddModule ("C:\...\MyLibrary.ana", Merge)
if ModName="" Then
’ Handle error conditions here
End if

FileSpec 参数应该包含将被包括的模块的路径和文件名。Merge参数是一个布尔变量,确定先前已经存在的具有相同名称的对象是否被重写。如果Merge=True,那么冲突变量将被重写。如果Merge=False,并且存在冲突变量,那么表示调用 AddModule失败。

读取脚本文件:

If ADE. ReadScript("C:\..\MyScript.ana") Then
’ Script successfully read
End

如果一个脚本文件包含一个typescript指令列表。根据载入的文件,引擎执行文件中包含的指令。当运行脚本文件在 ErrorText属性中被描述时,将发生错误。

在一个单独的文件中保存模块(也就是当前模型的一个子集):

If ADE. SaveModuleFile ("MyLibrary", "C:\...\MyLibrary.ana") Then
’ Save succeeded
End

如果第一个参数是模块标识符,那么第二个参数将是文件名。

将当前模型保存到一个文件中:

If ADE. SaveModel("C:\...\MyNewModel.ana") Then
’ Save succeeded
End If

关闭但不保存当前模型:

If ADE. CloseModel() Then … ’ Close succeeded

CloseModel方法不带任何参数。

ADE 对象

建立一个新的CAObject对象

Dim ObjName As String = "NewVariable"
Dim ObjClass As String = "Variable"
Dim var As CAObject = ADE. CreateObject(ObjName, ObjClass)

将被建立的对象的名称和类型被传递给 CreateObject方法。注意在给对象取名时,必须使用对象的 标识符而非 名称。大多数与对象相关的方法使用 标识符属性,而非 名称属性。ADE能够建立一下对象类型:一般变量、模块、机会变量、常量、决策变量、索引变量和决策变量等等。关于这些对象类型的更多信息请参考Analytica 用户指南

从模型中删除Analytica对象:

Dim obj as CAObject
If ADE. DeleteObject(obj) Then … ’ Successful

设置正在使用的模块

ObjName = "ModuleToMakeActive"
ObjClass = "Module"
Var = ADE. CreateObject (ObjName, ObjClass)
ADE. CurrentModule = Var

ADE使用层次结构给对象排序。当一个对象被建立时,该对象是在当前模块中被建立。默认情况下,所有对象都被置于顶层模块当中,除非你设置了 CurrentModule属性。

识别当前模块:

Dim module As CAObject = ADE. CurrentModule

当你知道一个Analytica变量的名称时,获取 CAObject 对象

这可能是ADE中最常使用的方法。

Dim Var As CAObject = ADE. GetObjectByName ("IdentifierInModel")
If Var Is Nothing Then
’Analytica model associated with Ana
’does not contain variable with
’identifier "IdentifierInModel"
End If

方法 CAEngine::Get CAEngine::GetObjectByName等同。

使用GetAttribute方法获取Analytica全部属性

UnitsOfVar = Var. GetAttribute (" Units")

使用 SetAttribute 方法改变一个Analytica 对象的属性:

If Var. SetAttribute (" 定义","A/B") Then
’Attribute Set Correctly
Else
’Attribute Not Set
End If

使用Name属性访问或者重命名对象的标识符

Dim oldName As String = Var.Name
Var. Name = "NewIdentifer"


对象属性完整列表请参考 Analytica 脚本指南

==检索计算结果== CAObject类型包含三种可以导致结果被计算并返回的方法。 Result方法求取模型中的一个对象的值并返回一个单一值结果。如果你知道结果是一个单一数或者单一文本字符串,这非常有用。 ResultTable方法求取模型中的一个对象的值并返回一个 CATable对象结果。 CATable对象的方法和属性让你理解有哪些维度存在,并访问单个元素(单元格)。 Evaluate方法处理一个任意表达式并以一个多维 CATable的形式返回解析和计算该表达式的结果。

在检索结果时,你控制使用哪种计算方式来计算结果。你可以计算确定性中间值,或者不同的概率视图:Mean(平均值)、Sample(样本)、 PDF(概率密度函数)、CDF(累积密度函数)、Statistics(统计数据)或者Bands(概率带)。将 ResultType 设置成你想要的结果类型(默认为Mid:中间值)。

不论你是在计算一个标量还是一个表格,你的程序最终获取单个基元值,比如说数字或者文本字符串。你可以使用各种 RenderingStyle设置来控制这些值被返回的形式。例如。数字值可以以浮点数、格式化字符串、或者完整字符串描述的形式返回。文本字符串返回时可带可不带引号。

计算并获得一个对象的简单结果(例如一个标量)

使用 CAObject Result方法:

Dim Obj As  CAObject
Dim Result
Obj = ADE. GetObjectByName ("ObjectToEvaluate")
Result = Obj. CAObject::Result
If ADE. ErrorCode = 0 Then
’Result was successfully retrieved
Else
’An error occurred
End If

默认情况下 CAObject Result属性检索对象的中间值结果。返回一个变体结果(或者在.NET中,返回一个System.Object结果)。该方法对于检索计算结果为标量的对象的结果非常方便。

计算并获取非中间值的对象结果

使用 CATable或者 CAObject ResultType属性:

Dim Obj As CAObject = ADE. GetObjectByName("ObjectToEvaluate")
Dim Result
Obj. ResultType = 1 ' get result as mean
Result = Obj. Result
If ADE. ErrorCode = 0 Then
’Result was successfully retrieved as a mean
Else
’An error occurred
End If

ResultType属性用来指明 Result应该返回的结果的类型。可能值为: 0=Mid point(中间值)、1=Mean(平均值)、2=Probabilistic Sample(概率样本)、3=PDF(概率密度函数)、4=CDF(累积密度函数)、5=Statistics(统计数据),以及6=Probability Bands(概率带)。当ResultType>=2,结果通常是一个表格,即使中间值和平均值为标量。关于检索表格结果的讨论请参考下一节。

检索格式化结果,设置对象的 RenderingStyle属性

Dim Obj As CAObject = ADE.GetObjectByName("ObjectToEvaluate")
Dim Result
Obj. RenderingStyle. NumberAsText = true
Obj. RenderingStyle. StringQuotes = 2 ’ double quotes.
Result = Obj. Result
If ADE. ErrorCode = 0 then
’ Result was successfully returned.
End If

在此示例中,通过使用对象的数字格式属性以格式化文本的形式返回数字。字符串返回带双引号。例如,如果数字格式为带尾部零、千分位和货币符号的固定小数点、两位有效数字的数值1.2K可能以字符串"$1,200.00"的形式返回。该数值以文本字符串的形式返回是因为: NumberAsText的属性设置为True。返回的该字符串为""$1,200.00"",结果字符串中有两个额外的双引号。该特性由 StringQuotes属性(0 = 无引号、1 = '单引号'、2 = "双引号")控制。

检索多维结果

在描述如何获取表格对象(具有一个或者多个维度的数组)结果之前,让我们剪短地讨论一下Analytica中的表格概念模型。

一个Analytica表格包含以下几个部分:

  • 索引:每个索引标识表格的一个维度
  • 表格单元中的值
  • 索引标签:标识每个单元格的坐标

索引数量确定表格的维数。例如,如果一个表格包含两个索引,那么表示该表格是二维的.

索引中元素的数量确定表格中实际单元的数量。假设表格TIJ连个索引组成。如果I包含5个元素(AABBCCDDEE),J包含3个元素(ABC),那么T是一个5x3或者3x5的表格,由你的视图确定。

在使用ADE时确定一个表格的视图是非常重要的。由你决定告诉ADE你想如何查看表格。例如,在上面的段落中,如果你告诉ADE先使用索引I,再使用索引J,那么元素2,3由位置I=B, J=CC确定。然而,如果你先使用J,再使用索引I,那么元素2,3由位置I=C, J=BB(注意ADE中的表格是从1开始的1,也就是说,每一个维度从1到NN是索引的大小)确定。通过下面所描述的调用 SetIndexOrder的方法,你可以设置表格中索引的顺序,这样你可以以任何你想要的方式查看表格。

在如何参考表格中单个元素方面,ADE方法是非常柔性的。你可以通过元素位置或者标它们的签名称来参考单个元素。例如,如果你告诉ADE给予你在位置2,1(沿着第一个索引的第2行或列,沿着第二个索引的第1行或列)上的元素,或者你可以告诉ADE给予你由‘BB’、’A’描述的元素,其中‘BB’和‘A’是它们在各自索引中的标签名。这些事物类型中经常用到这些方法的有如下描述的( GetDataByElements GetDataByLabels)。

如前一部分讨论所示, Result ResultType用来计算并获取一个对象的结果。但是,对于计算结果为多维的对象,使用 Result通常不方便,因为输出可能是一个如下形式的很长的以调号隔开的字符串:

Table Index1...IndexN [Value1, Value2...]

这里,Index1IndexN是表格的索引,Value1ValueN是表格(逐行填充)中的值。因此,如果我们想通过使用 Result方法来获取表格中的某一个特定元素,我们必须解析由 Result返回的以逗号隔开的字符串,这样才能获得想要的元素。不幸地是,ADE提供了一个CATable 类型的ADE对象,包含了可以简化表格处理的方法。

计算并获取结果为表格的对象结果

使用 CAObject ResultType方法:

Dim Obj As CAObject = ADE. GetObjectByName("MultiDimObject")
Dim TableResult As CATable = Obj. ResultTable
If Not (TableResult Is Nothing) Then
’Result table was successfully retrieved
Else
’An error occurred
End If

CAObject ResultTable方法返回CATable类型的一个自动化对象。CATable包含各种允许你设置、检索和处理单个表格元素的方法。很有可能,在检索完CATable对象后第一件事就是去设定结果表中索引的顺序。

解析和计算任意表达式

使用 CAObject Evaluate

Dim Obj As CAObject = ADE. GetObjectByName("ContextObject")
Obj. ResultType = 2 ’ Sample
Dim TableResult As CATable = Obj. Evaluate("Normal(X,Y^2) / Z")
If ADE. ErrorCode <> 0 Then
’An error occurred
Else
’Evaluation successful
End If

如果要使用 Evaluate,你必须先获得一个 CAObject实例。虽然你计算的表达式可能和任何具体对象没关系,但是 CAObject有几个用途。第一,对象的 ResultType属性提供了一个位置指明你想计算的结果类型。第二,如果你想使用NumberAsText显示风格,和指示的对象存储的数字格式确定数字如何被格式化。但是,通常你使用的对象是不重要要的;你可能甚至使用顶层模型对象作为你的文本对象。

在比较前面的两个例子的同时也说明了通常有两种方式检测失败原因。如果在一种方法计算过程中国出错,那么 ErrorCode属性为非零。并且对于任何方法,如果失败的话,返回值是Nothing或者False。

设定 CATable对象的索引顺序

使用 SetIndexOrder方法:

Dim Obj As CAObject
Dim TableResult As CATable
Dim IndexOrder (2) as String
Set Obj = ADE. GetObjectByName ("MultiDimObject")
Set TableResult = Obj. ResultTable
If Not (TableResult Is Nothing) Then
’Result table was successfully retrieved
IndexOrder (1) = "Index2"
IndexOrder (2) = "Index1"
If TableResult. SetIndexOrder(IndexOrder)Then
’Index Order set successfully
Else
’An error occurred in setting index order
End If
Else
’An error occurred
End If

上面的代码假设我们正在处理一个二维表格。我们设定该表格的索引顺序,以便Index2为第一个索引,Index1为第二个索引。

在一些计算机语言中,一个数组的第一个元素被认为位置为0(从0开始),而另外一些语言中认为该位置为1(从1开始)。Analytica的 Slice函数和ADE方法都认为数组第一个元素位置为1。早期的Visual Basic版本是从1开始的,但是目前的Visual Basic版本和其它现代编程语言从0开始。在上面的示例中,通过如下方式声明和使用Visual Basic数组:

Dim IndexOrder(2) As String
IndexOrder (1) = "Index2"
IndexOrder (2) = "Index1"

在现代Visual Basic中,这些代码声明了一个位置从0到2这个范围内的数组——一个包含3个元素的数组。应为第一个元素没有被设置,它包含特殊值Empty。ADE可以识别要传递的数组到底是基于0(位置从0开始)还是基于1(位置从1开始)。因此,根据你的偏好,使用位置从0开始的版本同样一点问题没有,例如:

Dim IndexOrder(1) As String
IndexOrder (0) = "Index2"
IndexOrder (1) = "Index1"
ResultTable. SetIndexOrder(IndexOrder)

通过索引顺序检索表格中的元素

使用 GetDataByElements 方法:

Dim Obj As CAObject
Dim TableResult As CATable
Dim IndexOrder (2) As String
Dim Pos (2) As Integer
Dim Element
Obj = ADE. GetObjectByName("MultiDimObject")
TableResult = Obj. ResultTable
If Not (TableResult Is Nothing) Then
’Result table was successfully retrieved
IndexOrder (1) = "Index2"
IndexOrder (2) = "Index1"
RetValue = TableResult. SetIndexOrder (IndexOrder)
If RetValue = True Then
’Index Order set successfully
Pos (1) = 2
Pos (2) = 1
Element = TableResult. GetDataByElements (Pos)
If ADE. ErrorCode = 0 Then
’element retrieved successfully
Else
’an error occurred
End If
Else
’An error occurred in setting index order
End If
Else
’An error occurred
End If

该代码使用了 GetDataByElements检索Index2=2, Index1=1位置上的元素并将结果存储到该Element(元素)中。

通过索引标签检索表格中的元素

使用 GetDataByLabels 方法:

Dim Obj As CAObject
Dim TableResult As CATable
Dim IndexOrder (2) As String
Dim Pos (2) As String
Dim Element
Obj = ADE. GetObjectByName (“MultiDimObject”)
TableResult = Obj. ResultTable
If Not TableResult Is Nothing Then
’Result table was successfully retrieved
IndexOrder (1) = "Index2"
IndexOrder (2) = "Index1"
If TableResult. SetIndexOrder(IndexOrder) Then
’Index Order set successfully
Pos (1) = "SomeLabelInIndex2"
Pos (2) = "SomeLabelInIndex1"
Element = TableResult. GetDataByLabels (Pos)
If ADE. ErrorCode = 0 Then
’element retrieved successfully
Else
’an error occurred
End If
Else
’An error occurred in setting index order
End If
Else
’An error occurred
End If

上面的代码使用了 GetDataByLabels检索Index2="SomeLabelInIndex2",Index1="SomeLabelInIndex1"位置上的元素,并将结果存储到Element(元素)中。

控制由GetDataByLabels、GetDataByElements、AtomicValue,或者GetSafeArray方法获得的元素格式

设定CATable的RenderingStyle 属性:

Dim TableResult as CATable = Obj. ResultTable
Dim rs As CARenderingStyle = TableResult. RenderingStyle
rs.NumberAsText = True
rs. FullPrecision = True
rs. UndefValue = ""
rs. StringQuotes = 1 Dim Element
If TableResult. SetIndexOrder(Split("Index2;Index1",";")) Then
Element = TableResult. GetDataByLabels( _
Split("SomeLabel1,SomeLabel2”,”,”))
If ADE. ErrorCode=0 Then
’ Element retrieved successfully
End If
End If

/tt>

通过一次调用检索一个完整的Visual Basic或者.NET数组表格

使用GetSafeArray 方法:

Dim Obj As CAObject
Dim TableResult As CATable
Dim IndexOrder (2) as String
Dim Pos (2) As Integer
Dim TheWholeTable
Obj = ADE. GetObjectByName ("MultiDimObject")
TableResult = Obj. ResultTable
If Not (TableResult Is Nothing) Then
’Result table was successfully retrieved
IndexOrder (1) = "Index2"
IndexOrder (2) = "Index1"
If TableResult. SetIndexOrder(IndexOrder) Then
’Index Order set successfully
TheWholeTable = TableResult. GetSafeArray
If ADE.ErrorCode = 0 Then
’table retrieved successfully
Else
’an error occurred
End If
Else
’An error occurred in setting index order
End If
Else
’An error occurred
End If

上面的代码使用了 GetSafeArray将整个表格存储到TheWholeTable中。由 GetSafeArray 返回的与该表格关联的每一个维度的元素被从1N,其中N是维度的长度。通过在调用 GetSafeArray之前使用该代码可以将安全数组的下届改成0:tt>

TableResult. RenderingStyle.SafeArrayLowerBound = 0

对于在C#中读取一个.NET数组中的一个多维结果的语法值得提一下:

Array theWholeTable = (Array) tableResult.GetSafeArray( );

确定表格的维数

使用 NumDims属性:

NumDimensions = ADE. Get("MultiDimObject"). ResultTable. NumDims

获取与表格关联的索引名称

使用 IndexNames方法:

Dim CurIndexName As String
Dim Obj As CAObject = ADE. GetObjectByName ("MultiDimObject")
Dim TableResult As CATable = Obj. ResultTable
Dim NumDimensions As integer = TableResult. NumDims
Dim I as Integer
For I = 1 To NumDimensions
CurIndexName = TableResult. IndexNames(I)
MsgBox “Current index is “ & CurIndexName
Next I

IndexNames方法按照 SetIndexOrder中指定的顺序返回表格中索引名称。如果对于 CATable SetIndexOrder没有预先设置,那么将返回默认的索引顺序。

将CAIndex对象与你的表格关联

使用 CATable GetIndexObject方法:

Dim Obj As CAObject = ADE. GetObjectByName ("MultiDimObject")
Dim Res As CATable = Obj. ResultTable
Dim NumDimensions As Integer = Res. NumDims
Dim CurIndexName As String = Res. IndexNames(NumDimensions)
Dim IndexObj As CAIndex = Res. GetIndexObject(CurIndexName)

上面的示例检索了最后的 CAIndex对象. CAIndex对象提供了属性和方法允许你获得关于单个索引的信息。

获取索引中元素的数量

使用 IndexElements 属性:

Dim Obj As CAObject = ADE. GetObjectByName ("MultiDimObject")
Dim Res As CATable = Obj. ResultTable
Dim NumDimensions As Integer = Res. NumDims
Dim CurIndexName As String = Res. IndexNames (NumDimensions)
Dim IndexObj As CAIndex = Res. GetIndexObject(CurIndexName)
Dim NumElsInIndex As Integer = IndexObj. IndexElements

获取索引中指定位置的索引标签

使用 GetValueByNumber 方法:

Dim I As Integer
Dim Obj As CAObject = ADE. GetObjectByName("MultiDimObject")
Dim Res As CATable = Obj. ResultTable
Dim NumDimensions As Integer = Res. NumDims
Dim CurIndexName As String = Res. IndexNames(NumDimensions)
Dim IndexObj As CAIndex = Res. GetIndexObject (CurIndexName)
Dim Str As String = "The elements in the index are: " & vbCrLf
For I=1 To IndexObj. IndexElements
Str = Str & IndexObj. GetValueByNumber(I) & " "
Next I
MsgBox Str

获取一个索引中索引标签的位置

使用 GetNumberByValue 方法:

Dim Obj As CAObject = ADE. GetObjectByName ("MultiDimObject")
Dim Res As CATable = Obj. ResultTable
Dim IndexName As String = Res. IndexNames(Res.NumDims)
Dim IndexObj As CAIndex = Res. GetIndexObject(IndexName)
Dim IndexPosition As Integer
IndexPosition = IndexObj. GetNumberByValue("SomeIndexLabel")
If ADE. ErrorCode = 0 Then
’the index position was successfully retrieved
Else
’an error occurred
End If

在一个零维度数组中获取标量值

Dim Res As CATable = ADE. Get("SomeObject"). ResultTable
Dim x As Object
If Res. NumDims = 0 Then
x = Res. AtomicValue
Else
’ Handle the array case.
End If

有时候,不可能提前知道一个对象的求值结果是返回一个多维结果还是一个标量。在此情况下,使用 ResultTable。如果结果是一个标量, NumDims将返回0。在此情况下,所谓的“数组”其实根本不是一个数组,而是包含一个单一基元值。在调用 CATable::Slice或者 CATable::Subscript方法后,结果可能是一个零维度数组。要获取其基元值,使用 CATable::AtomicValue方法即可。

Slice Subscript运算中减少维数

Dim PandL As CATable = ADE. Get("P_n_L_Statement"). ResultTable
Dim CatIndex As CAIndex = PandL. GetIndexObject("Categories")
Dim Expenses As CATable = PandL. Subscript(CatIndex,"Expenses")
Dim Year As CAIndex = Expenses. GetIndexObject("Year")
Dim InitialExpense
InitialExpense = Expenses. Slice(Year,1). AtomicValue

CATable Slice Subscript方法返回一个维数减1的新 CATable对象。这些方法与Analytica内嵌的Slice和Subscript函数相似。 Slice返回沿给定维度的第N个切片(通过位置)。 Subscript返回与指定索引值相应的切片。

在表格中建立表格和设定值

我们可以将上面所描述的相同方法应用于definition tables(定义表格)以便从结果表中检索值。一个定义表格,顾名思义,就是当一个对象的定义是一个 Table函数的时候(在Analytica中又称为编辑表)。

Analytica变量的值(通过 ResultTable访问)可以是一个数组——不是因为它是由一个定义表格定义的,而只因为被定义为一个返回一个数组值的表达式或者函数。

在使用编辑表时,你需要注意你是将常规的表达式传递给每个表格单元,还是只是传递字符串。 RenderingStyle.GeneralExpression属性确定你传送给表格的字符串值是如何被解译的。默认情况下, GeneralExpression=true ,表示如果你将一个单元值设定为字符串"Revenue",那么它实际上是由一个变量标识符而非文字字符串组成的表达式。如果你将文字常量植入到一个定义表格中(例如你有可能将一个输入表格放到你的模型中),你应该使用 RenderingStyle. GeneralExpression=false,或者记得在文字字符串值上预先考虑添加引号。

一个被定义为定义表格的对象没有必要在 ResultTable被调用时生成同样的表格。毕竟,定义表格可以定义为一个由标识符组成的数组。当 ResultTable被调用时,每一个标识符的结果被求值,同时产生了一个新的表不同)。如果标识符求值为数组,那么结果表可能比定义表格拥有更多维度。

获取被定义为 CATable的对象定义表格

使用 CAObject DefTable方法:

Dim Obj As CAObject
Dim TableDef As CATable
Obj = ADE. GetObjectByName("MultiDimObject")
TableDef = Obj. DefTable
If Not TableDef Is Nothing Then
’Definition table was successfully retrieved
Else
’An error occurred, or definition is not a table
End If

在检索到定义表格后,我们可以使用上面小节中描述过的所有相同方法 GetDataByElements GetDataByLabels SetIndexOrder等等,来检索表格中的元素,以及获取与表格中索引有关的信息。我们也可以使用我们上面使用过的确定对象结果是多维的还是标量的方法来确认对象的定义是表格还是标量:

Dim Obj As Object
Dim TableDefinition As Object
Dim ScalarDefinition
Obj = ADE. GetObjectByName ("SomeObject")
TableDefinition = Obj. DefTable
If TableDefinition Is Nothing Then
ScalarDefinition = Obj. GetAttribute (" definition")
If ADE. ErrorCode = 0 Then
’you have a scalar definition
Else
’an error occurred
End If
Else
’you have a table definition
End If

根据索引顺序设定表格中的元素

使用 CATable SetDataByElements 方法:

Dim Obj As CAObject
Dim TableDef As CATable
Dim IndexOrder (2) As String
Dim Pos (2) As Integer
Dim Element
Obj = ADE. GetObjectByName ("MultiDimObject")
TableDef = Obj. DefTable
If Not TableDef Is Nothing Then
’Definition table was successfully retrieved
IndexOrder (1) = “Index2”
IndexOrder (2) = “Index1”
If TableDef. SetIndexOrder (IndexOrder) Then
’Index Order set successfully
Pos (1) = 2
Pos (2) = 1
Element = "'ABC'" ’ Notice the extra quotes
If TableDef. SetDataByElements(Element,Pos) Then
’element successfully set
If TableDef. Update Then
’model successfully updated
Else
’error updating def in model
End If
Else
’an error occurred
End If
Else
’An error occurred in setting index order
End If
Else
’An error occurred, or definition is scalar
End If

该代码使用 SetDataByElementsIndex2=2, Index1=1位置上的元素设定为Element。注意ABC引号的使用。这里,因为ABC是带单引号的,所以我们将字符串“ABC”放到表格中。如果我们反过来将Element设定为“ABC”,那么必须将表达式ABC置于表格中。在后一种情形中,ABC可能会是一个变量。如果标识符ABC在模型中不存在,那么在后一种情形中,当尝试设定元素时会发生错误。代码使用Update来更新带有新定义的模型。有一点需要注意:包含对象的模型直到Update被调用后才被更新。因此,如果Update没有被调用,并且依赖于此对象的节点的结果稍后才被计算的话,该对象的就定义将一直被使用。另一点需要注意的是:Update 函数对于结果表和定义表差别很大。对于结果表,Update 从指定对象再次检索结果。因此,对于通过使用 SetDataByElements SetDataByLabels做出的任何改变都将被重写。

根据索引标签设定表格中的元素

使用 CATable SetDataByLabels 方法:

Dim Obj As CAObject
Dim TabDef As CATable
Dim IndexOrder (2) as String
Dim Pos (2) as String
Dim Element Obj = ADE. GetObjectByName("MultiDimObject")
TabDef = Obj. DefTable
If Not TabDef Is Nothing Then
’Definition table was successfully retrieved
TabDef. RenderingStyle. GeneralExpression = False
IndexOrder (1) = "Index2"
IndexOrder (2) = "Index1"
If TabDef. SetIndexOrder (IndexOrder) Then
’Index Order set successfully
Pos (1) = "SomeLabelInIndex2"
Pos (2) = "SomeLabelInIndex1"
Element = "ABC"
If TabDef. SetDataByLabels(Element,Pos) Then
’element set successfully
If TabDef. UpdateThen
’model successfully updated
Else
’an error occurred
End If
Else
’an error occurred
End If
Else
’An error occurred in setting index order
End If
Else
’An error occurred, or definition is scalar
End If

上面的代码使用 SetDataByLabelsposition Index2="SomeLabelInIndex2", Index1="SomeLabelInIndex1"位置上的元素设定给Element

To set the whole table in one call

Use PutSafeArray and Update:

Dim Obj As CAObject
Dim TableResult, TableDef As CATable
Dim RetValue
Dim TheWholeTable
Obj = ADE.GetObjectByName("MultiDimObject")
TableDef = Obj.DefTable
TableResult = Obj.ResultTable
TheWholeTable = TableResult.GetSafeArray
‘make changes to TheWholeTable
RetValue = TableDef.PutSafeArray (TheWholeTable)
If RetValue = True Then
’table successfully put
RetValue = TableDef.Update
If RetValue = True Then
’model successfully updated
End If
End If

To create a whole table from scratch

Use CreateDefTable:

Dim Obj As CAObject
Dim RetValue
Dim IndexLabs (2) As String
Obj = ADE.CreateObject ("MyNewTable", "Variable")
IndexLabs (1) = "I"
IndexLabs (2) = "J"
RetValue = Obj.CreateDefTable (IndexLabs)
If RetValue = True Then
’a table indexed by I and J has successfully
’been created. We are assuming that I and J
’already exist
Else
’an error occurred when creating the table
End If

The code above created a definition table indexed by I and J. The table is dimensioned according to the size of I and J. All the cells in the table are initially set to 0. The user can then call DefTable, and then use SetIndexOrder, SetDataByElements, SetDataByLabels, PutSafeArray, and Update to put values into the table. Note that the function CreateDefTable is very rarely used in an ADE program. After all, it is much easier to create an object in Analytica than it is in ADE.

Adjusting How Values are Returned

Analytica models can contain several different data types for values in attributes or in the cells of a table or index. Data types include floating point numbers, textual strings, the special values Undefined and Null, references, and handles to other objects. When these data types are returned and ultimately mapped to data types in the programming language you are using, you might want or need to alter how the values are returned. You can do so using RenderingStyle.

The CAObject, CATable, and CAIndex objects all contain a property called RenderingStyle, which returns a CARenderingStyle object. Properties of the rendering style can be changed to alter how values are returned. You can also control whether safe arrays returned by Analytica are 1-based or 0-based. These settings impact CAObject::GetAttribute and CAObject::Result; they also impact CATable::GetDataByElements, CATable::GetDataByLabels, CATable::AtomicValue, CATable::GetSafeArray, and CAIndex::GetValueByNumber.

When transferring values to cells in a DefTable, you can also control whether the cells are populated by literal strings and values, or by general expressions. This is controlled by the GeneralExpression property of CARenderingStyle.

The DefaultRenderingStyle and DefaultDefTableRenderingStyle properties of CAEngine can be set once just after the CAEngine has been instantiated to set the rendering style globally. For example, if you always use zero-based arrays, this can be specified once.

Retrieving numeric values as numbers

obj.RenderingStyle.NumberAsText = False
Dim x As Double = obj.Result

Numeric values are returned as numbers by default, so unless NumberAsText is set to True at some point, there is no need to specify this explicitly.

Retrieving numeric values as formatted strings

Dim TableResult As CATable = Obj.Evaluate("1 / 3 * 10 ^ 6")
TableResult.RenderingStyle.NumberAsText = True
TableResult.RenderingStyle.FullPrecision = False
Dim s As String = TableResult.AtomicValue

The number format associated with Obj is used to format the numeric value. A suffix style with four digits returns “333.3K” for this example.

Retrieving numeric values as strings with no loss of precision

Dim TableResult As CATable = Obj.Evaluate("1 / 3 * 10 ^ 6")
TableResult.RenderingStyle.NumberAsText = True
TableResult.RenderingStyle.FullPrecision = True
Dim s As String = TableResult.AtomicValue

Analytica continues to use the number format associated with Obj, but the significant digits is increased to avoid any loss in precision. So, suffix, exponential, fixed point, and percent formats are not truncated. If a date, integer, or Boolean format is used, some truncation might still occur. In the example, the return value would be 333.333333333333.

Retrieving string results without quotation marks

tab.RenderingStyle.StringQuotes= 0

Retrieving string results with explicit quotation marks

tab.RenderingStyle.StringQuotes = 1 ’ for single quotes
tab.RenderingStyle.StringQuotes= 2 ’ for double quotes

Using a custom value for Undefined

ADE.DefaultRenderingStyle.UndefValue = ""

By default, the special value Undefined is returned as a the special Windows variant type Empty. There are some scripting languages that cannot deal with the Empty data type, so if you encounter this problem, you might want to change this value.

Setting the cells of a definition table to string values

defTab.RenderingStyle.GeneralExpression= False

defTab.SetDataByElements("A & B",inds)

In this example, the indicated table cell is set to the string value "A & B". When this table is evaluated, this cell’s result is a string containing the five characters "A & B".

Setting the cells of a definition table to expressions

defTab.RenderingStyle.GeneralExpression = True
defTab.SetDataByElements("A & B",inds)

Here the cell is set to the expression "A & B". When this table is evaluated, the variable named A and the variable B is evaluated, and their results are coerced to strings and concatenated by the & operator.

You can set table cells to literal strings with GeneralExpression=True, but you must embed explicit quotations marks in the expression. For example:

defTab.RenderingStyle.GeneralExpression= True
defTab.SetDataByElements("’A & B’",inds)

GeneralExpression=True by default.

Terminating an In-progress Computation

Methods such as CAObject::ResultTable, CAObject::Result, or CAObject::Evaluate can initiate lengthy evaluations that could tie up your application. Some models can take many minutes, or even several hours, to compute the result. Normally, your call to ResultTable does not return until the result is fully computed.

You might want to provide your end user with the capability to abort the current computation, such as when a "break" key or button is pressed. Alternatively, you might want to terminate the current computation if your application is suddenly being terminated while a lengthly computation is still processing, so that ADE also terminates.

To implement the ability to abort a computation, you need to have some form of multi-threading in your application. The thread that calls ADE is occupied waiting for ADE to return, so some other thread in your application must detect the request to terminate the computation. When this request is detected, the second thread communicates this request to ADE through a global system event object. You must obtain the information required to locate this global event object from ADE before you have started the length computation. Each CAEngine instance has its own event object with a unique global name, but the same event object applies to the same CAEngine instance for the entire lifetime of the CAEngine instance.

To obtain the event object (using Windows Platform SDK calls):

Dim abortEventName As String
Dim abortHandle as Handle ’ This needs to be global
ade.SendCommand("GetProcessInfo('Abort Event Object')")
abortEventName = ade.OutputBuffer
abortHandle = OpenEvent(EVENT_MODIFY_STATE, FALSE, abortEventName )
...
’ Launch the computation
tab = obj.ResultTable

In a separate thread that detects a request to terminate the computation, the following code sends that request to ADE (using Windows Platform SDK calls):

SetEvent(abortHandle)

Note that OpenEvent and SetEvent are Windows Platform SDK calls, not part of ADE. Also notice that the abortHandle variable must be a global that is accessible from both threads. However, the second thread does not require access to the ADE objects themselves. Alternatively, the abortEventName can be shared and the second thread can call OpenEvent.

When the event has been flagged, ADE backs out of the current computation and returns from the current method. A small delay might be experienced while ADE backs out of the current computation. When it has returned, the CAEngine.ErrorCode is set to 78 (“Computation aborted”). Your application might continue using the same ADE instance as it would have prior to the call that was aborted.

Instantiating CAEngine using CALicense

To start using ADE, the first thing you need to do is instantate a CAEngine object. You have the option of instantiating your CAEngine object directly, or you can instantiate a CALicense object first and use it to instantiate your CAEngine object. When you’ve successfully obtained a CAEngine object, it doesn’t matter which of these two methods you used to get it. The difference is that if you use a CALicense object to instantiate your CAEngine, more information about why it failed is available. In particular, if the failure is due to a license code problem, your application has an opportunity to handle the error gracefully.

If you are creating an ADE-based application that are to be redistributed to many end users, who would otherwise not have an ADE license, you need to make arrangements with Lumina to license ADE for distribution with your application. This form of licensing requires your application to provide a special type of license code to ADE, called an application license code, since your end users usually do not have a license code of ADE on their computer. Note that a license to redistribute ADE in this fashion is not included as part of the standard ADE license agreement, and additional license fees apply. To use an application license code, you must use a CALicense to instantiate your CAEngine, since your application uses the CALicense object to supply the license code to Analytica.

The steps for instantiating a CAEngine using a CALicense are as follows, e.g.,:

' In VB
Imports ADE
...
Dim license As CALicense = New CALicense
Dim ade As CAEngine = license.NewCAEngine
If (ade Is Nothing) Then
' substitute error handling routine here
ReportError(license.ErrorText)
End If
// In C#
Using ADE;
...
CALicense license = new CALicense();
CAEngine ade = license.NewCAEngine();
if (ade==null) {
ReportError( license.ErrorText );
}

If you have an application license code, insert a call to SetApplicationLicenseCode:

license = new CALicense
license.SetApplicationLicenseCode("1234-5678-ABCD-EFGH-IJKL")
ade = license..NewCAEngine

Using the Analytica Graphing Engine

When you have a CATable result with at least one dimension, you can obtain a graph of the result as an image. One use of this is to embed graphs as JPEG images in a web page that uses ADE on the back end.

Obtaining the graph of a result requires the following steps:

  1. Select the appropriate graph settings, such as chart type, axis range settings, colors, fonts, and so on. The easiest way is to open the model in Analytica Enterprise, and select the settings you want for each variable using the Graph Setup dialog.
    The graph template you create using the Graph Setup dialog is stored in the GraphSetup attribute of the object. You can copy the GraphSetup attribute from an existing variable if you need to change the style template.
  2. From ADE, obtain a CATable with the result to be graphed.
  3. Set the GraphWidth and GraphHeight properties of the CATable object to indicate the desired size of the graph in pixels.
  4. If your result has more than two dimensions, use one of the following options:
    • call Slice or Subscript to reduce the dimensionality to the desired dimensionality for the plot (usually one dimension if there is no key or two dimensions if there is a key).
      • If you have more than one dimension, call SetIndexOrder to select the desired pivot for the graph.
    or
    • set GraphWithStoredPivot to true. Use this option to produce the same slice and pivot as seen when viewed in desktop Analytica.
  5. If you are sending the graph to an output stream, obtain a Windows IStream interface to the stream. If you have a .NET Stream (System.io.Stream), you need to use a wrapper class (see below).
  6. Call either the GraphToStream or GraphToFile methods of CATable, depending on where you want the graph written to. The graph can be created in different MIME types (e.g., image/ JPEG).

If you are able to view the result graph in Analytica with no slicers, then steps 4 and 5 are unnecessary.

Writing a result graph to a file:

Dim res As CATable = obj.ResultTable
res.GraphWidth = 640
res.GraphHeight = 400
If res.GraphToFile("C:\Temp\Result.JPG","image/JPEG") Then
’ success
End If

Dynamically generating a result graph from an ASP.NET web page

<%
Response.ContentType = "image/JPEG"
Dim varName As String = Request.QueryString("var")
Dim ADE As CAEngine = Session("ADE") ’ assume existing session
Dim res As CATable = ADE.Get(varName).ResultTable
res.GraphWidth = 640
res.GraphHeight = 400
Dim stream As StreamConnector = _ new StreamConnector( Response.OutputStream)
If res.GraphToStream( stream, "image/JPEG") Then
’ success
End If
%>

In this example code, Response, Request, and Session are Active Server Page objects. The HTTP in the client browser would contain a tag like this:

<img src="http://www.acme.com/Myapp/graph.aspx?var=Profit">

When Microsoft introduced .NET, they did not make the base Stream class interface in .NET compatible with the IStream interface in Windows. Because of this, it is necessary to create a stream wrapper that implements the IStream interface around the .NET Stream before passing it to GraphToStream. This wrapper, class StreamConnector, is included in the example AdeTest. To use the example above, add the file StreamConnector.vb to your project.

See Also

ADE User Guide Navigation
Using the Analytica Decision Engine Server << Previous Working with Models, Modules, and Files in ADE Next >> ADE Server Class Reference
Comments


You are not allowed to post comments.