演習 2.5:ジョブオプションの実装
推定所要時間
ジョブオプションを追加する
説明:
5 分
演習:
15 分
説明
ジョブには、ジョブデータエクスポートまたはジョブ結果処理以外にジョブの実行に影響を与える追加パラメータが必要な場合があります。このために、XML 設定ファイル内で (任意の) Options セクションを使用して、IJobOptions インターフェイスの実装を指定できます。
インターフェイス IJobOptions を実装するよりも、以下のパラメータタイプをサポートしている JobOptions クラスを使用または拡張する方が便利な場合があります。
テキストパラメータ
<parameter-name type="text">default text
<DisplayName>My Parameter</DisplayName>
</parameter-name>
選択パラメータ
<parameter-name type="choice">value2
<ChoiceValue>value1
<DisplayName>Display Name 1</DisplayName>
</ChoiceValue>
<ChoiceValue>value2
<DisplayName>Display Name 2</DisplayName>
</ChoiceValue>
...
</parameter-name>
ブールパラメータ
<parameter-name type="boolean">true
<DisplayName>My Parameter</DisplayName>
</parameter-name>
演習
パラメータ CheckType によって Simple Model Geometry Check を拡張するには、以下のアクションが必要です。
1. SimpleModelGeometryCheckTemplate.lspCheckType パラメータを評価する。
CheckTypeMinimal または Maximal に設定できます。Minimal がデフォルト値です。
2. ジョブの XML 設定ファイル内で CheckType パラメータを設定する。
3. ジョブオプションを含めるよう、ジョブの xml 設定ファイルを更新する。
前提条件
演習 2.4 の SimpleModelGeometryCheckJob.xml ファイルと SimpleModelGeometryCheckTemplate.lsp ファイルが必要です。
* 
パラメータ名は、ジョブ設定ファイル内で一意でなければなりません。たとえば、ジョブオプションは、ジョブデータエクスポータのパラメータまたはジョブ結果ハンドラのパラメータと同じ名前のパラメータを持つことはできません。
CheckType パラメータの評価
SimpleModelGeometryCheckTemplate.lsp に、CheckType パラメータを評価する以下のコードを追加します。
(in-package :frame2)
;;-------------------------
;; utilities
;;-------------------------
(load "JobUtilities.lsp")
;;------------------------------------------------------------------------------
;; This is the routine that is called by the automation framework in Modeling.
;; When this routine is called, the current directory of Modeling is the one
;; where the input data can be found.
;;
;; parameter : <output-directory> directory for output data
;; return : <error-message> return value 'nil' ==> success
;;------------------------------------------------------------------------------
(defun run-job (output-directory)
(let* (
;; parameters provided by the job
(ann-model-file-name "%AnnotatedModelFileName%")
(check-type (parse-choice "%CheckType%"
'("Minimal" "Maximal")
'(:minimal_check :maximal_check)
:minimal_check))

;; the item to check
(part-to-check nil)

;; variables for XML serialization
(results-element (xml:xml-list-create-element "results"))
(xml-file (create-path output-directory "index.xml"))

;; variables for writing the output file
(file-zipname "model.pkg") ;; the real name of the result file in the ZIP archive
(file-path (create-path output-directory file-zipname)) ;; the path of the result file
(file-name (format nil "~A.pkg" (if (oli:sd-string= ann-model-file-name "") "AnnotatedModel" ann-model-file-name))) ;; the symbolic name of the result file
)
;;---------------------------------------------
;; load input & call the part checker
;;---------------------------------------------
(load-job-data "%ModelFile%")
(setf part-to-check (oli:sd-inq-obj-children (get-all-at-top)))
(k2-ui::check_part :objects part-to-check check-type :labels :on :keep_labels :on)
(collect-check-results (frame2:getres) results-element)
;; -------------------
;; write result data
;; -------------------
;; write the annotated model
(frame2::save-all-in-package file-path)
;; and make a corresponding entry in the index.xml
(xml:xml-list-add-child results-element (make-result-file-element "PKG" :file file-zipname :name file-name))
;;------------------------------------------------------------------------
;; write the index.xml
;; write-xml-to-list serializes the list into XML (see JobUtilities.lsp)
;;------------------------------------------------------------------------
(write-xml-list-to-file results-element xml-file)

;;------------------------------
;; return = nil means 'no error'
;;------------------------------
(get-automation-error)
) ; let
)
(defun collect-check-results (part-check-result results-element)
(let* (
(error-entries (cdr (assoc k2-ui::*body-check-error-string* part-check-result)))
(model-state (if (> (length error-entries) 0) "model_corrupt" "model_ok"))
)
(xml:xml-list-add-attribute results-element "modelcheck" model-state)
)
)
xml ファイルの <Options> セクションを読み込むための JobOptions の拡張
データベースから動的に値を取得するパラメータの場合 (図面の部品の一覧など)、XML 設定ファイルが不十分な場合があります。以下の例は、演習 2.3 で使用したのと同じ AnnotatedModelFileName パラメータの実装を示していますが、UI で編集可能なジョブオプションの一部として実装しています(ここをクリックして表示されるファイルから、コードを NetBeans にコピーアンドペーストできます)。
package com.acme;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.osm.automation.JobOptions;
import com.osm.biz.WMElement;
import com.osm.datamgmt.biz.Model;
import com.osm.ui.builder.descriptor.ParameterDescriptor;
import com.osm.ui.builder.descriptor.TextParameterDescriptor;
public class SimpleModelGeometryCheckJobOptions extends JobOptions {
private final FileNameJobParameterDescriptor filenameJPD;
public SimpleModelGeometryCheckJobOptions() {
super();
this.filenameJPD = new FileNameJobParameterDescriptor();
}
@Override
protected List<ParameterDescriptor> getDeclaredParameters() {
final ArrayList<ParameterDescriptor> jpdList = new ArrayList<ParameterDescriptor>();
jpdList.addAll(super.getDeclaredParameters());
jpdList.add(filenameJPD);
return jpdList;
}
private static class FileNameJobParameterDescriptor extends TextParameterDescriptor {
public static final String PARAMETER_NAME = "AnnotatedModelFileName";
public static final String DISPLAY_NAME = "Annotated Model File Name";
public FileNameJobParameterDescriptor() {
super(PARAMETER_NAME, DISPLAY_NAME, PARAMETER_NAME, true, true, true, null);
}
@Override
public void init(WMElement wmElement, Properties parameters) {
final String value = getValue(parameters);
if (value == null) {
if (getDefaultText() != null) {
setValue(parameters, getDefaultText());
} else if (wmElement instanceof Model) {
final Model model = (Model) wmElement;
try {
setValue(parameters, model.getName() + "_" + model.getState());
} catch (Exception e) {
setValue(parameters, "Annotated_Model");
}
}
}
}
}
}
ジョブの XML 設定ファイルの更新
ジョブオプションをジョブの xml 設定ファイルに追加します。
<?xml version="1.0" encoding="UTF-8"?>
<Job type="modeling.simplegeometrycheck" has_result_data="true">
<DisplayName>Simple Model Geometry Check</DisplayName>
<UI>
<ActionMenu wm_java_class="com.osm.datamgmt.biz.Model" include_derived="true">
<Name>Simple Model Geometry Check</Name>
<JobGroup>Model Checks</JobGroup>
<Action java_class="com.osm.automation.action.MultiSelectCreateJobScheduleAction">
<Description>Checks a model's geometry</Description>
</Action>
</ActionMenu>
</UI>
<Exporter java_class="com.osm.automation.ModelingJobExporter">
<StartScriptTemplate>SimpleModelGeometryCheckTemplate.lsp</StartScriptTemplate>
<ModelLoadRule catalog="model" msg_num="728">Highest Revisions</ModelLoadRule>
</Exporter>
<ResultHandler java_class="com.osm.automation.SaveToFileSystemJobResultHandler">
<DisplayName>Save annotated model to server directory</DisplayName>
<TargetDirectory>C:\taskagent</TargetDirectory>
</ResultHandler>
<Options java_class="com.acme.SimpleModelGeometryCheckJobOptions">
<CheckType type="choice" editable="true">Minimal
<DisplayName>Checks</DisplayName>
<ChoiceValue>Minimal</ChoiceValue>
<ChoiceValue>Maximal</ChoiceValue>
</CheckType>
</Options>
</Job>
* 
ジョブデータエクスポータ SimpleModelGeometryCheckJobExporter は、SimpleModelGeometryCheckJobOptions と一緒に使用できません。ジョブパラメータ名 AnnotationModelFileName が一意ではなくなるからです。
「タスクの作成」ダイアログボックスに、実装済みのジョブパラメータ AnnotationModelFileName と、xml 設定ファイル内で宣言された CheckType オプションの両方のジョブオプションが表示されるようになりました。
* 
可能であればいつでもカスタムパラメータを使用できます。一般的に、パラメータ実装は部品、プリンタリストなどの動的な値リストに必要です。
* 
Java でのジョブパラメータの実装に関するヘルプは、com.osm.automation.ui.descriptors.JobParameterDescriptor とその派生クラス (特に com.osm.automation.ui.descriptors パッケージに含まれるもの) の API マニュアルを参照してください。
これは役に立ちましたか?