Search in sources :

Example 86 with Pair

use of org.apache.commons.math3.util.Pair in project mica2 by obiba.

the class AbstractFileSystemResource method doGetAttachment.

protected Attachment doGetAttachment(String path, @Nullable String version, @Nullable String shareKey) {
    String basePath = normalizePath(path);
    if (isPublishedFileSystem())
        subjectAclService.checkAccess("/file", basePath);
    else
        subjectAclService.checkPermission("/draft/file", "VIEW", basePath, shareKey);
    if (path.endsWith("/"))
        throw new IllegalArgumentException("Folder download is not supported");
    Pair<String, String> pathName = FileSystemService.extractPathName(basePath);
    AttachmentState state = fileSystemService.getAttachmentState(pathName.getKey(), pathName.getValue(), isPublishedFileSystem());
    if (isPublishedFileSystem())
        return state.getPublishedAttachment();
    if (Strings.isNullOrEmpty(version))
        return state.getAttachment();
    List<Attachment> attachments = fileSystemService.getAttachmentRevisions(state).stream().filter(a -> a.getId().equals(version)).collect(Collectors.toList());
    if (attachments.isEmpty())
        throw new NoSuchElementException("No file version " + version + " found at path: " + basePath);
    return attachments.get(0);
}
Also used : RevisionStatus(org.obiba.mica.core.domain.RevisionStatus) Attachment(org.obiba.mica.file.Attachment) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Pair(org.apache.commons.math3.util.Pair) AttachmentState(org.obiba.mica.file.AttachmentState) FileUtils(org.obiba.mica.file.FileUtils) FileSystemService(org.obiba.mica.file.service.FileSystemService) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) FileUtils.normalizePath(org.obiba.mica.file.FileUtils.normalizePath) FileUtils.isRoot(org.obiba.mica.file.FileUtils.isRoot) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) List(java.util.List) Lists(com.google.common.collect.Lists) Mica(org.obiba.mica.web.model.Mica) Comparator(java.util.Comparator) NoSuchElementException(java.util.NoSuchElementException) Dtos(org.obiba.mica.web.model.Dtos) Nullable(javax.annotation.Nullable) AttachmentState(org.obiba.mica.file.AttachmentState) Attachment(org.obiba.mica.file.Attachment) NoSuchElementException(java.util.NoSuchElementException)

Example 87 with Pair

use of org.apache.commons.math3.util.Pair in project iobserve-analysis by research-iobserve.

the class BehaviorModelTable method toInstances.

/**
 * create an Instances object for clustering.
 *
 * @return instance
 */
public Instances toInstances() {
    final FastVector fastVector = new FastVector();
    // add transitions
    for (int i = 0; i < this.signatures.size(); i++) {
        for (int j = 0; j < this.signatures.size(); j++) {
            if (this.transitions[i][j] > AbstractBehaviorModelTable.TRANSITION_THRESHOLD) {
                final Attribute attribute = new Attribute(AbstractBehaviorModelTable.EDGE_INDICATOR + this.inverseSignatures[i] + AbstractBehaviorModelTable.EDGE_DIVIDER + this.inverseSignatures[j]);
                fastVector.addElement(attribute);
            } else {
                continue;
            }
        }
    }
    // add informations
    this.signatures.values().stream().forEach(pair -> Arrays.stream(pair.getSecond()).forEach(callInformation -> fastVector.addElement(new Attribute(AbstractBehaviorModelTable.INFORMATION_INDICATOR + this.inverseSignatures[pair.getFirst()] + AbstractBehaviorModelTable.INFORMATION_DIVIDER + callInformation.getSignature()))));
    // TODO name
    final Instances instances = new Instances("Test", fastVector, 0);
    final Instance instance = this.toInstance();
    instances.add(instance);
    return instances;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) FastVector(weka.core.FastVector) Pair(org.apache.commons.math3.util.Pair) Instances(weka.core.Instances) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PayloadAwareEntryCallEvent(org.iobserve.stages.general.data.PayloadAwareEntryCallEvent) EntryCallEvent(org.iobserve.stages.general.data.EntryCallEvent) Instance(weka.core.Instance) List(java.util.List) SingleOrNoneCollector(org.iobserve.analysis.behavior.SingleOrNoneCollector) CallInformation(org.iobserve.analysis.behavior.models.extended.CallInformation) Map(java.util.Map) Optional(java.util.Optional) Attribute(weka.core.Attribute) Instances(weka.core.Instances) FastVector(weka.core.FastVector) Attribute(weka.core.Attribute) Instance(weka.core.Instance)

Example 88 with Pair

use of org.apache.commons.math3.util.Pair in project incubator-gobblin by apache.

the class CouchbaseWriter method write.

@Override
public Future<WriteResponse> write(final D record, final WriteCallback callback) {
    assertRecordWritable(record);
    if (record instanceof TupleDocument) {
        ((TupleDocument) record).content().value1().retain();
    }
    Observable<D> observable;
    try {
        observable = _bucket.async().upsert(setDocumentTTL(record));
    } catch (DataRecordException e) {
        throw new RuntimeException("Caught exception trying to set TTL of the document", e);
    }
    if (callback == null) {
        return new WriteResponseFuture<>(observable.timeout(_operationTimeout, _operationTimeunit).toBlocking().toFuture(), _defaultWriteResponseMapper);
    } else {
        final AtomicBoolean callbackFired = new AtomicBoolean(false);
        final BlockingQueue<Pair<WriteResponse, Throwable>> writeResponseQueue = new ArrayBlockingQueue<>(1);
        final Future<WriteResponse> writeResponseFuture = new Future<WriteResponse>() {

            @Override
            public boolean cancel(boolean mayInterruptIfRunning) {
                return false;
            }

            @Override
            public boolean isCancelled() {
                return false;
            }

            @Override
            public boolean isDone() {
                return callbackFired.get();
            }

            @Override
            public WriteResponse get() throws InterruptedException, ExecutionException {
                Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.take();
                return getWriteResponseOrThrow(writeResponseThrowablePair);
            }

            @Override
            public WriteResponse get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                Pair<WriteResponse, Throwable> writeResponseThrowablePair = writeResponseQueue.poll(timeout, unit);
                if (writeResponseThrowablePair == null) {
                    throw new TimeoutException("Timeout exceeded while waiting for future to be done");
                } else {
                    return getWriteResponseOrThrow(writeResponseThrowablePair);
                }
            }
        };
        observable.timeout(_operationTimeout, _operationTimeunit).subscribe(new Subscriber<D>() {

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                callbackFired.set(true);
                writeResponseQueue.add(new Pair<WriteResponse, Throwable>(null, e));
                callback.onFailure(e);
            }

            @Override
            public void onNext(D doc) {
                try {
                    callbackFired.set(true);
                    WriteResponse writeResponse = new GenericWriteResponse<D>(doc);
                    writeResponseQueue.add(new Pair<WriteResponse, Throwable>(writeResponse, null));
                    callback.onSuccess(writeResponse);
                } finally {
                    if (doc instanceof TupleDocument) {
                        ((TupleDocument) doc).content().value1().release();
                    }
                }
            }
        });
        return writeResponseFuture;
    }
}
Also used : WriteResponseFuture(org.apache.gobblin.writer.WriteResponseFuture) WriteResponse(org.apache.gobblin.writer.WriteResponse) GenericWriteResponse(org.apache.gobblin.writer.GenericWriteResponse) TupleDocument(org.apache.gobblin.couchbase.common.TupleDocument) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) WriteResponseFuture(org.apache.gobblin.writer.WriteResponseFuture) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) DataRecordException(org.apache.gobblin.source.extractor.DataRecordException) Pair(org.apache.commons.math3.util.Pair) TimeoutException(java.util.concurrent.TimeoutException)

Example 89 with Pair

use of org.apache.commons.math3.util.Pair in project incubator-gobblin by apache.

the class GobblinMultiTaskAttempt method runWorkUnits.

/**
 * Run a given list of {@link WorkUnit}s of a job.
 *
 * <p>
 *   This method assumes that the given list of {@link WorkUnit}s have already been flattened and
 *   each {@link WorkUnit} contains the task ID in the property {@link ConfigurationKeys#TASK_ID_KEY}.
 * </p>
 *
 * @param countDownLatch a {@link java.util.concurrent.CountDownLatch} waited on for job completion
 * @return a list of {@link Task}s from the {@link WorkUnit}s, as well as if there's a failure in task creation
 * which should be handled separately to avoid silently starving on certain workunit.
 */
private synchronized Pair<List<Task>, Boolean> runWorkUnits(CountUpAndDownLatch countDownLatch) {
    List<Task> tasks = Lists.newArrayList();
    // have been submitted to the underlying task executor.
    if (this.stopped.get()) {
        return new Pair<>(tasks, false);
    }
    // A flag indicating if there are any tasks not submitted successfully.
    // Caller of this method should handle tasks with submission failures accordingly.
    boolean areAllTasksSubmitted = true;
    while (this.workUnits.hasNext()) {
        WorkUnit workUnit = this.workUnits.next();
        String taskId = workUnit.getProp(ConfigurationKeys.TASK_ID_KEY);
        // skip tasks that executed successfully in a prior attempt
        if (taskSuccessfulInPriorAttempt(taskId)) {
            continue;
        }
        SubscopedBrokerBuilder<GobblinScopeTypes, ?> taskBrokerBuilder = this.jobBroker.newSubscopedBuilder(new TaskScopeInstance(taskId));
        WorkUnitState workUnitState = new WorkUnitState(workUnit, this.jobState, taskBrokerBuilder);
        workUnitState.setId(taskId);
        workUnitState.setProp(ConfigurationKeys.JOB_ID_KEY, this.jobId);
        workUnitState.setProp(ConfigurationKeys.TASK_ID_KEY, taskId);
        workUnitState.setProp(ConfigurationKeys.TASK_START_TIME_MILLIS_KEY, Long.toString(System.currentTimeMillis()));
        if (this.containerIdOptional.isPresent()) {
            workUnitState.setProp(ConfigurationKeys.TASK_ATTEMPT_ID_KEY, this.containerIdOptional.get());
        }
        // Create a new task from the work unit and submit the task to run.
        // If an exception occurs here then the count down latch is decremented
        // to avoid being stuck waiting for a task that was not created and submitted successfully.
        Task task = null;
        try {
            countDownLatch.countUp();
            task = createTaskWithRetry(workUnitState, countDownLatch);
            this.taskStateTracker.registerNewTask(task);
            task.setTaskFuture(this.taskExecutor.submit(task));
            tasks.add(task);
        } catch (Throwable e) {
            if (e instanceof OutOfMemoryError) {
                log.error("Encountering memory error in task creation/execution stage, please investigate memory usage:", e);
                printMemoryUsage();
            }
            if (task == null) {
                if (e instanceof RetryException) {
                    // Indicating task being null due to failure in creation even after retrying.
                    areAllTasksSubmitted = false;
                }
                // task could not be created, so directly count down
                countDownLatch.countDown();
                log.error("Could not create task for workunit {}", workUnit, e);
            } else if (!task.hasTaskFuture()) {
                // Task was created and may have been registered, but not submitted, so call the
                // task state tracker task run completion directly since the task cancel does nothing if not submitted
                this.taskStateTracker.onTaskRunCompletion(task);
                areAllTasksSubmitted = false;
                log.error("Could not submit task for workunit {}", workUnit, e);
            } else {
                // task was created and submitted, but failed later, so cancel the task to decrement the CountDownLatch
                task.cancel();
                log.error("Failure after task submitted for workunit {}", workUnit, e);
            }
        }
    }
    EventSubmitter.Builder eventSubmitterBuilder = new EventSubmitter.Builder(JobMetrics.get(this.jobId, new JobMetrics.CreatorTag(this.attemptId)).getMetricContext(), "gobblin.runtime");
    eventSubmitterBuilder.addMetadata(this.taskEventMetadataGenerator.getMetadata(jobState, JobEvent.TASKS_SUBMITTED));
    eventSubmitterBuilder.build().submit(JobEvent.TASKS_SUBMITTED, "tasksCount", Long.toString(countDownLatch.getRegisteredParties()));
    return new Pair<>(tasks, areAllTasksSubmitted);
}
Also used : WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) EventSubmitter(org.apache.gobblin.metrics.event.EventSubmitter) SubscopedBrokerBuilder(org.apache.gobblin.broker.iface.SubscopedBrokerBuilder) TaskScopeInstance(org.apache.gobblin.broker.gobblin_scopes.TaskScopeInstance) RetryException(com.github.rholder.retry.RetryException) JobMetrics(org.apache.gobblin.runtime.util.JobMetrics) GobblinScopeTypes(org.apache.gobblin.broker.gobblin_scopes.GobblinScopeTypes) WorkUnit(org.apache.gobblin.source.workunit.WorkUnit) Pair(org.apache.commons.math3.util.Pair)

Example 90 with Pair

use of org.apache.commons.math3.util.Pair in project android_packages_apps_Dialer by LineageOS.

the class NewVoicemailMediaPlayerView method queryVoicemailSourcePackage.

@Nullable
private Pair<String, Uri> queryVoicemailSourcePackage(Pair<Context, Uri> contextUriPair) {
    LogUtil.enterBlock("NewVoicemailMediaPlayer.queryVoicemailSourcePackage");
    Context context = contextUriPair.first;
    Uri uri = contextUriPair.second;
    String sourcePackage;
    try (Cursor cursor = context.getContentResolver().query(uri, new String[] { Voicemails.SOURCE_PACKAGE }, null, null, null)) {
        if (!hasContent(cursor)) {
            LogUtil.e("NewVoicemailMediaPlayer.queryVoicemailSourcePackage", "uri: %s does not return a SOURCE_PACKAGE", uri.toString());
            sourcePackage = null;
        } else {
            sourcePackage = cursor.getString(0);
            LogUtil.i("NewVoicemailMediaPlayer.queryVoicemailSourcePackage", "uri: %s has a SOURCE_PACKAGE: %s", uri.toString(), sourcePackage);
        }
        LogUtil.i("NewVoicemailMediaPlayer.queryVoicemailSourcePackage", "uri: %s has a SOURCE_PACKAGE: %s", uri.toString(), sourcePackage);
    }
    return new Pair<>(sourcePackage, uri);
}
Also used : Context(android.content.Context) Cursor(android.database.Cursor) Uri(android.net.Uri) Pair(android.support.v4.util.Pair) Nullable(android.support.annotation.Nullable)

Aggregations

Pair (android.support.v4.util.Pair)79 ArrayList (java.util.ArrayList)56 Pair (org.apache.commons.math3.util.Pair)38 List (java.util.List)30 View (android.view.View)28 Collectors (java.util.stream.Collectors)20 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)19 Intent (android.content.Intent)18 Arrays (java.util.Arrays)17 Map (java.util.Map)17 IntStream (java.util.stream.IntStream)17 MaxEval (org.apache.commons.math3.optim.MaxEval)17 HashMap (java.util.HashMap)16 TextView (android.widget.TextView)15 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)15 InitialGuess (org.apache.commons.math3.optim.InitialGuess)14 PointValuePair (org.apache.commons.math3.optim.PointValuePair)14 MultivariateOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer)14 java.util (java.util)13 Collections (java.util.Collections)13