`
barryzhong
  • 浏览: 20335 次
  • 性别: Icon_minigender_1
社区版块
存档分类

生命周期组件框架:生命周期描述语言——可继承状态机示例

阅读更多
    @StateMachine
    protected static interface CustomerLifecycleMeta {

        @StateSet
        static interface States {

            @Initial
            @Function(transition = CustomerLifecycleMeta.Transitions.Activate.class, value = { Active.class })
            static interface Draft {}
            @Functions({ @Function(transition = CustomerLifecycleMeta.Transitions.Suspend.class, value = Suspended.class),
                    @Function(transition = CustomerLifecycleMeta.Transitions.Cancel.class, value = Canceled.class) })
            static interface Active {}
            @Function(transition = CustomerLifecycleMeta.Transitions.Resume.class, value = Active.class)
            static interface Suspended {}
            @End
            static interface Canceled {}
        }
        @TransitionSet
        static interface Transitions {

            static interface Activate {}
            static interface Suspend {}
            static interface Resume {}
            static interface Cancel {}
        }
    }
   
    @StateMachine
    protected static interface InternetServiceLifecycleMeta {

        @StateSet
        static interface States {

            @Initial
            @Function(transition = InternetServiceLifecycleMeta.Transitions.Start.class, value = { InternetServiceLifecycleMeta.States.InService.class })
            @ValidWhile(on = { CustomerLifecycleMeta.States.Active.class }, relation = InternetServiceLifecycleMeta.Relations.CustomerRelation.class)
            static interface New {}
            @Function(transition = InternetServiceLifecycleMeta.Transitions.End.class, value = { InternetServiceLifecycleMeta.States.Ended.class })
            // @InboundWhile(on = { CustomerLifecycleMeta.States.Active.class },
            // relation =
            // InternetServiceLifecycleMeta.Relations.CustomerRelation.class)
            static interface InService {}
            @End
            static interface Ended {}
        }
        @TransitionSet
        static interface Transitions {

            static interface Start {}
            static interface End {}
        }
        @RelationSet
        static interface Relations {

            @RelateTo(value = CustomerLifecycleMeta.class)
            static interface CustomerRelation {}
        }
    }
    @StateMachine
    protected static interface ServiceProviderLifecycle {

        @StateSet
        static interface States {

            @Initial
            @Function(transition = Transitions.Shutdown.class, value = Closed.class)
            static interface ServiceAvailable {}
            @End
            static interface Closed {}
        }
        @TransitionSet
        static interface Transitions {

            static interface Shutdown {}
        }
    }
    @StateMachine
    protected static interface InternetTVServiceLifecycle extends InternetServiceLifecycleMeta {

        @StateSet
        static interface States extends InternetServiceLifecycleMeta.States {

            @ValidWhile(relation = TVProvider.class, on = ServiceAvailable.class)
            static interface New extends InternetServiceLifecycleMeta.States.New {}
        }
        @RelationSet
        static interface Relations extends InternetServiceLifecycleMeta.Relations {

            @RelateTo(InternetTVProviderLifecycle.class)
            static interface TVProvider {}
        }
    }
    @StateMachine
    protected static interface InternetTVProviderLifecycle extends ServiceProviderLifecycle {}

 

    @LifecycleMeta(CustomerLifecycleMeta.class)
    public static class Customer extends ReactiveObject {

        protected Customer() {
            initialState(Draft.class.getSimpleName());
        }

        @Transition
        public void activate() {}

        @Transition
        public void suspend() {}

        @Transition
        public void resume() {}

        @Transition
        public void cancel() {}
    }

    @LifecycleMeta(InternetServiceLifecycleMeta.class)
    public class InternetServiceOrder extends ReactiveObject {

        private Date startDate;
        private Date endDate;
        @Relation(InternetServiceLifecycleMeta.Relations.CustomerRelation.class)
        private Customer customer;
        private String type;

        public InternetServiceOrder() {
            initialState(InternetServiceLifecycleMeta.States.New.class.getSimpleName());
        }

        public InternetServiceOrder(Date startDate, Date endDate, Customer customer, String type) {
            super();
            this.startDate = startDate;
            this.endDate = endDate;
            this.customer = customer;
            this.type = type;
            initialState(InternetServiceLifecycleMeta.States.New.class.getSimpleName());
        }

        @Transition
        public void start() {}

        @Transition
        public void end() {}

        public void setStartDate(Date startDate) {
            this.startDate = startDate;
        }

        public void setEndDate(Date endDate) {
            this.endDate = endDate;
        }

        public void setCustomer(Customer customer) {
            this.customer = customer;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Date getStartDate() {
            return startDate;
        }

        public Date getEndDate() {
            return endDate;
        }

        public Customer getCustomer() {
            return customer;
        }

        public String getType() {
            return type;
        }
    }

 

    @LifecycleMeta(InternetServiceLifecycleMeta.class)
    public static class BaseService<T extends BaseServiceProvider> extends ReactiveObject {

        private Customer customer;

        public BaseService(Customer customer) {
            initialState(InternetServiceLifecycleMeta.States.New.class.getSimpleName());
            this.customer = customer;
        }

        private T provider;

        public T getProvider() {
            return provider;
        }

        public void setProvider(T provider) {
            this.provider = provider;
        }

        @Relation(InternetServiceLifecycleMeta.Relations.CustomerRelation.class)
        public Customer getCustomer() {
            return customer;
        }

        public void setCustomer(Customer customer) {
            this.customer = customer;
        }

        @Transition
        void start() {}

        @Transition
        void end() {}
    }
    @LifecycleMeta(ServiceProviderLifecycle.class)
    public static class BaseServiceProvider extends ReactiveObject {

        public BaseServiceProvider() {
            initialState(ServiceProviderLifecycle.States.ServiceAvailable.class.getSimpleName());
        }

        @Transition
        void shutdown() {}
    }

    @LifecycleMeta(InternetTVServiceLifecycle.class)
    public static class InternetTVService extends BaseService<InternetTVServiceProvider> {

        public InternetTVService(Customer customer) {
            super(customer);
        }

        @Relation(InternetTVServiceLifecycle.Relations.TVProvider.class)
        public InternetTVServiceProvider getProvider() {
            return super.getProvider();
        }
    }

    @LifecycleMeta(InternetTVProviderLifecycle.class)
    public static class InternetTVServiceProvider extends BaseServiceProvider {}

 

    @Test
    public void test_inherited_valid_while_relation_validation() {
        final InternetTVServiceProvider provider = new InternetTVServiceProvider();
        assertEquals(InternetTVProviderLifecycle.States.ServiceAvailable.class.getSimpleName(), provider.getState());
        Customer customer = new Customer();
        customer.activate();
        assertEquals(CustomerLifecycleMeta.States.Active.class.getSimpleName(), customer.getState());
        final InternetTVService service = new InternetTVService(customer);
        service.setProvider(provider);
        service.start();
        assertEquals(InternetServiceLifecycleMeta.States.InService.class.getSimpleName(), service.getState());
    }

    @Test(expected = LifecycleException.class)
    public void test_inherited_valid_while_relation_validation_negative_with_self_valid_while() throws LifecycleException {
        final InternetTVServiceProvider provider = new InternetTVServiceProvider();
        assertEquals(InternetTVProviderLifecycle.States.ServiceAvailable.class.getSimpleName(), provider.getState());
        provider.shutdown();
        assertEquals(InternetTVProviderLifecycle.States.Closed.class.getSimpleName(), provider.getState());
        final Customer customer = new Customer();
        customer.activate();
        assertEquals(CustomerLifecycleMeta.States.Active.class.getSimpleName(), customer.getState());
        final InternetTVService service = new InternetTVService(customer);
        service.setProvider(provider);
        try {
            service.start();
        } catch (LifecycleException e) {
            assertInvalidStateErrorByValidWhile(e, provider, service, VOIPProviderLifecycleMeta.States.ServiceAvailable.class);
        }
    }

 前文:生命周期组件框架——关系型状态及服务

分享到:
评论

相关推荐

    ASP.NET的网页代码模型及生命周期

    第4章 ASP.NET的网页代码模型及生命周期 从本章开始,就进入了ASP.NET应用程序开发的世界。在了解了C#的结构,以及面向对象的概念后,就可以从面向对象的思想开发ASP.NET应用程序。在ASP.NET中,能够使用面向对象的...

    wpf源码大全 精通C#3.0图书源码 详细源码 学习好用

    SimpleStateMachine 简单的状态机示例程序。 WorkflowruntimeEvent 工作流运行时引擎事件。 第20章(\C20) 示例描述:本章介绍WF的标准活动。 CAGDemo ConditionedActivityGroup活动使用示例。 ...

    UML和模式应用(架构师必备).part01.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    UML和模式应用(架构师必备).part07.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    UML和模式应用(架构师必备).part02.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    UML和模式应用(架构师必备).part06.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    UML和模式应用(架构师必备).part03.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    UML和模式应用(架构师必备).part04.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    UML和模式应用(架构师必备).part08.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    UML和模式应用(架构师必备).part05.rar

    29.5 示例:使用状态机进行UI导航建模 29.6 示例:NextGen用例的状态机图 29.7 过程:UP中的状态机图 29.8 推荐资源 第30章 用例关联 30.1 包含关系 30.2 术语:具体用例、抽象用例、基础用例和附加用例 30.3...

    workflow:对象工作流程以处理对象生命周期

    这是描述ATM的状态变化的状态机图: 可以在文件夹“ tests \ ATMStateMachine.php”中找到示例代码。 作曲家 Pluf Workflow已部署到pakcagest(php composer存储库)存储库,因此您只需向composer.json添加以下...

    Java开发技术大全 电子版

    3.2.4对象的生命周期106 3.2.5对象的创建106 3.2.6对象的使用108 3.2.7对象的释放和垃圾收集机制108 3.3成员变量的定义与使用109 3.3.1成员变量的定义109 3.3.2成员变量的访问权限110 3.3.3实例成员变量和...

    C#微软培训资料

    C#语言在.NET 框架中的作用及其特性 1.1 Microsoft.NET 一场新的革命 1.1.1 什么是.NET 2000 年 6 月 22 日 不论对 Microsoft 还是对整个 IT 业界都将成为值得纪念的一天 这一天 微软公司正式推出...

    Kamera:使用Android Kotlin编写的示例Android应用程序,用于展示OpenGL相机预览显示的自定义实现

    卡梅拉 用于展示OpenGL相机预览显示的自定义实现的示例Android应用... 活动生命周期和Surface生命周期不匹配,因此需要将它们都考虑在内。 Android体系结构组件LifecycleObserver用于在一个类中处理所有事件。 Grafika

    精通ANDROID 3(中文版)1/2

    2.7 了解应用程序生命周期  2.8 调试应用程序  2.8.1 启动模拟器  2.8.2 StrictMode  2.8.3 参考资料  2.9 小结  第3章 使用Android资源  3.1 资源  3.1.1 字符串资源  3.1.2 布局资源  3.1.3 ...

    精通Android 3 (中文版)2/2

    2.7 了解应用程序生命周期  2.8 调试应用程序  2.8.1 启动模拟器  2.8.2 StrictMode  2.8.3 参考资料  2.9 小结  第3章 使用Android资源  3.1 资源  3.1.1 字符串资源  3.1.2 布局资源  3.1.3 ...

    亮剑.NET深入体验与实战精要2

    3.1.2 具有Master页的生命周期事件顺序 116 3.1.3 ASP.NET生命周期详解 118 3.2 页面状态管理 120 3.2.1 Cookie 121 3.2.2 HtmlInputHidden隐藏域 123 3.2.3 ViewState 123 3.2.4 查询字符串Request 124 3.2.5 ...

    亮剑.NET深入体验与实战精要3

    3.1.2 具有Master页的生命周期事件顺序 116 3.1.3 ASP.NET生命周期详解 118 3.2 页面状态管理 120 3.2.1 Cookie 121 3.2.2 HtmlInputHidden隐藏域 123 3.2.3 ViewState 123 3.2.4 查询字符串Request 124 3.2.5 ...

    软件工程完整ppt

     1632组件模型及描述语言  1633组件的检索与组装  164计算机辅助软件工程技术  1641CASE的基本概念  1642CASE工具与集成CASE环境  165软件产品线技术  1651软件产品线基本概念  1652软件产品线方法  1653...

    Android高级编程--源代码

    3.4 Android应用程序生命周期 48 3.5 理解应用程序的优先级和进程状态 49 3.6 分离资源 50 3.6.1 创建资源 50 3.6.2 使用资源 57 3.6.3 To-Do List资源示例 60 3.6.4 为不同的语言和硬件创建资源 61 3.6.5 ...

Global site tag (gtag.js) - Google Analytics