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

    @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);
        }
    }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值