下面列出了com.google.common.base.Predicates#equalTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testSatisfiable() {
Model model2 = context.createModel("test", UUID.randomUUID());
attributeName = "test:test";
thresholdValue = 85.0;
sensitivityValue = 5.0;
Predicate<Address> source = Predicates.equalTo(model.getAddress());
trigger = new ThresholdTrigger(attributeName, thresholdValue, sensitivityValue, TriggerOn.GREATER_THAN, source);
trigger.activate(context);
assertFalse(trigger.isSatisfiable(context));
model.setAttribute(attributeName, new Double(80));
assertTrue(trigger.isSatisfiable(context));
//valueChangeEvent = AttributeValueChangedEvent.create(model.getAddress(), attributeName, new Double(86.0), new Double(84.0));
assertTrue(trigger.shouldFire(context, createValueChange(attributeName, new Double(80), new Double(86))));
assertFalse(trigger.shouldFire(context, createValueChange("test:bad", new Double(80), new Double(86))));
//source does not match
assertFalse(trigger.shouldFire(context, createValueChange(model2, attributeName, new Double(80), new Double(86))));
}
@Test
public void testMultiples() {
Model model2 = context.createModel("test", UUID.randomUUID());
attributeName = "test:test";
thresholdValue = 85.0;
sensitivityValue = 5.0;
Predicate<Address> source = Predicates.equalTo(model.getAddress());
trigger = new ThresholdTrigger(attributeName, thresholdValue, sensitivityValue, TriggerOn.GREATER_THAN, source);
trigger.activate(context);
//valueChangeEvent = AttributeValueChangedEvent.create(model.getAddress(), attributeName, new Double(86.0), new Double(84.0));
assertTrue(trigger.shouldFire(context, createValueChange(attributeName, new Double(80), new Double(86))));
assertFalse(trigger.shouldFire(context, createValueChange(model2, attributeName, new Double(80), new Double(86))));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, new Double(86), new Double(90))));
assertFalse(trigger.shouldFire(context, createValueChange(model2, attributeName, new Double(86), new Double(90))));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, new Double(90), new Double(70))));
assertFalse(trigger.shouldFire(context, createValueChange(model2, attributeName, new Double(90), new Double(70))));
assertFalse(trigger.shouldFire(context, createValueChange(model2, attributeName, new Double(70), new Double(88))));
assertTrue(trigger.shouldFire(context, createValueChange(attributeName, new Double(70), new Double(88))));
}
@Test
public void testGreaterThan() {
attributeName = "test:test";
thresholdValue = 85.0;
sensitivityValue = 5.0;
Predicate<Address> source = Predicates.equalTo(model.getAddress());
trigger = new ThresholdTrigger(attributeName, thresholdValue, sensitivityValue, TriggerOn.GREATER_THAN, source);
trigger.activate(context);
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 65, 80)));
assertTrue(trigger.shouldFire(context, createValueChange(attributeName, 80, 86)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 88, 86)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 90, 88)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 88, 85)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 85, 84)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 84, 80)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 80, 79)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 79, 60)));
}
@Test
public void testLessThan() {
attributeName = "test:test";
thresholdValue = 10.0;
sensitivityValue = 1.8;
Predicate<Address> source = Predicates.equalTo(model.getAddress());
trigger = new ThresholdTrigger(attributeName, thresholdValue, sensitivityValue, TriggerOn.LESS_THAN, source);
trigger.activate(context);
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 15, 11)));
assertTrue(trigger.shouldFire(context, createValueChange(attributeName, 11, 9.9)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 9.9, 8.0)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 8.0, 10)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 10, 11.7)));
assertFalse(trigger.shouldFire(context, createValueChange(attributeName, 11.7, 15)));
}
@Test
public void testOneMatchingModel() {
final RuleContext namespacedContext = new NamespaceContext(model1.getId(), context);
mockAction.execute(namespacedContext);
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
// verify that the address has been changed
assertEquals(model1.getAddress(), namespacedContext.getVariable(targetVariable));
return ActionState.FIRING;
}
});
EasyMock.replay(mockAction);
ForEachModelAction selector = new ForEachModelAction(mockAction, Predicates.<Model>alwaysTrue(), Predicates.<Model>equalTo(model1), "address");
selector.execute(context);
assertNull(context.getVariable(targetVariable));
assertEquals(ImmutableList.<Address>of(model1.getAddress()),context.getVariable(ForEachModelAction.VAR_STILL_FIRING));
EasyMock.verify(mockAction);
}
@Override
public Condition generate(Map<String, Object> values) {
Preconditions.checkState(messageType != null, "must specify a message type");
Predicate<Address> fromPred = this.source == null ?
Predicates.alwaysTrue() :
Predicates.equalTo( FunctionFactory.toAddress(this.source.toTemplate(), values) );
Predicate<String> messageTypePred = Predicates.equalTo( FunctionFactory.toString(this.messageType.toTemplate(), values) );
Predicate<Map<String,Object>> attributesPred;
if(this.attributes.isEmpty()) {
attributesPred = Predicates.alwaysTrue();
}
else {
Map<String, Predicate<? super Object>> matches = new HashMap<>();
for(Map.Entry<String, TemplatedExpression> attributeAndExpression: this.attributes.entrySet()) {
String attribute = attributeAndExpression.getKey();
TemplatedExpression expression = attributeAndExpression.getValue();
matches.put(
attribute,
// TODO more options than equalTo?
Predicates.equalTo( expression.toTemplate().apply(values) )
);
}
attributesPred = FunctionFactory.containsAll(matches);
}
ReceivedMessageCondition condition = new ReceivedMessageCondition(messageTypePred,fromPred,attributesPred);
return condition;
}
public void testSomePathRevisiting() {
DiGraph<String, String> g = LinkedDirectedGraph.create();
g.createDirectedGraphNode("1");
g.createDirectedGraphNode("2a");
g.createDirectedGraphNode("2b");
g.createDirectedGraphNode("3");
g.createDirectedGraphNode("4a");
g.createDirectedGraphNode("4b");
g.createDirectedGraphNode("5");
g.connect("1", "-", "2a");
g.connect("1", "-", "2b");
g.connect("2a", "-", "3");
g.connect("2b", "-", "3");
g.connect("3", "-", "4a");
g.connect("3", "-", "4b");
g.connect("4a", "-", "5");
g.connect("4b", "-", "5");
CountingPredicate<String> p =
new CountingPredicate<String>(Predicates.equalTo("4a"));
assertTrue(createTest(g, "1", "5", p, ALL_EDGE)
.somePathsSatisfyPredicate());
// Make sure we are not doing more traversals than we have to.
assertEquals(4, p.count);
}
VerifierBuilder resolveAllUriAsync_ForceRefresh(boolean forceRefresh) {
methodName(resolveAllUriAsync);
Condition alwaysTrue = new Condition(Predicates.alwaysTrue(), "no condition");
Condition forceRefreshCond = new Condition(Predicates.equalTo(forceRefresh), String.format("%b (forceRefresh)", forceRefresh));
resolveAllUriAsync(alwaysTrue, alwaysTrue, forceRefreshCond);
return this;
}
public Builder eqURI(String uri) {
try {
this.uriPredicate = Predicates.equalTo(new URI(uri));
} catch (URISyntaxException e) {
throw new RuntimeException("invalid URI: " + uri, e);
}
return this;
}
@Nonnull
public static Predicate<ContentFolderTypeProvider> of(@Nonnull ContentFolderTypeProvider provider) {
return Predicates.equalTo(provider);
}
public Builder eqURI(URI uri) {
this.uriPredicate = Predicates.equalTo(uri);
return this;
}
@Override
public Predicate<Address> apply(String value) {
Address address = Address.fromString(value);
return Predicates.equalTo(address);
}
@Override
public Predicate<String> apply(String value) {
return Predicates.equalTo(value);
}
public EqualsCheck(Function<Void, T> actual, T expected, String message) {
this.eqToExpected = Predicates.equalTo(expected);
this.message = message;
this.actual = actual;
}
/**
* Returns the matching table report entries for a report ID and query parameters.
*/
@Override
public Iterable<TableReportEntry> getReportEntries(String reportId, final AllTablesReportQuery query) {
checkNotNull(reportId, "reportId");
final String reportTable = getTableName(reportId);
// Set up several filters based on the query attributes
final Predicate<String> placementFilter = query.getPlacements().isEmpty()
? Predicates.<String>alwaysTrue()
: Predicates.in(query.getPlacements());
final Predicate<Boolean> droppedFilter = query.isIncludeDropped()
? Predicates.<Boolean>alwaysTrue()
: Predicates.equalTo(false);
final Predicate<Boolean> facadeFilter = query.isIncludeFacades()
? Predicates.<Boolean>alwaysTrue()
: Predicates.equalTo(false);
return new Iterable<TableReportEntry>() {
@Override
public Iterator<TableReportEntry> iterator() {
return Iterators.limit(
Iterators.filter(
Iterators.transform(
queryDataStoreForTableReportResults(reportTable, query),
new Function<Map<String, Object>, TableReportEntry>() {
@Nullable
@Override
public TableReportEntry apply(Map<String, Object> map) {
if (Intrinsic.getId(map).startsWith("~")) {
// Skip this row, it's the metadata
return null;
}
return convertToTableReportEntry(
map, placementFilter, droppedFilter, facadeFilter);
}
}),
Predicates.notNull()
),
query.getLimit()
);
}
};
}
private boolean inlineAliasIfPossible(Ref alias, GlobalNamespace namespace) {
// Ensure that the alias is assigned to a local variable at that
// variable's declaration. If the alias's parent is a NAME,
// then the NAME must be the child of a VAR node, and we must
// be in a VAR assignment.
Node aliasParent = alias.node.getParent();
if (aliasParent.isName()) {
// Ensure that the local variable is well defined and never reassigned.
Scope scope = alias.scope;
Var aliasVar = scope.getVar(aliasParent.getString());
ReferenceCollectingCallback collector =
new ReferenceCollectingCallback(compiler,
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR,
Predicates.<Var>equalTo(aliasVar));
(new NodeTraversal(compiler, collector)).traverseAtScope(scope);
ReferenceCollection aliasRefs = collector.getReferences(aliasVar);
if (aliasRefs.isWellDefined()
&& aliasRefs.firstReferenceIsAssigningDeclaration()
&& aliasRefs.isAssignedOnceInLifetime()) {
// The alias is well-formed, so do the inlining now.
int size = aliasRefs.references.size();
Set<Node> newNodes = Sets.newHashSetWithExpectedSize(size - 1);
for (int i = 1; i < size; i++) {
ReferenceCollectingCallback.Reference aliasRef =
aliasRefs.references.get(i);
Node newNode = alias.node.cloneTree();
aliasRef.getParent().replaceChild(aliasRef.getNode(), newNode);
newNodes.add(newNode);
}
// just set the original alias to null.
aliasParent.replaceChild(alias.node, IR.nullNode());
compiler.reportCodeChange();
// Inlining the variable may have introduced new references
// to descendants of {@code name}. So those need to be collected now.
namespace.scanNewNodes(alias.scope, newNodes);
return true;
}
}
return false;
}
private boolean inlineAliasIfPossible(Ref alias, GlobalNamespace namespace) {
// Ensure that the alias is assigned to a local variable at that
// variable's declaration. If the alias's parent is a NAME,
// then the NAME must be the child of a VAR node, and we must
// be in a VAR assignment.
Node aliasParent = alias.node.getParent();
if (aliasParent.isName()) {
// Ensure that the local variable is well defined and never reassigned.
Scope scope = alias.scope;
Var aliasVar = scope.getVar(aliasParent.getString());
ReferenceCollectingCallback collector =
new ReferenceCollectingCallback(compiler,
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR,
Predicates.<Var>equalTo(aliasVar));
(new NodeTraversal(compiler, collector)).traverseAtScope(scope);
ReferenceCollection aliasRefs = collector.getReferences(aliasVar);
if (aliasRefs.isWellDefined()
&& aliasRefs.firstReferenceIsAssigningDeclaration()
&& aliasRefs.isAssignedOnceInLifetime()) {
// The alias is well-formed, so do the inlining now.
int size = aliasRefs.references.size();
Set<Node> newNodes = Sets.newHashSetWithExpectedSize(size - 1);
for (int i = 1; i < size; i++) {
ReferenceCollectingCallback.Reference aliasRef =
aliasRefs.references.get(i);
Node newNode = alias.node.cloneTree();
aliasRef.getParent().replaceChild(aliasRef.getNode(), newNode);
newNodes.add(newNode);
}
// just set the original alias to null.
aliasParent.replaceChild(alias.node, IR.nullNode());
compiler.reportCodeChange();
// Inlining the variable may have introduced new references
// to descendants of {@code name}. So those need to be collected now.
namespace.scanNewNodes(alias.scope, newNodes);
return true;
}
}
return false;
}
private boolean inlineAliasIfPossible(Ref alias, GlobalNamespace namespace) {
// Ensure that the alias is assigned to a local variable at that
// variable's declaration. If the alias's parent is a NAME,
// then the NAME must be the child of a VAR node, and we must
// be in a VAR assignment.
Node aliasParent = alias.node.getParent();
if (aliasParent.getType() == Token.NAME) {
// Ensure that the local variable is well defined and never reassigned.
Scope scope = alias.scope;
Var aliasVar = scope.getVar(aliasParent.getString());
ReferenceCollectingCallback collector =
new ReferenceCollectingCallback(compiler,
ReferenceCollectingCallback.DO_NOTHING_BEHAVIOR,
Predicates.<Var>equalTo(aliasVar));
(new NodeTraversal(compiler, collector)).traverseAtScope(scope);
ReferenceCollection aliasRefs =
collector.getReferenceCollection(aliasVar);
if (aliasRefs.isWellDefined()
&& aliasRefs.firstReferenceIsAssigningDeclaration()
&& aliasRefs.isAssignedOnceInLifetime()) {
// The alias is well-formed, so do the inlining now.
int size = aliasRefs.references.size();
Set<Node> newNodes = Sets.newHashSetWithExpectedSize(size - 1);
for (int i = 1; i < size; i++) {
ReferenceCollectingCallback.Reference aliasRef =
aliasRefs.references.get(i);
Node newNode = alias.node.cloneTree();
aliasRef.getParent().replaceChild(aliasRef.getNameNode(), newNode);
newNodes.add(newNode);
}
// just set the original alias to null.
aliasParent.replaceChild(alias.node, new Node(Token.NULL));
compiler.reportCodeChange();
// Inlining the variable may have introduced new references
// to descendents of {@code name}. So those need to be collected now.
namespace.scanNewNodes(alias.scope, newNodes);
return true;
}
}
return false;
}
public Builder eqVersion(String version) {
this.versionPredicate = Predicates.equalTo(version);
return this;
}
@Nonnull
public static Predicate<ContentFolderTypeProvider> onlyProduction() {
return Predicates.<ContentFolderTypeProvider>equalTo(ProductionContentFolderTypeProvider.getInstance());
}