欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

生命周期组件框架:带有多状态机的单实现类 博客分类: 软件设计 JavaLifecycleStateMachine状态机生命周期 

程序员文章站 2024-03-20 23:46:22
...

 ServiceOrder生命周期元数据

 

@StateMachine
public interface ServiceOrderLifecycleMeta extends OrderLifecycleMeta {

    @StateSet
    public static class States extends OrderLifecycleMeta.States {

        @InboundWhiles({
                @InboundWhile(relation = SummaryPlan.class, on = { ServiceSummaryPlanLifecycleMeta.States.Ongoing.class }, otherwise = {
                        @ErrorMessage(states = { ServiceSummaryPlanLifecycleMeta.States.VolumeLeftEmpty.class }, bundle = Consts.BUNDLE_NAME,
                                code = ErrorCodes.SUMMARY_PLAN__SHOULD_BE_ONGOING_THAN_VOLUME_EMPTY),
                        @ErrorMessage(states = { ServiceSummaryPlanLifecycleMeta.States.Done.class }, bundle = Consts.BUNDLE_NAME,
                                code = ErrorCodes.SUMMARY_PLAN__SHOULD_BE_ONGOING_THAN_DONE) }),
                @InboundWhile(relation = PlantResource.class,
                        on = { PlantResourceLifecycleMeta.States.Idle.class, PlantResourceLifecycleMeta.States.Busy.class }, otherwise = { @ErrorMessage(
                                bundle = Consts.BUNDLE_NAME, code = ErrorCodes.MIXING_PLANT_RESOURCE_INOT_IN_IDLE_OR_BUSY_STATE,
                                states = { PlantResourceLifecycleMeta.States.Maintaining.class }) }),
                @InboundWhile(relation = ConcreteTruckResource.class, on = { ConcreteTruckResourceLifecycleMeta.States.Idle.class,
                        ConcreteTruckResourceLifecycleMeta.States.Busy.class }, otherwise = { @ErrorMessage(bundle = Consts.BUNDLE_NAME,
                        code = ErrorCodes.TRUCK_RESOURCE_NOT_IN_IDLE_OR_BUSY_STATE, states = { ConcreteTruckResourceLifecycleMeta.States.Detached.class }) }) })
        public static class Ongoing extends OrderLifecycleMeta.States.Ongoing {}
    }
    @TransitionSet
    public static class Transitions extends OrderLifecycleMeta.Transitions {}
    @RelationSet
    public static class Relations {

        @Parent
        @RelateTo(ServiceSummaryPlanLifecycleMeta.class)
        public static class SummaryPlan {}
        @RelateTo(ConcreteTruckResourceLifecycleMeta.class)
        public static class ConcreteTruckResource {}
        @RelateTo(PlantResourceLifecycleMeta.class)
        public static class PlantResource {}
    }
}

 VehicleScheduleOrder生命周期元数据

@StateMachine
public interface VehicleScheduleOrderLifecycleMeta extends OrderLifecycleMeta {

    @StateSet
    public static class States {

        @LifecycleOverride
        @CompositeState
        @InboundWhile(relation = Relations.ConcreteTruckResource.class, on = { ConcreteTruckResourceLifecycleMeta.States.Idle.class },
                otherwise = { @ErrorMessage(bundle = Consts.BUNDLE_NAME, code = ErrorCodes.TRUCK_RESOURCE_NOT_IN_IDLE_OR_BUSY_STATE,
                        states = { ConcreteTruckResourceLifecycleMeta.States.Detached.class }) })
        public static class Ongoing extends OrderLifecycleMeta.States.Ongoing {

            @StateSet
            public static class CStates {

                @Initial
                @Function(transition = CTransitions.DoTransport.class, value = { OnPassage.class })
                public static class Loading {}
                @Function(transition = CTransitions.DoConstruct.class, value = { Constructing.class })
                public static class OnPassage {}
                @Function(transition = CTransitions.DoFinish.class, value = { Exit.class })
                public static class Constructing {}
                @End
                @ShortCut(value = Finished.class)
                public static class Exit {}
            }
            @TransitionSet
            public static class CTransitions {

                public static class DoFinish {}
                public static class DoTransport {}
                public static class DoConstruct {}
            }
        }
    }
    @TransitionSet
    public static class Transitions extends OrderLifecycleMeta.Transitions {}
    @RelationSet
    public static class Relations {

        @Parent
        @RelateTo(ServiceOrderLifecycleMeta.class)
        public static class ServiceOrder {}
        @RelateTo(ConcreteTruckResourceLifecycleMeta.class)
        public static class ConcreteTruckResource {}
    }
}

 

ConcreteTruckResource生命周期元数据

 

@StateMachine
public interface ConcreteTruckResourceLifecycleMeta extends SchedulableResourceLifecycleMeta {

    @StateSet
    public static class States extends SchedulableResourceLifecycleMeta.States {}
    @TransitionSet
    public static class Transitions extends SchedulableResourceLifecycleMeta.Transitions {}
}

 

IServiceOrder 业务接口

 

@LifecycleMeta(value = ServiceOrderLifecycleMeta.class)
public interface IServiceOrder {

    long getId();

    /** Transition methods **/
    @Transition(ServiceOrderLifecycleMeta.Transitions.Start.class)
    void configureResources(@Relation(ServiceOrderLifecycleMeta.Relations.PlantResource.class) MixingPlantResource plantResource,
            @Relation(ServiceOrderLifecycleMeta.Relations.ConcreteTruckResource.class) ConcreteTruckResource truckResource, double volume);

    @Transition(Finish.class)
    void confirmFinish();

    @Transition(Cancel.class)
    void cancel();

    @StateIndicator("serviceOrderState")
    String getState();

    @Relation(ServiceOrderLifecycleMeta.Relations.ConcreteTruckResource.class)
    ConcreteTruckResource getConcreteTruckResource();

    @Relation(ServiceOrderLifecycleMeta.Relations.SummaryPlan.class)
    ServiceSummaryPlan getSummaryPlan();

    @Relation(ServiceOrderLifecycleMeta.Relations.PlantResource.class)
    MixingPlantResource getMixingPlantResource();
}

 

 

IPlantScheduleOrder业务接口

 

@LifecycleMeta(value = PlantScheduleOrderLifecycleMeta.class)
public interface IPlantScheduleOrder {

    long getId();

    /* NON-TRANSITION Methods */
    String getPlantName();

    String getOperatorName();

    Date getProductionFinishedOn();

    Date getCreatedOn();

    /** Transition methods **/
    @Transition(PlantScheduleOrderLifecycleMeta.Transitions.Start.class)
    void doStartPlantOrder();

    @Transition(PlantScheduleOrderLifecycleMeta.Transitions.Finish.class)
    void doFinishPlantOrder();

    @Transition(PlantScheduleOrderLifecycleMeta.Transitions.Cancel.class)
    void cancelPlantOrder();

    @StateIndicator
    String getPlantScheduleOrderState();
    
    @Relation(PlantScheduleOrderLifecycleMeta.Relations.ServiceOrder.class)
    IServiceOrder getServiceOrder();
} 

 

IVehicleScheduleOrder业务接口 

 

@LifecycleMeta(value = VehicleScheduleOrderLifecycleMeta.class)
public interface IVehicleScheduleOrder {

    long getId();

    String getConcretePlantName();

    String getUnitProjectName();

    Address getAddress();

    Contact getContact();

    String getPouringPartName();

    String getMixtureStrengthGrade();

    String[] getAdditiveNames();

    double getTransportVolume();

    Date getCreatedOn();

    Date getTransportFinishedOn();

    /** Transition methods **/
    @Transition(VehicleScheduleOrderLifecycleMeta.Transitions.Start.class)
    void doLoad();

    @Transition(VehicleScheduleOrderLifecycleMeta.States.Ongoing.CTransitions.DoTransport.class)
    void doTransport();

    @Transition(VehicleScheduleOrderLifecycleMeta.States.Ongoing.CTransitions.DoConstruct.class)
    void doConstruct();

    @Transition(VehicleScheduleOrderLifecycleMeta.States.Ongoing.CTransitions.DoFinish.class)
    void doComplete();

    @Transition(VehicleScheduleOrderLifecycleMeta.Transitions.Finish.class)
    void doFinishVehicalOrder();

    @Transition(VehicleScheduleOrderLifecycleMeta.Transitions.Cancel.class)
    void doAbortOnVehicleScheduleOrder();

    @StateIndicator
    String getVehicleScheduleOrderState();

    @Relation(VehicleScheduleOrderLifecycleMeta.Relations.ServiceOrder.class)
    IServiceOrder getServiceOrder();

    @Relation(VehicleScheduleOrderLifecycleMeta.Relations.ConcreteTruckResource.class)
    ConcreteTruckResource getConcreteTruckResource();
}

 

 ServiceOrder业务实体——实现多个业务接口,同时具体化了多个状态机

 

@Entity
@Table(name = "service_order")
@ReactiveObject
public class ServiceOrder extends ServiceOrderBase implements IServiceOrder, IPlantScheduleOrder, IVehicleScheduleOrder {

    protected static final long serialVersionUID = -6118079224654228286L;

    protected ServiceOrder() {}

    public ServiceOrder(ServiceSummaryPlan parent) {
        parent.addResourceAllocatedTask(this);
        this.summaryPlan = parent;
    }

    @Transition(ServiceOrderLifecycleMeta.Transitions.Start.class)
    public void configureResources(@Relation(ServiceOrderLifecycleMeta.Relations.PlantResource.class) MixingPlantResource plantResource,
            @Relation(ServiceOrderLifecycleMeta.Relations.ConcreteTruckResource.class) ConcreteTruckResource truckResource, double volume) {
        {
            this.spec = summaryPlan.getSpec();
            this.truckResource = truckResource;
            this.mixingPlantResource = plantResource;
            this.plannedVolume = volume;
        }
        this.mixingPlantResource.assignOrder(this);
        this.truckResource.assignOrder(this);
    }

    @Transition(ServiceOrderLifecycleMeta.Transitions.Finish.class)
    public void confirmFinish() {
        this.finishedOn = new Date();
    }

    @Transition(ServiceOrderLifecycleMeta.Transitions.Cancel.class)
    public void cancel() {
        this.canceledOn = new Date();
    }

    @Override
    @Transition(VehicleScheduleOrderLifecycleMeta.Transitions.Start.class)
    public void doLoad() {
        this.vehicleLoadOn = new Date();
    }

    @Override
    @Transition(VehicleScheduleOrderLifecycleMeta.States.Ongoing.CTransitions.DoTransport.class)
    public void doTransport() {
        this.vehicleTransportOn = new Date();
    }

    @Override
    @Transition(VehicleScheduleOrderLifecycleMeta.States.Ongoing.CTransitions.DoConstruct.class)
    public void doConstruct() {
        this.vehicleConstructOn = new Date();
    }

    @Override
    @Transition(VehicleScheduleOrderLifecycleMeta.Transitions.Finish.class)
    public void doComplete() {
        this.vehicleCompletedOn = new Date();
    }

    @Override
    @Transition(VehicleScheduleOrderLifecycleMeta.Transitions.Cancel.class)
    public void doAbortOnVehicleScheduleOrder() {
        this.vehicleAbortedOn = new Date();
    }

    @Override
    @Transition(VehicleScheduleOrderLifecycleMeta.States.Ongoing.CTransitions.DoFinish.class)
    public void doFinishVehicalOrder() {}

    @Override
    @Transition(PlantScheduleOrderLifecycleMeta.Transitions.Start.class)
    public void doStartPlantOrder() {
        this.plantStartedOn = new Date();
    }

    @Override
    @Transition(PlantScheduleOrderLifecycleMeta.Transitions.Finish.class)
    public void doFinishPlantOrder() {
        this.plantFinishOn = new Date();
    }

    @Override
    @Transition(PlantScheduleOrderLifecycleMeta.Transitions.Cancel.class)
    public void cancelPlantOrder() {
        this.plantCanceledOn = new Date();
    }

    @Override
    @Relation
    public IServiceOrder getServiceOrder() {
        return this;
    }

    @Override
    @Relation
    public ConcreteTruckResource getConcreteTruckResource() {
        return this.truckResource;
    }

    @Override
    public String getConcretePlantName() {
        return this.mixingPlantResource.getMixingPlant().getName();
    }

    @Override
    public String getUnitProjectName() {
        return this.spec.getUnitProject().getName();
    }

    @Override
    public Address getAddress() {
        return this.spec.getUnitProject().getAddress();
    }

    @Override
    public Contact getContact() {
        return this.spec.getUnitProject().getContact();
    }

    @Override
    public String getPouringPartName() {
        return this.spec.getPouringPart().getName();
    }

    @Override
    public String getMixtureStrengthGrade() {
        return this.spec.getMixture().getGradeName();
    }

    @Override
    public String[] getAdditiveNames() {
        final ArrayList<String> additiveNames = new ArrayList<String>();
        final List<Additive> additives = this.spec.getAdditives();
        for ( Additive additive : additives ) {
            additiveNames.add(additive.getName());
        }
        return additiveNames.toArray(new String[0]);
    }

    @Override
    public double getTransportVolume() {
        return this.plannedVolume;
    }

    @Override
    public Date getTransportFinishedOn() {
        return this.transportFinishedOn;
    }

    @Override
    public String getPlantName() {
        return this.mixingPlantResource.getMixingPlant().getName();
    }

    @Override
    public String getOperatorName() {
        return this.mixingPlantResource.getMixingPlant().getOperator().getFullName();
    }

    @Override
    public Date getProductionFinishedOn() {
        return this.productionFinishedOn;
    }
}

 

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