Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jason.asSyntax.directives;
  
 
 import java.util.Map;
This class maintains the set of arithmetic functions available for the AS parser.

Author(s):
Jomi
 
 public class FunctionRegister implements Directive {
     static Logger logger = Logger.getLogger(FunctionRegister.class.getName());
 
     private static Map<String,ArithFunctionfunctions = new HashMap<String,ArithFunction>();
 
     // add known global functions (can be computed without an agent reference)
     static {
         addJasonFunction(Abs.class);
         addJasonFunction(Max.class);
         addJasonFunction(Min.class);
         addJasonFunction(Sum.class);
         addJasonFunction(StdDev.class);
         addJasonFunction(Average.class);
         addJasonFunction(Length.class);
         addJasonFunction(Random.class);
         addJasonFunction(Round.class);
         addJasonFunction(Sqrt.class);
         addJasonFunction(pi.class);
         addJasonFunction(e.class);
         addJasonFunction(floor.class);
         addJasonFunction(ceil.class);
         addJasonFunction(log.class);
         addJasonFunction(time.class);
     }
     
     private static void addJasonFunction(Class<? extends ArithFunctionc) {
         try {
             ArithFunction af = c.newInstance();
             .put(af.getName(), af);
         } catch (Exception e) {
             .log(."Error registering function "+c.getName(),e);
         }
     }

    
add new global function (shared among all agents in the JVM)
 
     public static void addFunction(Class<? extends ArithFunctionc) {
         try {
             ArithFunction af = c.newInstance();
             String error = FunctionRegister.checkFunctionName(af.getName());
             if (error != null)
                 .warning(error);
             else
                 .put(af.getName(), af);
         } catch (Exception e) {
             .log(."Error registering function "+c.getName(),e);
         }
     }
 
     public static String checkFunctionName(String fName) {
         if (.get(fName) != null)
             return "Can not register the function "+fName+"  twice!";
         else if (fName.indexOf(".") < 0)
             return "The function "+fName+" was not registered! A function must have a '.' in its name.";
         else if (fName.startsWith(".")) 
             return "The function "+fName+" was not registered! An user function name can not start with '.'.";
         else 
             return null;
     }
     
     public static ArithFunction getFunction(String functionint arity) {
         ArithFunction af = .get(function);
         if (af != null && af.checkArity(arity))
             return af;
         else
             return null;
    }
    
    @SuppressWarnings("unchecked")
    public Agent process(Pred directiveAgent outerContentAgent innerContent) {
        if (outerContent == null)
            return null;
        try {
            String id = ((StringTerm)directive.getTerm(0)).getString();
            if (directive.getArity() == 1) {
                // it is implemented in java
                outerContent.addFunction((Class<ArithFunction>)Class.forName(id));
            } else if (directive.getArity() == 3) {
                // is is implemented in AS
                int arity = (int)((NumberTerm)directive.getTerm(1)).solve();
                String predicate = ((StringTerm)directive.getTerm(2)).getString();
                outerContent.addFunction(idaritypredicate);
            } else {
                // error
                .log(."Wrong number of arguments for register_function "+directive);
            }
        } catch (Exception e) {
            .log(."Error processing directive register_function.",e);
        }
        return null;
    }
New to GrepCode? Check out our FAQ X