Permalink
Browse files

Check in new collection capabilities and the LiftedCapability class

  • Loading branch information...
twschiller committed Oct 14, 2013
1 parent 56f46d0 commit 7359fb4a8be79a8c8e1abc7de6d936df59544021
@@ -0,0 +1,102 @@
package edu.washington.cs.cupid.wizards.internal;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import edu.washington.cs.cupid.CupidPlatform;
import edu.washington.cs.cupid.capability.CapabilityJob;
import edu.washington.cs.cupid.capability.CapabilityStatus;
import edu.washington.cs.cupid.capability.CapabilityUtil;
import edu.washington.cs.cupid.capability.ICapability;
import edu.washington.cs.cupid.capability.linear.GenericLinearSerializableCapability;
import edu.washington.cs.cupid.capability.linear.LinearJob;
import edu.washington.cs.cupid.capability.linear.LinearStatus;
public class LiftedCapability<I,V> extends GenericLinearSerializableCapability<List<I>, List<V>>{
private static final long serialVersionUID = 1L;
private ICapability capability;
private String capabilityName;
private TypeToken<I> inputType;
private TypeToken<V> outputType;
public LiftedCapability(ICapability capability) {
super("Lifted: " + capability.getName(),
"Perform " + capability.getName() + " on a list",
capability.getFlags().toArray(new ICapability.Flag[]{}));
this.inputType = (TypeToken<I>) CapabilityUtil.unaryParameter(capability).getType();
this.outputType = (TypeToken<V>) CapabilityUtil.singleOutput(capability).getType();
if (CapabilityUtil.isSerializable(capability)){
this.capability = capability;
this.capabilityName = null;
}else{
this.capability = null;
this.capabilityName = capability.getName();
}
}
@Override
public LinearJob<List<I>, List<V>> getJob(List<I> input) {
return new LinearJob<List<I>, List<V>>(this, input){
@Override
protected LinearStatus<List<V>> run(final IProgressMonitor monitor) {
try{
ICapability c = capability != null ? capability : CupidPlatform.getCapabilityRegistry().findCapability(capabilityName);
List<I> input = getInput();
monitor.beginTask(getName(), input.size());
List<V> result = Lists.newArrayList();
for (I elt : input){
if (monitor.isCanceled()){
throw new InterruptedException("Capability Job was cancelled");
}
CapabilityJob<?> subtask = c.getJob(CapabilityUtil.packUnaryInput(c, elt));
subtask.schedule();
subtask.join();
CapabilityStatus status = (CapabilityStatus) subtask.getResult();
if (!status.isOK() || status.getException() != null){
throw status.getException();
}
result.add((V) CapabilityUtil.singleOutputValue(c, status));
monitor.worked(1);
}
return LinearStatus.makeOk(getCapability(), result);
}catch(Throwable t){
return LinearStatus.<List<V>>makeError(t);
}finally{
monitor.done();
}
}
};
}
@Override
public TypeToken<List<I>> getInputType() {
return new TypeToken<List<I>>(getClass()){}
.where(new TypeParameter<I>(){}, inputType );
}
@Override
public TypeToken<List<V>> getOutputType() {
return new TypeToken<List<V>>(getClass()){}
.where(new TypeParameter<V>(){}, outputType);
}
}
@@ -0,0 +1,54 @@
package edu.washington.cs.cupid.standard;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;
import com.google.common.reflect.TypeToken;
import edu.washington.cs.cupid.capability.ICapability.Flag;
import edu.washington.cs.cupid.capability.linear.GenericLinearCapability;
import edu.washington.cs.cupid.capability.linear.ImmediateJob;
import edu.washington.cs.cupid.capability.linear.LinearCapability;
import edu.washington.cs.cupid.capability.linear.LinearJob;
import edu.washington.cs.cupid.capability.linear.LinearStatus;
public class Any extends LinearCapability<Collection<Boolean>, Boolean> {
/**
* A capability that indicates whether any of the elements in the collection are true
*/
public Any() {
super("Any",
"True if any of the elements are true",
new TypeToken<Collection<Boolean>>(){},
TypeToken.of(Boolean.class),
Flag.PURE);
}
@Override
public LinearJob<Collection<Boolean>, Boolean> getJob(Collection<Boolean> input) {
return new LinearJob<Collection<Boolean>, Boolean> (this, input) {
@Override
protected LinearStatus<Boolean> run(final IProgressMonitor monitor) {
try {
monitor.beginTask(getName(), getInput().size());
for (Boolean elt : getInput()){
if (elt) return LinearStatus.makeOk(Any.this, true);
monitor.worked(1);
}
return LinearStatus.makeOk(Any.this, false);
} catch (Exception ex) {
return LinearStatus.<Boolean>makeError(ex);
} finally {
monitor.done();
}
}
};
}
}
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2013 Todd Schiller.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Todd Schiller - initial API, implementation, and documentation
******************************************************************************/
package edu.washington.cs.cupid.standard;
import java.util.Collection;
import java.util.Set;
import com.google.common.collect.Sets;
import com.google.common.reflect.TypeToken;
import edu.washington.cs.cupid.capability.linear.GenericLinearCapability;
import edu.washington.cs.cupid.capability.linear.ImmediateJob;
import edu.washington.cs.cupid.capability.linear.LinearJob;
/**
* A capability that returns the number of elements in a collection.
* @author Todd Schiller
* @param <V> element type
*/
public final class Distinct<V> extends GenericLinearCapability<Collection<V>, Set<V>> {
/**
* A capability that returns the number of elements in a collection.
*/
public Distinct() {
super(
"Disinct",
"Returns the distinct elements in a collection",
Flag.PURE);
}
@Override
public TypeToken<Collection<V>> getInputType() {
return new TypeToken<Collection<V>>(getClass()) {
private static final long serialVersionUID = 1L;
};
}
@Override
public TypeToken<Set<V>> getOutputType() {
return new TypeToken<Set<V>>(getClass()) {
private static final long serialVersionUID = 1L;
};
}
@Override
public LinearJob<Collection<V>, Set<V>> getJob(final Collection<V> input) {
return new ImmediateJob<Collection<V>, Set<V>>(this, input, Sets.newHashSet(input));
}
}
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2013 Todd Schiller.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Todd Schiller - initial API, implementation, and documentation
******************************************************************************/
package edu.washington.cs.cupid.standard;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;
import com.google.common.reflect.TypeToken;
import edu.washington.cs.cupid.capability.linear.GenericLinearCapability;
import edu.washington.cs.cupid.capability.linear.ImmediateJob;
import edu.washington.cs.cupid.capability.linear.LinearJob;
import edu.washington.cs.cupid.capability.linear.LinearStatus;
/**
* A capability that computes the most frequent element in a collection.
* @author Todd Schiller
* @param <V> the collection element type
*/
public final class Last<V> extends GenericLinearCapability<List<V>, V> {
// TODO make efficient
/**
* A capability that computes the most last element in a collection.
*/
public Last() {
super(
"Last",
"Gets the last item in a list",
Flag.PURE);
}
@Override
public ImmediateJob<List<V>, V> getJob(final List<V> input) {
if (input.isEmpty()){
return new ImmediateJob<List<V>, V>(this, input, new IllegalArgumentException("Input cannot be empty"));
}else{
return new ImmediateJob<List<V>, V>(this, input, input.get(input.size()-1));
}
}
@Override
public TypeToken<List<V>> getInputType() {
return new TypeToken<List<V>>(getClass()) {};
}
@Override
public TypeToken<V> getOutputType() {
return new TypeToken<V>(getClass()) {};
}
}

0 comments on commit 7359fb4

Please sign in to comment.