Castor:为具有抽象属性的类创建映射文件

发布于 2024-08-24 04:53:23 字数 1125 浏览 2 评论 0原文

我有这个类:

public class Source extends Node {
  protected DistributionSampler delay ;
  protected DistributionSampler batchsize ;


/**
 * @param name The name of the source node
 * @param d The {@link DistributionSampler} used to generate the
 *          inter-arrival times
*/
  public Source( String name, DistributionSampler d ) {
    super( name ) ;
    delay = d ;
    batchsize = new Deterministic( 1 ) ;
    Sim.schedule( new Arrival( Sim.now() + delay.next() ) ) ;
  }

/**
 * @param name The name of the source node
 * @param d The {@link DistributionSampler} used to generate the
 *          inter-arrival times
 * @param b The {@link DistributionSampler} used to generate the
            batch sizes
*/
  public Source( String name, DistributionSampler d, DistributionSampler b ) {
    super( name ) ;
    delay = d ;
    batchsize = b ;
    Sim.schedule( new Arrival( Sim.now() + delay.next() ) ) ;
  }

   ....
}

DistributionSampler 是一个 AbstractClass。

在从 XML 转换为 Java 对象时,我将知道要使用抽象类的哪个具体实现(通过 bean 名称)。

但是,我不完全确定如何编写映射文件来告诉 Castor 如何进行翻译。

任何帮助将不胜感激。

I have this class:

public class Source extends Node {
  protected DistributionSampler delay ;
  protected DistributionSampler batchsize ;


/**
 * @param name The name of the source node
 * @param d The {@link DistributionSampler} used to generate the
 *          inter-arrival times
*/
  public Source( String name, DistributionSampler d ) {
    super( name ) ;
    delay = d ;
    batchsize = new Deterministic( 1 ) ;
    Sim.schedule( new Arrival( Sim.now() + delay.next() ) ) ;
  }

/**
 * @param name The name of the source node
 * @param d The {@link DistributionSampler} used to generate the
 *          inter-arrival times
 * @param b The {@link DistributionSampler} used to generate the
            batch sizes
*/
  public Source( String name, DistributionSampler d, DistributionSampler b ) {
    super( name ) ;
    delay = d ;
    batchsize = b ;
    Sim.schedule( new Arrival( Sim.now() + delay.next() ) ) ;
  }

   ....
}

DistributionSampler is an AbstractClass.

At the time of conversion from XML to Java Object, I will know which concrete implementation of my abstract class to use (via the bean name).

However, I'm not entirely sure how to write the mapping file to tell castor how to do the translation.

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-08-31 04:53:23
<class name="network.Source">
        <description xmlns="">
            Default mapping for class network.Source
        </description>

        <map-to xml="Source"/>

        <field name="name" type="java.lang.String" required="true">
            <bind-xml  node="element" />
        </field>

        <field name="delay" type="tools.DistributionSampler" required="true" set-method="initialiseDelay" get-method="getDelay">
            <bind-xml  auto-naming="deriveByClass" node="element" location="delay"/>
        </field>

        <field name="batchSize" type="tools.DistributionSampler">
            <bind-xml  auto-naming="deriveByClass" node="element" location="batchSize"/>
        </field>
    </class>

auto-naming="deriveByClass" 部分意味着,如果我们发送它将嵌入延迟内的元素的节点名称绑定到它希望扩展 distributionSampler 的等效类。

因此,传递它的快乐来处理以下 xml:

<Source name="asd">
    <delay>
        <Deterministic time="234" />
    </delay>

    <batchSize>
        <Erlang K="234" Theta="234" />
    </batchSize>
</Source>

将使用确定性和 Erlang 的映射文件将其映射到扩展 DistributionSampler 的类实例。

<class name="network.Source">
        <description xmlns="">
            Default mapping for class network.Source
        </description>

        <map-to xml="Source"/>

        <field name="name" type="java.lang.String" required="true">
            <bind-xml  node="element" />
        </field>

        <field name="delay" type="tools.DistributionSampler" required="true" set-method="initialiseDelay" get-method="getDelay">
            <bind-xml  auto-naming="deriveByClass" node="element" location="delay"/>
        </field>

        <field name="batchSize" type="tools.DistributionSampler">
            <bind-xml  auto-naming="deriveByClass" node="element" location="batchSize"/>
        </field>
    </class>

The auto-naming="deriveByClass" part means that if we send it will bind node name for the element embedded inside delay to an equivalent class which it hopes extends distributionSampler.

So passing its happy to process the following xml:

<Source name="asd">
    <delay>
        <Deterministic time="234" />
    </delay>

    <batchSize>
        <Erlang K="234" Theta="234" />
    </batchSize>
</Source>

Will use the mapping files for Deterministic and Erlang to map it to the class instances whihc extend DistributionSampler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文