Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

optaplanner - Setting assignment in multiple object hierarchy

I have a problem regarding assignment of products using optaplanner, I'm implementing classes in following hierarchy, Plan -> AvailableProduct -> AvailableProductTask -> assignee. I want to set the assignee field which is containing in above object hierarchy, I have tried couple of examples given in documentation including Employee Roster. Could anyone suggest the solution for setting assignee present in AvailableProductTask class? POJO code are as following...

    import java.util.ArrayList;
    import java.util.List;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.EnumType;
    import javax.persistence.Enumerated;
    import javax.persistence.FetchType;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    
    import org.apache.commons.lang3.builder.EqualsBuilder;
    import org.apache.commons.lang3.builder.HashCodeBuilder;
    import org.apache.commons.lang3.builder.ToStringBuilder;
    
    @Entity
    @Table(name = "plan")
    public class Plan extends BaseEntity<Long> {
    
        private static final long serialVersionUID = -5734441725217207470L;
    
        @Column(length = 255, nullable = false)
        private String name;
    
        @Column(length = 255, nullable = false)
        private String description;
    
        private Long parentPlanId;
    
        @Column(nullable = false, columnDefinition = "varchar(32) default 'TODO'")
        @Enumerated(value = EnumType.STRING)
        private PlanStatus status;
    
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        @JoinColumn(name = "plan_id")
        private List<AvailableUser> users;
    
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        @JoinColumn(name = "plan_id")
        private List<AvailableCustomerSla> slas;
    
        @OneToMany
        @JoinColumn(name = "plan_Id")
        private List<AvailableProduct> products;
    
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
    
        /**
         * @param name the name to set
         */
        public void setName(final String name) {
            this.name = name;
        }
    
        /**
         * @return the description
         */
        public String getDescription() {
            return description;
        }
    
        /**
         * @param description the description to set
         */
        public void setDescription(final String description) {
            this.description = description;
        }
    
        /**
         * @return the parentPlanId
         */
        public Long getParentPlanId() {
            return parentPlanId;
        }
    
        /**
         * @param parentPlanId the parentPlanId to set
         */
        public void setParentPlanId(final Long parentPlanId) {
            this.parentPlanId = parentPlanId;
        }
    
        /**
         * @return the status
         */
        public PlanStatus getStatus() {
            return status;
        }
    
        /**
         * @return the users
         */
        public List<AvailableUser> getUsers() {
            return users;
        }
    
        /**
         * @param status the status to set
         */
        public void setStatus(final PlanStatus status) {
            this.status = status;
        }
    
        /**
         * @param users the users to set
         */
        public void setUsers(final List<AvailableUser> users) {
            this.users = users;
        }
    
        /**
         * @return the slas
         */
        public List<AvailableCustomerSla> getSlas() {
            return slas;
        }
    
        /**
         * @param slas the slas to set
         */
        public void setSlas(List<AvailableCustomerSla> slas) {
            this.slas = slas;
        }
    
        /**
         * @return the products
         */
        public List<AvailableProduct> getProducts() {
            return products;
        }
    
        /**
         * @param products the products to set
         */
        public void setProducts(List<AvailableProduct> products) {
            this.products = products;
        }
    
        /**
         * Adds a single available product to the list of products
         * @param product the product to add
         */
        public void addAvailableProduct(AvailableProduct product) {
            if (null == product) {
                return;
            }
            if (null == this.products) {
                this.products = new ArrayList<>();
            }
            this.products.add(product);
        }
    
        @Override
        public final boolean equals(final Object object) {
            if (object == null) {
                return false;
            }
            if (object == this) {
                return true;
            }
            if (!(object instanceof Plan)) {
                return false;
            }
            final Plan plan = (Plan) object;
            return new EqualsBuilder() //
                    .append(this.getId(), plan.getId()) //
                    .isEquals();
        }
    
        @Override
        public final int hashCode() {
            return new HashCodeBuilder() //
                    .append(getId()) //
                    .hashCode();
        }
    
        @Override
        public String toString() {
            return new ToStringBuilder(this) //
                    .append("Id", getId()) //
                    .append("Name", getName()) //
                    .append("Description", getDescription()) //
                    .append("ParentPlanId", getParentPlanId()) //
                    .append("Status", getStatus()) //
                    .append("Users", getUsers()) //
                    .append("Products", getProducts()) //
                    .toString();
        }
    }
    
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.ProblemFactCollectionProperty;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;

@Entity
@Table(uniqueConstraints = {
    @UniqueConstraint(columnNames = { "plan_id", "productId" }) })
@PlanningSolution
public class AvailableProduct extends BaseEntity<Long> {

    private static final long serialVersionUID = -4463281008771600521L;

    @Column(nullable = false)
    private String productId;

    @Column(nullable = false)
    private String categoryId;

    @OneToMany
    @JoinColumn(name = "plan_available_product_id")
    @PlanningEntityCollectionProperty
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<AvailableProductTask> tasks;

    @OneToMany
    @JoinColumn(name = "plan_available_product_id")
    private List<AvailableProductCustomerSLA> availableProductCustomerSLAs;

    @ElementCollection
    @ProblemFactCollectionProperty
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Integer> facts;

    @PlanningScore
    private HardSoftScore score;


    /**
     * Argument free constructor
     */
    public AvailableProduct() {

    }

    /**
     * Constructor for create available product instance.
     *
     * @param productId Id of the product.
     * @param categoryId Id of the category.
     */
    public AvailableProduct(final String productId, final String categoryId) {
        this.productId = productId;
        this.categoryId = categoryId;
        this.tasks = new ArrayList<>();
    }


    public List<Integer> getFacts() {
        return facts;
    }

    public void setFacts(List<Integer> facts) {
        this.facts = facts;
    }

    /**
     * @return the score
     */
    public HardSoftScore getScore() {
        return score;
    }

    /**
     * @param score the score to set
     */
    public void setScore(HardSoftScore score) {
        this.score = score;
    }


    /**
     * @return the productId
     */
    public String getProductId() {
        return productId;
    }

    /**
     * @param productId the productId to set
     */
    public void setProductId(final String productId) {
        this.productId = productId;
    }

    /**
     * @return the categoryId
     */
    public String getCategoryId() {
        return categoryId;
    }

    /**
     * @param categoryId the categoryId to set
     */
    public void setCategoryId(final String categoryId) {
        this.categoryId = categoryId;
    }

    /**
     * @return the tasks
     */
    public List<AvailableProductTask> getTasks() {
        return tasks;
    }

    /**
     * @param tasks the tasks to set
     */
    public void setTasks(final List<AvailableProductTask> tasks) {
        this.tasks = tasks;
    }

    /**
     * Adds a single task to the list of tasks
     * @param task the task to add
     */
    public void addTasks(final AvailableProductTask task) {
        if (null == task) {
            return;
        }
        if (null == this.tasks) {
            this.tasks = new ArrayList<>();
        }
        this.tasks.add(task);
    }

    /**
     * @return the availableProductCustomerSLAs
     */
    public List<AvailableProductCustomerSLA> getAvailableProductCustomerSLAs() {
        return availableProductCustomerSLAs;
    }

    /**
     * @param availableProductCustomerSLAs the availableProductCustomerSLAs to set
     */
    public void setAvailableProductCustomerSLAs(
            List<AvailableProductCustomerSLA> availableProductCustomerSLAs) {
        this.availableProductCustomerSLAs = availableProductCustomerSLAs;
    }

    /**
     * Adds a single availableProductCustomerSLA to the list of slas
     * @param availableProductCustomerSLA the availableProductCustomerSLA to add
     */
    public void addAvailableProductCustomerSLA(
            final AvailableProductCustomerSLA availableProductCustomerSLA) {
        if (null == availableProductCustomerSLA) {
            return;
        }
        if (null == this.availableProductCustomerSLAs) {
            this.availableProductCustomerSLAs = new ArrayList<>();
        }
        this.availableProductCustomerSLAs.add(availableProductCustomerSLA);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean equals(final Object object) {
        if (object == null) {
            return false;
        }
        if (object == this) {
            return true;
        }
        if (!(object instanceof Ava

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...