Search in sources :

Example 6 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class TestCompiledLongExpression method testSubtractWithLongs.

@Test
public void testSubtractWithLongs() {
    FunctionInterface helperFunction = new GiveLongTestFunction();
    FunctionDefinition fd = new FunctionDefinition(helperFunction.getClass().getName(), "description", "input", "result");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("ToLong", fd);
    Operand result;
    result = Expression.evaluate("ToLong(2) - ToLong(1)", null, null, null);
    Assert.assertEquals(1L, (long) result.value);
    result = Expression.evaluate("2 - ToLong(1)", null, null, null);
    Assert.assertEquals(1L, (long) result.value);
    result = Expression.evaluate("ToLong(2) - 1", null, null, null);
    Assert.assertEquals(1L, (long) result.value);
    result = Expression.evaluate("2.0 - ToLong(1)", null, null, null);
    Assert.assertEquals(1.0, (double) result.value, 0.000001);
    result = Expression.evaluate("ToLong(2) - 1.0", null, null, null);
    Assert.assertEquals(1.0, (double) result.value, 0.000001);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) Operand(com.dexels.navajo.document.Operand) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) GiveLongTestFunction(com.dexels.navajo.expression.compiled.GiveLongTestFunction) Test(org.junit.Test)

Example 7 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class TestCompiledPercentageExpression method setup.

@Before
public void setup() {
    FunctionInterface giveLongFunction = new GiveLongTestFunction();
    FunctionDefinition fd = new FunctionDefinition(giveLongFunction.getClass().getName(), "description", "input", "result");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("ToLong", fd);
    FunctionInterface givePercentageFunction = new GivePercentageTestFunction();
    fd = new FunctionDefinition(givePercentageFunction.getClass().getName(), "description", "input", "result");
    FunctionFactoryFactory.getInstance().addExplicitFunctionDefinition("ToPercentage", fd);
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) GiveLongTestFunction(com.dexels.navajo.expression.compiled.GiveLongTestFunction) GivePercentageTestFunction(com.dexels.navajo.expression.compiled.GivePercentageTestFunction) Before(org.junit.Before)

Example 8 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class FunctionListCommand method functions.

public void functions(CommandSession session, String filter) {
    String serviceFilter = null;
    if (filter != null) {
        serviceFilter = "(functionName=" + filter + ")";
    }
    session.getConsole().println("filter: " + filter);
    try {
        Collection<ServiceReference<FunctionInterface>> result = bundleContext.getServiceReferences(FunctionInterface.class, serviceFilter);
        for (ServiceReference<FunctionInterface> serviceReference : result) {
            String functionName = (String) serviceReference.getProperty("functionName");
            FunctionDefinition fd = (FunctionDefinition) serviceReference.getProperty("functionDefinition");
            session.getConsole().println("" + functionName);
            session.getConsole().println("\t" + fd.getDescription());
        // }
        }
    } catch (InvalidSyntaxException e) {
        e.printStackTrace(session.getConsole());
    }
}
Also used : FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 9 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class OsgiFunctionFactory method getAllFunctionElements.

@SuppressWarnings("rawtypes")
@Override
public List<XMLElement> getAllFunctionElements(String interfaceClass, String propertyKey) {
    List<XMLElement> result = new ArrayList<>();
    BundleContext context = Version.getDefaultBundleContext();
    try {
        ServiceReference[] refs = context.getServiceReferences(interfaceClass, null);
        if (refs == null) {
            logger.warn("Service enumeration failed class: {}", interfaceClass);
            return null;
        }
        for (ServiceReference serviceReference : refs) {
            Object o = serviceReference.getProperty(propertyKey);
            FunctionDefinition fd = (FunctionDefinition) o;
            XMLElement xe = fd.getXmlElement();
            if (xe != null) {
                result.add(xe);
            }
        }
    } catch (InvalidSyntaxException e) {
        logger.error("Error: ", e);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 10 with FunctionDefinition

use of com.dexels.navajo.expression.api.FunctionDefinition in project navajo by Dexels.

the class OsgiFunctionFactory method getAllAdapterElements.

@SuppressWarnings("rawtypes")
@Override
public // TODO not ok, seems to return function elements
List<XMLElement> getAllAdapterElements(String interfaceClass, String propertyKey) {
    List<XMLElement> result = new ArrayList<>();
    BundleContext context = Version.getDefaultBundleContext();
    try {
        ServiceReference[] refs = context.getServiceReferences(interfaceClass, null);
        if (refs == null) {
            logger.warn("Service enumeration failed class: {}", interfaceClass);
            return null;
        }
        for (ServiceReference serviceReference : refs) {
            Object o = serviceReference.getProperty(propertyKey);
            FunctionDefinition fd = (FunctionDefinition) o;
            XMLElement xe = fd.getXmlElement();
            if (xe != null) {
                result.add(xe);
            }
        }
    } catch (InvalidSyntaxException e) {
        logger.error("Error: ", e);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) FunctionDefinition(com.dexels.navajo.expression.api.FunctionDefinition) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) XMLElement(com.dexels.navajo.document.nanoimpl.XMLElement) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

FunctionDefinition (com.dexels.navajo.expression.api.FunctionDefinition)26 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)20 Test (org.junit.Test)13 GiveLongTestFunction (com.dexels.navajo.expression.compiled.GiveLongTestFunction)10 Operand (com.dexels.navajo.document.Operand)8 ArrayList (java.util.ArrayList)7 StringReader (java.io.StringReader)5 AddTestFunction (com.dexels.navajo.expression.compiled.AddTestFunction)4 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)4 ServiceReference (org.osgi.framework.ServiceReference)4 XMLElement (com.dexels.navajo.document.nanoimpl.XMLElement)3 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)3 Before (org.junit.Before)2 BundleContext (org.osgi.framework.BundleContext)2 CaseSensitiveXMLElement (com.dexels.navajo.document.nanoimpl.CaseSensitiveXMLElement)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 GiveMoneyTestFunction (com.dexels.navajo.expression.compiled.GiveMoneyTestFunction)1 GivePercentageTestFunction (com.dexels.navajo.expression.compiled.GivePercentageTestFunction)1 ParameterNamesFunction (com.dexels.navajo.expression.compiled.ParameterNamesFunction)1 FunctionFactoryInterface (com.dexels.navajo.functions.util.FunctionFactoryInterface)1