Search in sources :

Example 26 with Predicate

use of java.util.function.Predicate in project jena by apache.

the class PathLib method existsPath.

private static int existsPath(Graph graph, Node subject, Path path, final Node object, ExecutionContext execCxt) {
    if (!subject.isConcrete() || !object.isConcrete())
        throw new ARQInternalErrorException("Non concrete node for existsPath evaluation");
    Iterator<Node> iter = PathEval.eval(graph, subject, path, execCxt.getContext());
    Predicate<Node> filter = node -> Objects.equals(node, object);
    // See if we got to the node we're interested in finishing at.
    iter = Iter.filter(iter, filter);
    long x = Iter.count(iter);
    return (int) x;
}
Also used : Context(org.apache.jena.sparql.util.Context) Binding(org.apache.jena.sparql.engine.binding.Binding) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) BasicPattern(org.apache.jena.sparql.core.BasicPattern) QueryIterConcat(org.apache.jena.sparql.engine.iterator.QueryIterConcat) TriplePath(org.apache.jena.sparql.core.TriplePath) OpPath(org.apache.jena.sparql.algebra.op.OpPath) OpSequence(org.apache.jena.sparql.algebra.op.OpSequence) Graph(org.apache.jena.graph.Graph) ArrayList(java.util.ArrayList) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) QueryIterYieldN(org.apache.jena.sparql.engine.iterator.QueryIterYieldN) Op(org.apache.jena.sparql.algebra.Op) GraphUtils(org.apache.jena.sparql.util.graph.GraphUtils) Iter(org.apache.jena.atlas.iterator.Iter) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) BindingFactory(org.apache.jena.sparql.engine.binding.BindingFactory) PathEval(org.apache.jena.sparql.path.eval.PathEval) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) Triple(org.apache.jena.graph.Triple) QueryIterPlainWrapper(org.apache.jena.sparql.engine.iterator.QueryIterPlainWrapper) PropertyFunctionFactory(org.apache.jena.sparql.pfunction.PropertyFunctionFactory) PathBlock(org.apache.jena.sparql.core.PathBlock) Objects(java.util.Objects) List(java.util.List) Explain(org.apache.jena.sparql.mgt.Explain) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) PropertyFunctionRegistry(org.apache.jena.sparql.pfunction.PropertyFunctionRegistry) ARQInternalErrorException(org.apache.jena.sparql.ARQInternalErrorException) Node(org.apache.jena.graph.Node)

Example 27 with Predicate

use of java.util.function.Predicate in project jena by apache.

the class TestQuadFilter method createFilter.

/** Create a filter to exclude the graph http://example/g2 */
private static Predicate<Tuple<NodeId>> createFilter(Dataset ds) {
    DatasetGraphTDB dsg = (DatasetGraphTDB) (ds.asDatasetGraph());
    final NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable();
    final NodeId target = nodeTable.getNodeIdForNode(NodeFactory.createURI(graphToHide));
    return item -> !(item.len() == 4 && item.get(0).equals(target));
}
Also used : AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) NodeFactory(org.apache.jena.graph.NodeFactory) org.apache.jena.query(org.apache.jena.query) Predicate(java.util.function.Predicate) Test(org.junit.Test) SystemTDB(org.apache.jena.tdb.sys.SystemTDB) NodeId(org.apache.jena.tdb.store.NodeId) SSE(org.apache.jena.sparql.sse.SSE) DatasetGraphTDB(org.apache.jena.tdb.store.DatasetGraphTDB) BaseTest(org.apache.jena.atlas.junit.BaseTest) NodeTable(org.apache.jena.tdb.store.nodetable.NodeTable) Quad(org.apache.jena.sparql.core.Quad) TDBFactory(org.apache.jena.tdb.TDBFactory) Tuple(org.apache.jena.atlas.lib.tuple.Tuple) TDB(org.apache.jena.tdb.TDB) NodeId(org.apache.jena.tdb.store.NodeId) NodeTable(org.apache.jena.tdb.store.nodetable.NodeTable) DatasetGraphTDB(org.apache.jena.tdb.store.DatasetGraphTDB)

Example 28 with Predicate

use of java.util.function.Predicate in project lucene-solr by apache.

the class TestAuthorizationFramework method verifySecurityStatus.

public static void verifySecurityStatus(HttpClient cl, String url, String objPath, Object expected, int count) throws Exception {
    boolean success = false;
    String s = null;
    List<String> hierarchy = StrUtils.splitSmart(objPath, '/');
    for (int i = 0; i < count; i++) {
        HttpGet get = new HttpGet(url);
        s = EntityUtils.toString(cl.execute(get, HttpClientUtil.createNewHttpClientRequestContext()).getEntity());
        Map m = (Map) Utils.fromJSONString(s);
        Object actual = Utils.getObjectByPath(m, true, hierarchy);
        if (expected instanceof Predicate) {
            Predicate predicate = (Predicate) expected;
            if (predicate.test(actual)) {
                success = true;
                break;
            }
        } else if (Objects.equals(String.valueOf(actual), expected)) {
            success = true;
            break;
        }
        Thread.sleep(50);
    }
    assertTrue("No match for " + objPath + " = " + expected + ", full response = " + s, success);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) Map(java.util.Map) Predicate(java.util.function.Predicate)

Example 29 with Predicate

use of java.util.function.Predicate in project lucene-solr by apache.

the class HttpShardHandler method prepDistributed.

@Override
public void prepDistributed(ResponseBuilder rb) {
    final SolrQueryRequest req = rb.req;
    final SolrParams params = req.getParams();
    final String shards = params.get(ShardParams.SHARDS);
    // since the cost of grabbing cloud state is still up in the air, we grab it only
    // if we need it.
    ClusterState clusterState = null;
    Map<String, Slice> slices = null;
    CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
    CloudDescriptor cloudDescriptor = coreDescriptor.getCloudDescriptor();
    ZkController zkController = req.getCore().getCoreContainer().getZkController();
    final ReplicaListTransformer replicaListTransformer = httpShardHandlerFactory.getReplicaListTransformer(req);
    if (shards != null) {
        List<String> lst = StrUtils.splitSmart(shards, ",", true);
        rb.shards = lst.toArray(new String[lst.size()]);
        rb.slices = new String[rb.shards.length];
        if (zkController != null) {
            // figure out which shards are slices
            for (int i = 0; i < rb.shards.length; i++) {
                if (rb.shards[i].indexOf('/') < 0) {
                    // this is a logical shard
                    rb.slices[i] = rb.shards[i];
                    rb.shards[i] = null;
                }
            }
        }
    } else if (zkController != null) {
        // we weren't provided with an explicit list of slices to query via "shards", so use the cluster state
        clusterState = zkController.getClusterState();
        String shardKeys = params.get(ShardParams._ROUTE_);
        // This will be the complete list of slices we need to query for this request.
        slices = new HashMap<>();
        // we need to find out what collections this request is for.
        // A comma-separated list of specified collections.
        // Eg: "collection1,collection2,collection3"
        String collections = params.get("collection");
        if (collections != null) {
            // If there were one or more collections specified in the query, split
            // each parameter and store as a separate member of a List.
            List<String> collectionList = StrUtils.splitSmart(collections, ",", true);
            // cloud state and add them to the Map 'slices'.
            for (String collectionName : collectionList) {
                // The original code produced <collection-name>_<shard-name> when the collections
                // parameter was specified (see ClientUtils.appendMap)
                // Is this necessary if ony one collection is specified?
                // i.e. should we change multiCollection to collectionList.size() > 1?
                addSlices(slices, clusterState, params, collectionName, shardKeys, true);
            }
        } else {
            // just this collection
            String collectionName = cloudDescriptor.getCollectionName();
            addSlices(slices, clusterState, params, collectionName, shardKeys, false);
        }
        // Store the logical slices in the ResponseBuilder and create a new
        // String array to hold the physical shards (which will be mapped
        // later).
        rb.slices = slices.keySet().toArray(new String[slices.size()]);
        rb.shards = new String[rb.slices.length];
    }
    //
    if (zkController != null) {
        // Are we hosting the shard that this request is for, and are we active? If so, then handle it ourselves
        // and make it a non-distributed request.
        String ourSlice = cloudDescriptor.getShardId();
        String ourCollection = cloudDescriptor.getCollectionName();
        // Some requests may only be fulfilled by replicas of type Replica.Type.NRT
        boolean onlyNrtReplicas = Boolean.TRUE == req.getContext().get(ONLY_NRT_REPLICAS);
        if (rb.slices.length == 1 && rb.slices[0] != null && // handle the <collection>_<slice> format
        (rb.slices[0].equals(ourSlice) || rb.slices[0].equals(ourCollection + "_" + ourSlice)) && cloudDescriptor.getLastPublished() == Replica.State.ACTIVE && (!onlyNrtReplicas || cloudDescriptor.getReplicaType() == Replica.Type.NRT)) {
            // currently just a debugging parameter to check distrib search on a single node
            boolean shortCircuit = params.getBool("shortCircuit", true);
            String targetHandler = params.get(ShardParams.SHARDS_QT);
            // if a different handler is specified, don't short-circuit
            shortCircuit = shortCircuit && targetHandler == null;
            if (shortCircuit) {
                rb.isDistrib = false;
                rb.shortCircuitedURL = ZkCoreNodeProps.getCoreUrl(zkController.getBaseUrl(), coreDescriptor.getName());
                return;
            }
        // We shouldn't need to do anything to handle "shard.rows" since it was previously meant to be an optimization?
        }
        for (int i = 0; i < rb.shards.length; i++) {
            final List<String> shardUrls;
            if (rb.shards[i] != null) {
                shardUrls = StrUtils.splitSmart(rb.shards[i], "|", true);
                replicaListTransformer.transform(shardUrls);
            } else {
                if (clusterState == null) {
                    clusterState = zkController.getClusterState();
                    slices = clusterState.getSlicesMap(cloudDescriptor.getCollectionName());
                }
                String sliceName = rb.slices[i];
                Slice slice = slices.get(sliceName);
                if (slice == null) {
                    // Treat this the same as "all servers down" for a slice, and let things continue
                    // if partial results are acceptable
                    rb.shards[i] = "";
                    continue;
                // throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "no such shard: " + sliceName);
                }
                final Predicate<Replica> isShardLeader = new Predicate<Replica>() {

                    private Replica shardLeader = null;

                    @Override
                    public boolean test(Replica replica) {
                        if (shardLeader == null) {
                            try {
                                shardLeader = zkController.getZkStateReader().getLeaderRetry(cloudDescriptor.getCollectionName(), slice.getName());
                            } catch (InterruptedException e) {
                                throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "Exception finding leader for shard " + slice.getName() + " in collection " + cloudDescriptor.getCollectionName(), e);
                            } catch (SolrException e) {
                                if (log.isDebugEnabled()) {
                                    log.debug("Exception finding leader for shard {} in collection {}. Collection State: {}", slice.getName(), cloudDescriptor.getCollectionName(), zkController.getZkStateReader().getClusterState().getCollectionOrNull(cloudDescriptor.getCollectionName()));
                                }
                                throw e;
                            }
                        }
                        return replica.getName().equals(shardLeader.getName());
                    }
                };
                final List<Replica> eligibleSliceReplicas = collectEligibleReplicas(slice, clusterState, onlyNrtReplicas, isShardLeader);
                replicaListTransformer.transform(eligibleSliceReplicas);
                shardUrls = new ArrayList<>(eligibleSliceReplicas.size());
                for (Replica replica : eligibleSliceReplicas) {
                    String url = ZkCoreNodeProps.getCoreUrl(replica);
                    shardUrls.add(url);
                }
                if (shardUrls.isEmpty()) {
                    boolean tolerant = rb.req.getParams().getBool(ShardParams.SHARDS_TOLERANT, false);
                    if (!tolerant) {
                        // stop the check when there are no replicas available for a shard
                        throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "no servers hosting shard: " + rb.slices[i]);
                    }
                }
            }
            // And now recreate the | delimited list of equivalent servers
            rb.shards[i] = createSliceShardsStr(shardUrls);
        }
    }
    String shards_rows = params.get(ShardParams.SHARDS_ROWS);
    if (shards_rows != null) {
        rb.shards_rows = Integer.parseInt(shards_rows);
    }
    String shards_start = params.get(ShardParams.SHARDS_START);
    if (shards_start != null) {
        rb.shards_start = Integer.parseInt(shards_start);
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) HashMap(java.util.HashMap) CoreDescriptor(org.apache.solr.core.CoreDescriptor) Replica(org.apache.solr.common.cloud.Replica) CloudDescriptor(org.apache.solr.cloud.CloudDescriptor) Predicate(java.util.function.Predicate) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) Slice(org.apache.solr.common.cloud.Slice) ZkController(org.apache.solr.cloud.ZkController) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) SolrException(org.apache.solr.common.SolrException)

Example 30 with Predicate

use of java.util.function.Predicate in project sling by apache.

the class ValidationServiceImplTest method testValidateResourceRecursively.

@Test()
public void testValidateResourceRecursively() throws Exception {
    modelBuilder.resourceProperty(propertyBuilder.build("field1"));
    final ValidationModel vm1 = modelBuilder.build("resourcetype1", "some source");
    modelBuilder = new ValidationModelBuilder();
    modelBuilder.resourceProperty(propertyBuilder.build("field2"));
    final ValidationModel vm2 = modelBuilder.build("resourcetype2", "some source");
    // set model retriever
    validationService.modelRetriever = new ValidationModelRetriever() {

        @Override
        @CheckForNull
        public ValidationModel getValidationModel(@Nonnull String resourceType, String resourcePath, boolean considerResourceSuperTypeModels) {
            if (resourceType.equals("resourcetype1")) {
                return vm1;
            } else if (resourceType.equals("resourcetype2")) {
                return vm2;
            } else {
                return null;
            }
        }
    };
    ResourceResolver rr = context.resourceResolver();
    // resource is lacking the required field (is invalid)
    Resource testResource = ResourceUtil.getOrCreateResource(rr, "/content/validation/1/resource", "resourcetype1", JcrConstants.NT_UNSTRUCTURED, true);
    // child1 is valid
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype2");
    properties.put("field2", "test");
    rr.create(testResource, "child1", properties);
    // child2 is invalid
    properties.clear();
    properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype2");
    rr.create(testResource, "child2", properties);
    // child3 has no model (but its resource type is ignored)
    properties.clear();
    properties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "resourcetype3");
    rr.create(testResource, "child3", properties);
    final Predicate<Resource> ignoreResourceType3Filter = new Predicate<Resource>() {

        @Override
        public boolean test(final Resource resource) {
            return !"resourcetype3".equals(resource.getResourceType());
        }
    };
    ValidationResult vr = validationService.validateResourceRecursively(testResource, true, ignoreResourceType3Filter, false);
    Assert.assertFalse("resource should have been considered invalid", vr.isValid());
    Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field1"), new DefaultValidationFailure("child2", 20, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_MISSING_REQUIRED_PROPERTY_WITH_NAME, "field2")));
}
Also used : HashMap(java.util.HashMap) ValidationModelRetriever(org.apache.sling.validation.model.spi.ValidationModelRetriever) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.validation.model.ChildResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Predicate(java.util.function.Predicate) ValidationModel(org.apache.sling.validation.model.ValidationModel) CheckForNull(javax.annotation.CheckForNull) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) ValidationModelBuilder(org.apache.sling.validation.impl.model.ValidationModelBuilder) Test(org.junit.Test)

Aggregations

Predicate (java.util.function.Predicate)76 List (java.util.List)34 Map (java.util.Map)21 Arrays (java.util.Arrays)19 IOException (java.io.IOException)18 Collections (java.util.Collections)16 Collectors (java.util.stream.Collectors)16 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 Assert.assertEquals (org.junit.Assert.assertEquals)10 Mockito.mock (org.mockito.Mockito.mock)9 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)7 Optional (java.util.Optional)7 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 BufferedReader (java.io.BufferedReader)6 Set (java.util.Set)6 SortedSet (java.util.SortedSet)6 Before (org.junit.Before)6