Search in sources :

Example 11 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OSQLFunctionHeuristicPathFinderAbstract method getCustomHeuristicCost.

protected double getCustomHeuristicCost(final String functionName, final String[] vertextAxisNames, final OrientVertex start, final OrientVertex goal, final OrientVertex current, final OrientVertex parent, final long depth, double dFactor) {
    double heuristic = 0.0;
    OrientGraph ff;
    OFunction func = OrientGraph.getActiveGraph().getRawGraph().getMetadata().getFunctionLibrary().getFunction(functionName);
    Object fValue = func.executeInContext(context, vertextAxisNames, start, goal, current, parent, depth, dFactor);
    if (fValue != null && fValue instanceof Number) {
        heuristic = doubleOrDefault(fValue, heuristic);
    }
    return heuristic;
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Example 12 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OScriptManager method getLibrary.

/**
   * Formats the library of functions for a language.
   * 
   * @param db
   *          Current database instance
   * @param iLanguage
   *          Language as filter
   * @return String containing all the functions
   */
public String getLibrary(final ODatabase<?> db, final String iLanguage) {
    if (db == null)
        // NO DB = NO LIBRARY
        return null;
    final StringBuilder code = new StringBuilder();
    final Set<String> functions = db.getMetadata().getFunctionLibrary().getFunctionNames();
    for (String fName : functions) {
        final OFunction f = db.getMetadata().getFunctionLibrary().getFunction(fName);
        if (f.getLanguage() == null)
            throw new OConfigurationException("Database function '" + fName + "' has no language");
        if (f.getLanguage().equalsIgnoreCase(iLanguage)) {
            final String def = getFunctionDefinition(f);
            if (def != null) {
                code.append(def);
                code.append("\n");
            }
        }
    }
    return code.length() == 0 ? null : code.toString();
}
Also used : OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Example 13 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OClassTrigger method checkClzAttribute.

private Object checkClzAttribute(final ODocument iDocument, String attr) {
    final OImmutableClass clz = ODocumentInternal.getImmutableSchemaClass(iDocument);
    if (clz != null && clz.isTriggered()) {
        OFunction func = null;
        String fieldName = clz.getCustom(attr);
        OClass superClz = clz.getSuperClass();
        while (fieldName == null || fieldName.length() == 0) {
            if (superClz == null || superClz.getName().equals(CLASSNAME))
                break;
            fieldName = superClz.getCustom(attr);
            superClz = superClz.getSuperClass();
        }
        if (fieldName != null && fieldName.length() > 0) {
            // check if it is reflection or not
            final Object[] clzMethod = this.checkMethod(fieldName);
            if (clzMethod != null)
                return clzMethod;
            func = database.getMetadata().getFunctionLibrary().getFunction(fieldName);
            if (func == null) {
                // check if it is rid
                if (OStringSerializerHelper.contains(fieldName, ORID.SEPARATOR)) {
                    try {
                        ODocument funcDoc = database.load(new ORecordId(fieldName));
                        if (funcDoc != null) {
                            func = database.getMetadata().getFunctionLibrary().getFunction((String) funcDoc.field("name"));
                        }
                    } catch (Exception ex) {
                        OLogManager.instance().error(this, "illegal record id : ", ex.getMessage());
                    }
                }
            }
        } else {
            final Object funcProp = iDocument.field(attr);
            if (funcProp != null) {
                final String funcName = funcProp instanceof ODocument ? (String) ((ODocument) funcProp).field("name") : funcProp.toString();
                func = database.getMetadata().getFunctionLibrary().getFunction(funcName);
            }
        }
        return func;
    }
    return null;
}
Also used : OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OException(com.orientechnologies.common.exception.OException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OCommandScriptException(com.orientechnologies.orient.core.command.script.OCommandScriptException) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 14 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OScheduledEvent method getFunctionSafe.

private OFunction getFunctionSafe() {
    if (function == null) {
        final Object funcDoc = document.field(PROP_FUNC);
        if (funcDoc != null) {
            if (funcDoc instanceof OFunction) {
                function = (OFunction) funcDoc;
                // OVERWRITE FUNCTION ID
                document.field(PROP_FUNC, function.getId());
            } else if (funcDoc instanceof ODocument)
                function = new OFunction((ODocument) funcDoc);
            else if (funcDoc instanceof ORecordId)
                function = new OFunction((ORecordId) funcDoc);
        }
    }
    return function;
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 15 with OFunction

use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateFunction method execute.

/**
   * Execute the command and return the ODocument object created.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (name == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    if (name.isEmpty())
        throw new OCommandExecutionException("Syntax Error. You must specify a function name: " + getSyntax());
    if (code == null || code.isEmpty())
        throw new OCommandExecutionException("Syntax Error. You must specify the function code: " + getSyntax());
    ODatabaseDocument database = getDatabase();
    final OFunction f = database.getMetadata().getFunctionLibrary().createFunction(name);
    f.setCode(code);
    f.setIdempotent(idempotent);
    if (parameters != null)
        f.setParameters(parameters);
    if (language != null)
        f.setLanguage(language);
    f.save();
    return f.getId();
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Aggregations

OFunction (com.orientechnologies.orient.core.metadata.function.OFunction)18 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)4 OBasicCommandContext (com.orientechnologies.orient.core.command.OBasicCommandContext)3 Test (org.testng.annotations.Test)3 OException (com.orientechnologies.common.exception.OException)2 OCommandScriptException (com.orientechnologies.orient.core.command.script.OCommandScriptException)2 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)2 ArrayList (java.util.ArrayList)2 OPartitionedObjectPool (com.orientechnologies.common.concur.resource.OPartitionedObjectPool)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OFunctionLibrary (com.orientechnologies.orient.core.metadata.function.OFunctionLibrary)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)1