演習 2.3:ジョブデータエクスポータの実装
推定所要時間
ジョブデータエクスポータを実装する
説明:
5 分
演習:
15 分
説明
ジョブデータエクスポータは、インターフェイス IJobExporter を実装する必要があります。
setOutputDirectory - このメソッドは、ジョブエクスポータがジョブデータを書き込むディレクトリを設定します。
getOutputDirectory - setOutputDirectory に対応するゲッターメソッドです。
exportJobData(String elementElid, Properties jobParameters) - このメソッドは、指定されたデータベース要素とジョブパラメータに対するジョブデータを出力先ディレクトリに書き込みます。
インターフェイス IJobExporter を実装するよりも、すでに以下の基本サポートを提供している AbstractJobExporter クラスを拡張する方が便利な場合があります。
データとスクリプトを別々のサブディレクトリに編成
データエクスポートパラメータ
メタデータ (ジョブパラメータ、タイムスタンプなど) を含む index.xml ファイルの作成
図面およびモデルに関連するジョブの場合、以下のジョブデータエクスポータを拡張することもできます。
DrawingJobExporter
ModelingJobExporter
これらのジョブデータエクスポータ実装が使用するパラメータの詳細については、それぞれの API マニュアルを参照してください。プロットジョブ (PlotJobExporter) と干渉検出ジョブ (ClashJobExporter) には、より特化したジョブデータエクスポータが使用されます。
演習
<model name>_<model state> をファイル名として使用し、注釈作成済みのモデルをサーバ上のディレクトリに保管します。
1. 状態変更済みモデルのファイル名作成時に使用する LISP スクリプトに AnnotatedModelFileName プレースホルダを追加します。
2. ModelingJobExporter を拡張して、ジョブパラメータ AnnotatedModelFileName を提供します。
3. このジョブの XML 設定ファイルを更新して、新しいエクスポータを設定します。
前提条件
演習 2.2 の SimpleModelGeometryCheckJob.xml ファイルと SimpleModelGeometryCheckTemplate.lsp ファイルが必要です。
LISP スクリプトへの AnnotatedModelFileName プレースホルダの追加
SimpleModelGeometryCheckTemplate.lsp を編集して %AnnotatedModelFileName% プレースホルダを追加します。
(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 :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)
)
)
ModelingJobExporter クラスの拡張
ご使用の Java 開発環境で、ModelingJobExporter を拡張してジョブパラメータに AnnotatedModelFileName を提供できるようにした SimpleModelGeometryCheckJobExporter.java を作成します(ここをクリックして表示されるファイルからコードをコピーアンドペーストできます)。
package com.acme;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;
import com.osm.automation.ModelingJobExporter;
import com.osm.biz.WMSession;
import com.osm.datamgmt.biz.Model;
import com.osm.datamgmt.biz.VersionableDoc;
import com.osm.exception.WMException;
public class SimpleModelGeometryCheckJobExporter extends ModelingJobExporter {
public static final String ANNOTATED_MODEL_FILE_NAME_KEY = "AnnotatedModelFileName";
public SimpleModelGeometryCheckJobExporter() {
super("SimpleModelGeometryCheckTemplate.lsp", VersionableDoc.HIGHEST_REVISIONS);
}
@Override
protected void writeDataFiles(String modelElid, String modelLoadRule, Properties jobParameters)
throws IOException, WMException {
String annotatedModelFileName = null;
Model model = null;
try {
model = (Model) WMSession.getWMSession().openElement(modelElid, false);
annotatedModelFileName = MessageFormat.format("{0}_{1}", model.getName(), model.getState());
} finally {
WMSession.getWMSession().close(model);
}
jobParameters.put(ANNOTATED_MODEL_FILE_NAME_KEY, annotatedModelFileName);
super.writeDataFiles(modelElid, modelLoadRule, jobParameters);
}
}
* 
LISP からは、多くのモデル属性にもアクセスすることができます。本当に必要な場合以外は、カスタム Java クラスを作成しないでください。
ジョブの XML 設定ファイルの更新
ModelingJobExporter を新しい SimpleModelGeometryCheckJobExporter で置き換え、C:\taskagent ディレクトリに結果を保存するようジョブ結果ハンドラを設定します。
<?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.acme.SimpleModelGeometryCheckJobExporter">
<StartScriptTemplate>SimpleModelGeometryCheckTemplate.lsp</StartScriptTemplate>
<ModelLoadRule catalog="model" msg_num="728">Highest Revisions</ModelLoadRule>
</Exporter>
<ResultHandler java_class="com.osm.automation.SaveToFileSystemJobResultHandler">
<TargetDirectory>C:\taskagent</TargetDirectory>
</ResultHandler>
</Job>
ジョブの index.xml
ジョブエクスポータは、ジョブの実行に関する追加情報 (ジョブパラメータ) も index.xml ファイルに書き込みます。index.xml の情報をサーバ側で使用して、スクリプトとマクロ内にあるジョブパラメータのプレースホルダを実行前に置き換えます。
AbstractJobExporter クラスが作成するデフォルトの index.xml の構造は次のようになります。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<job type="modeling.simplegeometrycheck" version="20.7">
<element id="BY6ID3742VLI3N" timestamp="1192720161000"/>
<datadirectory/>
<scriptdirectory/>
<startscript>SimpleModelGeometryCheckTemplate.lsp</startscript>
<parameters>
<parameter key="parameter-name">parameter-value</parameter>
...
</parameters>
</job>
job 要素には、typeversion に関する属性があります。すでに説明したとおり、ジョブ実行のターゲット (Creo Elements/Direct Modeling または Creo Elements/Direct Drafting) は type (または type プレフィックス) によって決まります。element エントリの id (ELID) とタイムスタンプは、このジョブが参照する Creo Elements/Direct Manager Server データ要素を表しています。AbstractJobExporter クラスは、ジョブの実行に必要なすべてのデータとスクリプトを格納した 1 つの zip ファイルを作成します。XML タグ datadirectoryscriptdirectory は、zip ファイル構造内のデータおよびスクリプトのディレクトリの相対パス表現です。Startscript は、実行するメインのスクリプトまたはマクロを表します。parameters セクションには、ジョブタイプに応じたすべてのジョブパラメータが一覧表示されます。
ジョブ固有のジョブパラメータを持つモデル図形チェックジョブの index.xml の例は次のようになります。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<job type="modeling.simplegeometrycheck" version="20.7">
<element id="BY6ID3742VLI3N" timestamp="1192720161000"/>
<datadirectory/>
<scriptdirectory/>
<startscript>SimpleModelGeometryCheckTemplate.lsp</startscript>
<parameters>
<parameter key="AnnotatedModelFileName">Cylinder_work</parameter>
<parameter key="ModelFile">model.sdexp</parameter>
<parameter key="ModelLoadRule">AS_STORED</parameter>
<parameter key="ModelName">Cylinder</parameter>
<parameter key="StartScriptTemplate">SimpleModelGeometryCheckTemplate.lsp</parameter>
<parameter key="TargetDirectory">E:\taskagent</parameter>
</parameters>
</job>
ジョブパラメータ AnnotatedModelFileName は、SimpleModelGeometryCheckJobExporter によって導入されたジョブパラメータです。ジョブパラメータ ModelFileModelLoadRuleModelName、および StartScriptTemplate は、スーパークラス ModelingJobExporter によって提供されています。ジョブパラメータ TargetDirectory は、SaveToFileSystemJobResultHandler によって提供されています。
これは役に立ちましたか?