Search in sources :

Example 1 with Assignment

use of org.kie.internal.task.api.assignment.Assignment in project jbpm by kiegroup.

the class BusinessRuleAssignmentStrategy method apply.

@SuppressWarnings("unchecked")
@Override
public Assignment apply(Task task, TaskContext context, String excludedUser) {
    if (!active) {
        logger.debug("{} strategy is not active", this);
        return null;
    }
    logger.debug("Using rules to assign actual owner to task {}", task);
    KieSession kieSession = this.kieContainer.newKieSession();
    try {
        context.loadTaskVariables(task);
        kieSession.insert(task);
        kieSession.fireAllRules();
        Set<Assignment> assignments = new TreeSet<>();
        String queryName = System.getProperty("org.jbpm.task.assignment.rules.query");
        if (queryName != null) {
            logger.debug("Query {} is going to be used to retrieve results from working memory", queryName);
            QueryResults results = kieSession.getQueryResults(queryName);
            results.forEach(row -> assignments.add((Assignment) row.get("assignment")));
        } else {
            logger.debug("No query defined, retrieving all facts of type Assignment");
            Collection<Assignment> results = (Collection<Assignment>) kieSession.getObjects(new ClassObjectFilter(Assignment.class));
            assignments.addAll(results);
        }
        logger.debug("Rule evaluation completed with selected assignments of {}", assignments);
        if (assignments.isEmpty()) {
            logger.debug("No assignments found by BusinessRule strategy");
            return null;
        }
        Assignment selected = assignments.iterator().next();
        logger.debug("Selected assignment is {} for task {}", selected, task);
        return selected;
    } finally {
        kieSession.dispose();
        logger.debug("KieSession in BusinessRule disposed");
    }
}
Also used : Assignment(org.kie.internal.task.api.assignment.Assignment) ClassObjectFilter(org.drools.core.ClassObjectFilter) TreeSet(java.util.TreeSet) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults)

Example 2 with Assignment

use of org.kie.internal.task.api.assignment.Assignment in project jbpm by kiegroup.

the class LoadBalanceAssignmentStrategy method apply.

@Override
public Assignment apply(Task task, TaskContext taskContext, String excludedUser) {
    UserInfo userInfo = (UserInfo) ((org.jbpm.services.task.commands.TaskContext) taskContext).get(EnvironmentName.TASK_USER_INFO);
    List<OrganizationalEntity> excluded = (getExcludedEntities(task, userInfo));
    // Get the the users from the task's the potential owners, making sure that excluded users are not included
    List<OrganizationalEntity> potentialOwners = task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof User && !excluded.contains(oe) && !oe.getId().equals(excludedUser)).collect(Collectors.toList());
    // Get the users belonging to groups that are potential owners
    task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof Group).forEach(oe -> {
        Iterator<OrganizationalEntity> groupUsers = userInfo.getMembersForGroup((Group) oe);
        if (groupUsers != null) {
            groupUsers.forEachRemaining(user -> {
                if (user != null && !excluded.contains(user) && !potentialOwners.contains(user) && !user.getId().equals(excludedUser)) {
                    potentialOwners.add(user);
                }
            });
        }
    });
    logger.debug("Asking the load calculator [{}] for task loads for the users {}", calculator.getIdentifier(), potentialOwners);
    List<User> users = potentialOwners.stream().map(entityToUser).collect(Collectors.toList());
    Collection<UserTaskLoad> loads = calculator.getUserTaskLoads(users, taskContext);
    UserTaskLoad lightestLoad = loads.stream().min(UserTaskLoad::compareTo).orElse(null);
    return lightestLoad != null ? new Assignment(lightestLoad.getUser().getId()) : null;
}
Also used : TaskContext(org.kie.api.task.TaskContext) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Task(org.kie.api.task.model.Task) Assignment(org.kie.internal.task.api.assignment.Assignment) EnvironmentName(org.kie.api.runtime.EnvironmentName) UserInfo(org.kie.internal.task.api.UserInfo) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) LoadCalculator(org.jbpm.services.task.assignment.LoadCalculator) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) List(java.util.List) Group(org.kie.api.task.model.Group) AssignmentStrategy(org.kie.internal.task.api.assignment.AssignmentStrategy) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) User(org.kie.api.task.model.User) Group(org.kie.api.task.model.Group) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) UserInfo(org.kie.internal.task.api.UserInfo) Assignment(org.kie.internal.task.api.assignment.Assignment) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad)

Example 3 with Assignment

use of org.kie.internal.task.api.assignment.Assignment in project jbpm by kiegroup.

the class PotentialOwnerBusynessAssignmentStrategy method apply.

@Override
public Assignment apply(Task task, TaskContext context, String excludedUser) {
    if (task.getPeopleAssignments().getPotentialOwners().isEmpty()) {
        logger.debug("No potential owners in the task {} can't auto assign", task);
        return null;
    }
    List<OrganizationalEntity> potentialOwners = new ArrayList<>(task.getPeopleAssignments().getPotentialOwners());
    Set<String> resolvedUsers = new TreeSet<>(Collections.reverseOrder());
    List<OrganizationalEntity> excludedOwners = ((InternalPeopleAssignments) task.getPeopleAssignments()).getExcludedOwners();
    potentialOwners.stream().filter(po -> po instanceof User && !excludedOwners.contains(po)).forEach(po -> resolvedUsers.add(po.getId()));
    UserInfo userInfo = (UserInfo) ((org.jbpm.services.task.commands.TaskContext) context).get(EnvironmentName.TASK_USER_INFO);
    if (userInfo != null) {
        logger.debug("Groups going to be resolved by {}", userInfo);
        potentialOwners.stream().filter(po -> po instanceof Group && !excludedOwners.contains(po)).forEach(po -> {
            Iterator<OrganizationalEntity> usersOfGroup = userInfo.getMembersForGroup((Group) po);
            if (usersOfGroup != null) {
                while (usersOfGroup.hasNext()) {
                    OrganizationalEntity entity = usersOfGroup.next();
                    if (!excludedOwners.contains(entity)) {
                        resolvedUsers.add(entity.getId());
                    }
                }
            }
        });
    }
    logger.debug("Resolved users eligible for task {} assignments are {}", task, resolvedUsers);
    if (excludedUser != null) {
        logger.debug("Removing excluded user {} from the list of eligible users", excludedUser);
        resolvedUsers.remove(excludedUser);
    }
    TaskPersistenceContext persistenceContext = ((org.jbpm.services.task.commands.TaskContext) context).getPersistenceContext();
    Map<String, Object> params = new HashMap<>();
    params.put("owners", resolvedUsers);
    logger.debug("DB query to be used for finding assignments :: '{}'", getQuery());
    List<Assignment> assignments = persistenceContext.queryStringWithParametersInTransaction(getQuery(), params, ClassUtil.<List<Assignment>>castClass(List.class));
    if (assignments.size() < resolvedUsers.size()) {
        logger.debug("Not all eligible users found in db, adding missing bits (eligible {}, found in db {})", resolvedUsers, assignments);
        // in case not all users have already assigned tasks added them to the list so can get the tasks
        resolvedUsers.forEach(user -> {
            Assignment assignment = new AssignmentImpl(user);
            if (!assignments.contains(assignment)) {
                // always add missing users to the top of the list so they get assigned first
                assignments.add(0, assignment);
            }
        });
    }
    if (assignments.isEmpty()) {
        logger.debug("No assignments found for task {}", task);
        return null;
    }
    logger.debug("Following assignments {} were found for task {}", assignments, task);
    // select first from the top of the list as it has the least assigned tasks
    Assignment selected = assignments.get(0);
    logger.debug("Retruning first of found assignments {}", selected);
    return new Assignment(selected.getUser());
}
Also used : TaskContext(org.kie.api.task.TaskContext) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Task(org.kie.api.task.model.Task) Assignment(org.kie.internal.task.api.assignment.Assignment) EnvironmentName(org.kie.api.runtime.EnvironmentName) UserInfo(org.kie.internal.task.api.UserInfo) AssignmentImpl(org.jbpm.services.task.assignment.impl.AssignmentImpl) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) ClassUtil(org.jbpm.services.task.utils.ClassUtil) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) List(java.util.List) Group(org.kie.api.task.model.Group) AssignmentStrategy(org.kie.internal.task.api.assignment.AssignmentStrategy) Map(java.util.Map) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) Collections(java.util.Collections) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) User(org.kie.api.task.model.User) Group(org.kie.api.task.model.Group) User(org.kie.api.task.model.User) HashMap(java.util.HashMap) AssignmentImpl(org.jbpm.services.task.assignment.impl.AssignmentImpl) ArrayList(java.util.ArrayList) UserInfo(org.kie.internal.task.api.UserInfo) Assignment(org.kie.internal.task.api.assignment.Assignment) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) TaskContext(org.kie.api.task.TaskContext) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 4 with Assignment

use of org.kie.internal.task.api.assignment.Assignment in project jbpm by kiegroup.

the class RoundRobinAssignmentStrategy method apply.

@Override
public Assignment apply(Task task, TaskContext taskContext, String excludedUser) {
    UserInfo userInfo = (UserInfo) ((org.jbpm.services.task.commands.TaskContext) taskContext).get(EnvironmentName.TASK_USER_INFO);
    List<OrganizationalEntity> excluded = getExcludedEntities(task, userInfo);
    // Get the the users from the task's the potential owners
    List<OrganizationalEntity> potentialOwners = task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof User && !excluded.contains(oe)).collect(Collectors.toList());
    // Get the users belonging to groups that are potential owners
    task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof Group).forEach(oe -> {
        Iterator<OrganizationalEntity> groupUsers = userInfo.getMembersForGroup((Group) oe);
        if (groupUsers != null) {
            groupUsers.forEachRemaining(user -> {
                if (user != null && !excluded.contains(user) && !potentialOwners.contains(user)) {
                    potentialOwners.add(user);
                }
            });
        }
    });
    if (excludedUser != null) {
        logger.debug("Removing excluded user {} from the list of eligible users", excludedUser);
        potentialOwners.removeIf(entity -> entity.getId().equals(excludedUser));
    }
    String queueName = getQueueName(task);
    CircularQueue<OrganizationalEntity> mappedQueue = synchronizedQueue(queueName, potentialOwners);
    OrganizationalEntity owner = mappedQueue.take();
    return new Assignment(owner.getId());
}
Also used : TaskContext(org.kie.api.task.TaskContext) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Task(org.kie.api.task.model.Task) Assignment(org.kie.internal.task.api.assignment.Assignment) EnvironmentName(org.kie.api.runtime.EnvironmentName) UserInfo(org.kie.internal.task.api.UserInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) List(java.util.List) Group(org.kie.api.task.model.Group) AssignmentStrategy(org.kie.internal.task.api.assignment.AssignmentStrategy) Map(java.util.Map) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) User(org.kie.api.task.model.User) Group(org.kie.api.task.model.Group) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) UserInfo(org.kie.internal.task.api.UserInfo) Assignment(org.kie.internal.task.api.assignment.Assignment)

Example 5 with Assignment

use of org.kie.internal.task.api.assignment.Assignment in project jbpm by kiegroup.

the class AssignmentServiceImpl method assignTask.

@Override
public void assignTask(Task task, TaskContext context, String excludedUser) {
    if (!isEnabled()) {
        logger.debug("AssignmentService is not enabled - to enable it set system property '" + ENABLED_PROPERTY + "' to true");
        return;
    }
    Assignment assignTo = this.strategy.apply(task, context, excludedUser);
    if (assignTo == null || assignTo.getUser() == null) {
        logger.warn("Strategy {} did not return any assignment for task {}", strategy, task);
        return;
    }
    logger.debug("Actual owner returned by strategy {} is {} for task {}", strategy, assignTo, task);
    User actualOwner = taskModelFactory.newUser(assignTo.getUser());
    ((InternalTaskData) task.getTaskData()).setActualOwner(actualOwner);
    ((InternalTaskData) task.getTaskData()).setStatus(Status.Reserved);
}
Also used : Assignment(org.kie.internal.task.api.assignment.Assignment) User(org.kie.api.task.model.User) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData)

Aggregations

Assignment (org.kie.internal.task.api.assignment.Assignment)5 User (org.kie.api.task.model.User)4 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 List (java.util.List)3 EnvironmentName (org.kie.api.runtime.EnvironmentName)3 TaskContext (org.kie.api.task.TaskContext)3 Group (org.kie.api.task.model.Group)3 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)3 Task (org.kie.api.task.model.Task)3 UserInfo (org.kie.internal.task.api.UserInfo)3 AssignmentStrategy (org.kie.internal.task.api.assignment.AssignmentStrategy)3 InternalPeopleAssignments (org.kie.internal.task.api.model.InternalPeopleAssignments)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Collection (java.util.Collection)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 Collectors (java.util.stream.Collectors)2 Collections (java.util.Collections)1