Example usage for java.util Collection parallelStream

List of usage examples for java.util Collection parallelStream

Introduction

In this page you can find the example usage for java.util Collection parallelStream.

Prototype

default Stream<E> parallelStream() 

Source Link

Document

Returns a possibly parallel Stream with this collection as its source.

Usage

From source file:org.lightjason.agentspeak.agent.IBaseAgent.java

/**
 * create execution list with plan and context
 *
 * @param p_trigger trigger stream//from w  ww  .  j  a va 2  s .  c o  m
 * @return collection with excutable plans, instantiated execution context and plan statistic
 */
private Collection<Pair<Triple<IPlan, AtomicLong, AtomicLong>, IContext>> generateexecution(
        final Stream<ITrigger> p_trigger) {
    return p_trigger.filter(Objects::nonNull).flatMap(i -> {
        final Collection<Triple<IPlan, AtomicLong, AtomicLong>> l_plans = m_plans.get(i);

        return l_plans == null ? Stream.of()
                : l_plans.parallelStream()

                        // tries to unify trigger literal and filter of valid unification (returns set of unified variables)
                        .map(j -> new ImmutablePair<>(j,
                                CCommon.unifytrigger(m_unifier, i, j.getLeft().getTrigger())))
                        .filter(j -> j.getRight().getLeft())

                        // initialize context
                        .map(j -> new ImmutablePair<>(j.getLeft(),
                                CCommon.instantiateplan(j.getLeft().getLeft(), j.getLeft().getMiddle().get(),
                                        j.getLeft().getRight().get(), this, j.getRight().getRight())
                                        .getRight()))

                        // check plan condition
                        .filter(j -> m_fuzzy.getDefuzzyfication()
                                .defuzzify(j.getLeft().getLeft().condition(j.getRight())));
    })
            // collectors-call must be toList not toSet because plan-execution can be have equal elements
            // so a set avoid multiple plan-execution
            .collect(Collectors.toList());

}

From source file:org.lightjason.agentspeak.agent.IBaseAgent.java

/**
 * execute list of plans//from  w  w w . j a va2s .  co m
 *
 * @param p_execution execution collection with instantiated plans and context
 * @return fuzzy result for each executaed plan
 */
private IFuzzyValue<Boolean> execute(
        final Collection<Pair<Triple<IPlan, AtomicLong, AtomicLong>, IContext>> p_execution) {
    // update executable plan list, so that test-goals are defined all the time
    p_execution.parallelStream()
            .forEach(i -> m_runningplans.put(i.getLeft().getLeft().getTrigger().getLiteral().fqnfunctor(),
                    i.getLeft().getLeft().getTrigger().getLiteral().unify(i.getRight())));

    // execute plan and return values and return execution result
    return p_execution.parallelStream().map(i -> {

        final IFuzzyValue<Boolean> l_result = i.getLeft().getLeft().execute(i.getRight(), false, null, null,
                null);
        if (m_fuzzy.getDefuzzyfication().defuzzify(l_result))
            // increment successful runs
            i.getLeft().getMiddle().getAndIncrement();
        else
            // increment failed runs and create delete goal-event
            i.getLeft().getRight().getAndIncrement();

        return l_result;
    }).collect(m_fuzzy.getResultOperator());
}

From source file:org.lightjason.agentspeak.language.execution.action.achievement_test.IAchievementRule.java

/**
 * execute rule from context//w  ww .  j a v a2  s  . co  m
 *
 * @param p_context execution context
 * @param p_value execution literal
 * @param p_parallel parallel execution
 * @return boolean result
 */
@SuppressWarnings("unchecked")
protected static IFuzzyValue<Boolean> execute(final IContext p_context, final ILiteral p_value,
        final boolean p_parallel) {
    // read current rules, if not exists execution fails
    final Collection<IRule> l_rules = p_context.agent().rules().get(p_value.fqnfunctor());
    if (l_rules == null)
        return CFuzzyValue.from(false);

    // first step is the unification of the caller literal, so variables will be set from the current execution context
    final ILiteral l_unified = p_value.allocate(p_context);

    // second step execute backtracking rules sequential / parallel
    return (p_parallel ? l_rules.parallelStream() : l_rules.stream()).map(i -> {

        // instantiate variables by unification of the rule literal
        final Set<IVariable<?>> l_variables = p_context.agent().unifier().literal(i.getIdentifier(), l_unified);

        // execute rule
        final IFuzzyValue<Boolean> l_return = i.execute(i.instantiate(p_context.agent(), l_variables.stream()),
                false, Collections.<ITerm>emptyList(), Collections.<ITerm>emptyList(),
                Collections.<ITerm>emptyList());

        // create rule result with fuzzy- and defuzzificated value and instantiate variable set
        return new ImmutableTriple<>(p_context.agent().fuzzy().getDefuzzyfication().defuzzify(l_return),
                l_return, l_variables);

    })

            // find successfully ended rule
            .filter(ImmutableTriple::getLeft).findFirst()

            // realocate rule instantiated variables back to execution context
            .map(i -> {

                i.getRight().parallelStream().filter(j -> j instanceof IRelocateVariable)
                        .forEach(j -> ((IRelocateVariable) j).relocate());

                return i.getMiddle();

            })

            // otherwise rule fails (default behaviour)
            .orElse(CFuzzyValue.from(false));
}

From source file:org.matonto.rdf.orm.generate.GraphReadingUtility.java

public static Model readOntologies(final Collection<Pair<File, String>> pairs) throws IOException {
    final Model overall = new LinkedHashModel();
    final List<String> issues = new ArrayList<>();
    pairs.parallelStream().forEach(pair -> {
        try {//from w ww  .  j  av a2  s  .  c o  m
            readOntology(pair.getLeft(), pair.getRight());
        } catch (Exception e) {
            LOG.error("Issue reading ontology '" + pair.getLeft() + "'" + e.getMessage(), e);
            issues.add("Issue reading ontology '" + pair.getLeft() + "'" + e.getMessage()
                    + "\n\tEnsure the file format matches type file suffix.");
        }
    });
    if (!pairs.isEmpty()) {
        throw new IOException(StringUtils.join(issues, "\n"));
    }
    return overall;
}

From source file:org.nanoframework.orm.jedis.sharded.RedisClientImpl.java

@Override
public boolean smove(final String source, final String destination, final String member) {
    Assert.hasText(source);/*from  w  w w . ja  v a2  s. c o  m*/
    Assert.hasText(destination);
    Assert.hasText(member);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Collection<Jedis> allShards;
        if ((allShards = jedis.getAllShards()).size() == 1) {
            return isSuccess(allShards.iterator().next().smove(source, destination, member));
        } else if (allShards.size() > 1) {
            final AtomicLong val = new AtomicLong();
            allShards.parallelStream().forEach(shard -> {
                Pipeline pipeline = shard.pipelined();
                pipeline.sismember(source, member);
                Response<Long> response = pipeline.smove(source, destination, member);
                pipeline.sync();
                val.addAndGet(response.get());
            });

            if (val.get() > 0) {
                return true;
            }
        }

        return false;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}

From source file:org.openlmis.fulfillment.service.referencedata.PermissionStringDto.java

/**
 * Parses a collection of string representation of permissionString to set with object
 * representation./*from   www .  j a  v  a2 s .co  m*/
 *
 * @param permissionStrings a collection of string representation
 * @return a set with {@link PermissionStringDto} instances
 */
public static Set<PermissionStringDto> from(Collection<String> permissionStrings) {
    return permissionStrings.parallelStream().map(PermissionStringDto::from).collect(Collectors.toSet());
}