Search in sources :

Example 1 with UserFunction

use of org.neo4j.procedure.UserFunction in project neo4j by neo4j.

the class UserFunctionVisitor method validateName.

private Stream<CompilationMessage> validateName(ExecutableElement method) {
    UserFunction function = method.getAnnotation(UserFunction.class);
    String name = function.name();
    if (!name.isEmpty() && isInRootNamespace(name)) {
        return Stream.of(rootNamespaceError(method, name));
    }
    String value = function.value();
    if (!value.isEmpty() && isInRootNamespace(value)) {
        return Stream.of(rootNamespaceError(method, value));
    }
    PackageElement namespace = elements.getPackageOf(method);
    if (namespace == null) {
        return Stream.of(rootNamespaceError(method));
    }
    return Stream.empty();
}
Also used : UserFunction(org.neo4j.procedure.UserFunction) PackageElement(javax.lang.model.element.PackageElement)

Example 2 with UserFunction

use of org.neo4j.procedure.UserFunction in project neo4j-apoc-procedures by neo4j-contrib.

the class Numbers method format.

@UserFunction
@Description("apoc.number.format(number)  | format a long or double using the default system pattern and language to produce a string")
public String format(@Name("number") final Object value, @Name(value = "pattern", defaultValue = "") String pattern, @Name(value = "lang", defaultValue = "") String lang) {
    Number number = validateNumberParam(value);
    if (number == null)
        return null;
    DecimalFormat format = buildFormatter(pattern, lang);
    if (format == null)
        return null;
    return format.format(number);
}
Also used : DecimalFormat(java.text.DecimalFormat) Description(org.neo4j.procedure.Description) UserFunction(org.neo4j.procedure.UserFunction)

Example 3 with UserFunction

use of org.neo4j.procedure.UserFunction in project neo4j-apoc-procedures by neo4j-contrib.

the class Strings method random.

@UserFunction
@Description("apoc.text.random(length, valid) YIELD value - generate a random string")
public String random(@Name("length") final long length, @Name(value = "valid", defaultValue = "A-Za-z0-9") String valid) {
    valid = valid.replaceAll("A-Z", upper).replaceAll("a-z", lower).replaceAll("0-9", numeric);
    StringBuilder output = new StringBuilder(toIntExact(length));
    ThreadLocalRandom rand = ThreadLocalRandom.current();
    while (output.length() < length) {
        output.append(valid.charAt(rand.nextInt(valid.length())));
    }
    return output.toString();
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Description(org.neo4j.procedure.Description) UserFunction(org.neo4j.procedure.UserFunction)

Example 4 with UserFunction

use of org.neo4j.procedure.UserFunction in project neo4j by neo4j.

the class ProcedureCompiler method compileFunction.

private CallableUserFunction compileFunction(Class<?> procDefinition, Method method, QualifiedName procName) throws ProcedureException {
    restrictions.verify(procName);
    List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(method);
    Class<?> returnType = method.getReturnType();
    TypeCheckers.TypeChecker typeChecker = typeCheckers.checkerFor(returnType);
    String description = description(method);
    UserFunction function = method.getAnnotation(UserFunction.class);
    String deprecated = deprecated(method, function::deprecatedBy, "Use of @UserFunction(deprecatedBy) without @Deprecated in " + procName);
    List<FieldSetter> setters = allFieldInjections.setters(procDefinition);
    if (!config.fullAccessFor(procName.toString())) {
        try {
            setters = safeFieldInjections.setters(procDefinition);
        } catch (ComponentInjectionException e) {
            description = describeAndLogLoadFailure(procName);
            UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, typeChecker.type(), deprecated, config.rolesFor(procName.toString()), description, null, false);
            return new FailedLoadFunction(signature);
        }
    }
    UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, typeChecker.type(), deprecated, config.rolesFor(procName.toString()), description, null, false);
    return ProcedureCompilation.compileFunction(signature, setters, method);
}
Also used : FieldSignature(org.neo4j.internal.kernel.api.procs.FieldSignature) UserFunctionSignature(org.neo4j.internal.kernel.api.procs.UserFunctionSignature) FailedLoadFunction(org.neo4j.kernel.api.procedure.FailedLoadFunction) CallableUserFunction(org.neo4j.kernel.api.procedure.CallableUserFunction) UserFunction(org.neo4j.procedure.UserFunction) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException)

Example 5 with UserFunction

use of org.neo4j.procedure.UserFunction in project neo4j by neo4j.

the class ReflectiveProcedureCompiler method compileFunction.

private CallableUserFunction compileFunction(Class<?> procDefinition, MethodHandle constructor, Method method, QualifiedName procName) throws ProcedureException, IllegalAccessException {
    if (procName.namespace() == null || procName.namespace().length == 0) {
        throw new ProcedureException(Status.Procedure.ProcedureRegistrationFailed, "It is not allowed to define functions in the root namespace please use a namespace, e.g. `@UserFunction(\"org.example.com.%s\")", procName.name());
    }
    List<FieldSignature> inputSignature = inputSignatureDeterminer.signatureFor(method);
    Class<?> returnType = method.getReturnType();
    TypeMappers.NeoValueConverter valueConverter = typeMappers.converterFor(returnType);
    MethodHandle procedureMethod = lookup.unreflect(method);
    Optional<String> description = description(method);
    UserFunction function = method.getAnnotation(UserFunction.class);
    Optional<String> deprecated = deprecated(method, function::deprecatedBy, "Use of @UserFunction(deprecatedBy) without @Deprecated in " + procName);
    List<FieldInjections.FieldSetter> setters = allFieldInjections.setters(procDefinition);
    if (!config.fullAccessFor(procName.toString())) {
        try {
            setters = safeFieldInjections.setters(procDefinition);
        } catch (ComponentInjectionException e) {
            description = Optional.of(procName.toString() + " is not available due to having restricted access rights, check configuration.");
            log.warn(description.get());
            UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(procName.toString()), description);
            return new FailedLoadFunction(signature);
        }
    }
    UserFunctionSignature signature = new UserFunctionSignature(procName, inputSignature, valueConverter.type(), deprecated, config.rolesFor(procName.toString()), description);
    return new ReflectiveUserFunction(signature, constructor, procedureMethod, valueConverter, setters);
}
Also used : FieldSignature(org.neo4j.kernel.api.proc.FieldSignature) UserFunctionSignature(org.neo4j.kernel.api.proc.UserFunctionSignature) FailedLoadFunction(org.neo4j.kernel.api.proc.FailedLoadFunction) CallableUserFunction(org.neo4j.kernel.api.proc.CallableUserFunction) UserFunction(org.neo4j.procedure.UserFunction) ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) ComponentInjectionException(org.neo4j.kernel.api.exceptions.ComponentInjectionException) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

UserFunction (org.neo4j.procedure.UserFunction)10 Description (org.neo4j.procedure.Description)7 Arrays.asList (java.util.Arrays.asList)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Node (org.neo4j.graphdb.Node)2 Relationship (org.neo4j.graphdb.Relationship)2 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)2 StringResult (apoc.result.StringResult)1 Util (apoc.util.Util)1 Util.quote (apoc.util.Util.quote)1 AnnotatedText (com.graphaware.nlp.domain.AnnotatedText)1 PipelineSpecification (com.graphaware.nlp.dsl.request.PipelineSpecification)1 TextProcessor (com.graphaware.nlp.processor.TextProcessor)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Math.toIntExact (java.lang.Math.toIntExact)1 MethodHandle (java.lang.invoke.MethodHandle)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1