Search in sources :

Example 1 with ABSDynamicObject

use of abs.backend.java.lib.runtime.ABSDynamicObject in project abstools by abstools.

the class Clazz method setupMetaAPI.

/*
     * Define the methods of this class
     */
public static void setupMetaAPI() {
    thisClass.setName("Class");
    thisClass.addMethod(/*ABSString*/
    "getName", new ABSClosure() {

        @Override
        public ABSString exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicClass cls = (ABSDynamicClass) t.getFieldValue_Internal("class");
            return ABSString.fromString(cls.getName());
        }
    });
    thisClass.addMethod(/*ABSClosure*/
    "getMethod", new ABSClosure() {

        @Override
        public ABSDynamicObject exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicClass cls = (ABSDynamicClass) t.getFieldValue_Internal("class");
            ABSString name = (ABSString) params[0];
            ABSClosure method = cls.getMethod(name.getString());
            return method;
        }
    });
    thisClass.addMethod(/*ABSUnit*/
    "addMethod", new ABSClosure() {

        @Override
        public ABSUnit exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicClass cls = (ABSDynamicClass) t.getFieldValue_Internal("class");
            ABSString name = (ABSString) params[0];
            ABSClosure method = (ABSClosure) ((ABSDynamicObject) params[1]).getFieldValue_Internal("method");
            cls.addMethod(name.getString(), method);
            return ABSUnit.UNIT;
        }
    });
    thisClass.addMethod(/*ABSUnit*/
    "removeMethod", new ABSClosure() {

        @Override
        public ABSUnit exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicClass cls = (ABSDynamicClass) t.getFieldValue_Internal("class");
            ABSString name = (ABSString) params[0];
            cls.removeMethod(name.getString());
            return ABSUnit.UNIT;
        }
    });
}
Also used : ABSDynamicObject(abs.backend.java.lib.runtime.ABSDynamicObject) ABSClosure(abs.backend.java.lib.runtime.ABSClosure) ABSUnit(abs.backend.java.lib.types.ABSUnit) ABSValue(abs.backend.java.lib.types.ABSValue) ABSString(abs.backend.java.lib.types.ABSString) ABSDynamicClass(abs.backend.java.lib.runtime.ABSDynamicClass)

Example 2 with ABSDynamicObject

use of abs.backend.java.lib.runtime.ABSDynamicObject in project abstools by abstools.

the class Cog method setupAPI.

private static void setupAPI() {
    thisClass.setName("Cog");
    thisClass.addMethod(/*List<Process>*/
    "getQueue", new ABSClosure() {

        @Override
        public ABSValue exec(ABSDynamicObject t, ABSValue... params) {
            // TODO
            return ABSUnit.UNIT;
        }
    });
    thisClass.addMethod(/*ABSUnit*/
    "info", new ABSClosure() {

        @Override
        public ABSValue exec(ABSDynamicObject t, ABSValue... params) {
            COG cog = (COG) t.getFieldValue_Internal("cog");
            System.out.println("Cog scheduler " + cog.getScheduler().toString());
            if (cog.getScheduler() instanceof SimpleTaskScheduler) {
                SimpleTaskScheduler sch = (SimpleTaskScheduler) cog.getScheduler();
                TaskSchedulingStrategy strat = sch.getSchedulingStrategy();
                System.out.println("Strategy " + strat);
            }
            return ABSUnit.UNIT;
        }
    });
    thisClass.addMethod(/*ABSUnit*/
    "setScheduler", new ABSClosure() {

        @Override
        public ABSValue exec(ABSDynamicObject t, ABSValue... params) {
            COG cog = (COG) t.getFieldValue_Internal("cog");
            if (!(cog.getScheduler() instanceof SimpleTaskScheduler)) {
                throw new DynamicException("For user-defined scheduling to work, the Scheduler must be an instance of " + SimpleTaskScheduler.class.getName() + " (use \"-taskscheduler=simple\")");
            }
            // The user-defined scheduler
            final ABSDynamicObject userScheduler = (ABSDynamicObject) params[0];
            // Create a new scheduling strategy that invokes the user-defined scheduler's "schedule" method
            // - convert TaskSchedulingStrategy's List<TaskInfo> to ProcessScheduler's List<Process>
            // - convert returned Process to TaskInfo.
            // custom TaskSchedulingStrategy
            TaskSchedulingStrategy strategy = new TaskSchedulingStrategy() {

                @Override
                public synchronized TaskInfo schedule(final TaskScheduler scheduler, final List<TaskInfo> schedulableTasks) {
                    System.out.println("Scheduling (" + schedulableTasks.size() + " processes in queue)...");
                    // Remember TaskInfos based on their Pids to speed things up a little
                    HashMap<Long, TaskInfo> taskMap = new HashMap<>();
                    // Convert List<TaskInfo> to ArrayList<ABSProcess>
                    ArrayList<ABSProcess> processes = new ArrayList<>();
                    for (TaskInfo task : schedulableTasks) {
                        taskMap.put(task.id, task);
                        // TODO set: pid, method, arrival, cost, deadline, start, finish, critical, value
                        ABSProcess proc = new ABSProcess(task.task.getID(), task.task.getCall().methodName(), 0, 0, 0, 0, 0, false, 0);
                        processes.add(proc);
                        System.out.println("\t" + proc.toString());
                    }
                    // FIXME
                    return schedulableTasks.get(0);
                }
            };
            // Connect the strategy to the cog's scheduler
            System.out.println(cog.toString() + ": Setting TaskSchedulingStrategy to: " + userScheduler.getClassName());
            // move the userScheduler object to the Cog
            // this WILL cause problems if the user associates the scheduler with multiple cogs!!
            // userScheduler.setCOG(cog);
            SimpleTaskScheduler scheduler = (SimpleTaskScheduler) cog.getScheduler();
            scheduler.setSchedulingStrategy(strategy);
            return ABSUnit.UNIT;
        }
    });
}
Also used : ABSDynamicObject(abs.backend.java.lib.runtime.ABSDynamicObject) ABSClosure(abs.backend.java.lib.runtime.ABSClosure) HashMap(java.util.HashMap) SimpleTaskScheduler(abs.backend.java.scheduling.SimpleTaskScheduler) ArrayList(java.util.ArrayList) ABSValue(abs.backend.java.lib.types.ABSValue) SimpleTaskScheduler(abs.backend.java.scheduling.SimpleTaskScheduler) TaskScheduler(abs.backend.java.scheduling.TaskScheduler) ABSProcess(abs.backend.java.lib.types.ABSProcess) DynamicException(abs.backend.java.codegeneration.dynamic.DynamicException) TaskInfo(abs.backend.java.scheduling.SimpleTaskScheduler.TaskInfo) TaskSchedulingStrategy(abs.backend.java.scheduling.TaskSchedulingStrategy) COG(abs.backend.java.lib.runtime.COG)

Example 3 with ABSDynamicObject

use of abs.backend.java.lib.runtime.ABSDynamicObject in project abstools by abstools.

the class ObjectMirror method setupAPI.

public static void setupAPI() {
    thisClass.setName("ObjectMirror");
    /*
         * getClassName: get name of object's class
         */
    thisClass.addMethod(/*ABSString*/
    "getClassName", new ABSClosure() {

        @Override
        public ABSString exec(ABSDynamicObject t, ABSValue... params) {
            ABSString name;
            name = ABSString.fromString(((ABSDynamicObject) t.dispatch("getObject")).getClassName());
            return name;
        }
    });
    /*
         * getClass: get class of object
         */
    thisClass.addMethod(/*ABSDynamicObject<Clazz>*/
    "getClass", new ABSClosure() {

        @Override
        public ABSDynamicObject exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicClass cls = ((ABSDynamicObject) t.dispatch("getObject")).getClazz();
            ABSDynamicObject o = new ABSDynamicObject(Clazz.singleton());
            o.setFieldValue("class", cls);
            return o;
        }
    });
    /*
         * setClass: set new class for object
         */
    thisClass.addMethod(/*Unit*/
    "setClass", new ABSClosure() {

        @Override
        public ABSUnit exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicClass cls = (ABSDynamicClass) ((ABSDynamicObject) params[0]).getFieldValue_Internal("class");
            ((ABSDynamicObject) t.dispatch("getObject")).setClazz(cls);
            return ABSUnit.UNIT;
        }
    });
    /*
         * respondsTo: find out whether object responds to given method
         */
    thisClass.addMethod(/*ABSBool*/
    "respondsTo", new ABSClosure() {

        @Override
        public ABSBool exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicClass cls = (ABSDynamicClass) ((ABSDynamicObject) t.dispatch("getObject")).getClazz();
            return ABSBool.fromBoolean(cls.hasMethod(((ABSString) params[0]).getString()));
        }
    });
    // TODO: getFieldValue(), setFieldValue()
    thisClass.addMethod(/*ABSDynamicObject<COG>*/
    "getCog", new ABSClosure() {

        @Override
        public ABSDynamicObject exec(ABSDynamicObject t, ABSValue... params) {
            COG cog = ((ABSDynamicObject) t.dispatch("getObject")).getCOG();
            ABSDynamicObject o = new ABSDynamicObject(Cog.singleton());
            o.setFieldValue("cog", cog);
            return o;
        }
    });
    thisClass.addMethod(/*Unit*/
    "setCog", new ABSClosure() {

        @Override
        public ABSUnit exec(ABSDynamicObject t, ABSValue... params) {
            ((ABSDynamicObject) t.dispatch("getObject")).setCOG((COG) params[0]);
            return ABSUnit.UNIT;
        }
    });
    /*
         * getObject: obtain the mirrored object
         */
    thisClass.addMethod(/*ABSDynamicObject*/
    "getObject", new ABSClosure() {

        @Override
        public ABSDynamicObject exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicObject object;
            object = (ABSDynamicObject) t.getFieldValue_Internal("object");
            return object;
        }
    });
}
Also used : ABSDynamicObject(abs.backend.java.lib.runtime.ABSDynamicObject) ABSClosure(abs.backend.java.lib.runtime.ABSClosure) COG(abs.backend.java.lib.runtime.COG) ABSDynamicClass(abs.backend.java.lib.runtime.ABSDynamicClass)

Example 4 with ABSDynamicObject

use of abs.backend.java.lib.runtime.ABSDynamicObject in project abstools by abstools.

the class Reconfiguration method setupMetaAPI.

/*
     * Define the methods of this class
     */
public static void setupMetaAPI() {
    thisClass.setName("Reconfiguration");
    /* 
         * MetaABS Reconfiguration API -- cf. abslang.abs module ABS.Meta 
         */
    thisClass.addMethod(/*ABSString*/
    "getName", new ABSClosure() {

        @Override
        public ABSString exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            return ABSString.fromString(thisR.getName());
        }
    });
    thisClass.addMethod(/*ABSDynamicProduct*/
    "getCurrentProduct", new ABSClosure() {

        @Override
        public ABSDynamicProduct exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            return thisR.getCurrentProduct();
        }
    });
    thisClass.addMethod(/*ABSUnit*/
    "setCurrentProduct", new ABSClosure() {

        @Override
        public ABSUnit exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            ABSDynamicProduct prod = (ABSDynamicProduct) params[0];
            thisR.setCurrentProduct(prod);
            return ABSUnit.UNIT;
        }
    });
    thisClass.addMethod(/*ABSDynamicProduct*/
    "getTargetProduct", new ABSClosure() {

        @Override
        public ABSDynamicProduct exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            return thisR.getTargetProduct();
        }
    });
    thisClass.addMethod(/*ABSUnit*/
    "setTargetProduct", new ABSClosure() {

        @Override
        public ABSUnit exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            ABSDynamicProduct prod = (ABSDynamicProduct) params[0];
            thisR.setTargetProduct(prod);
            return ABSUnit.UNIT;
        }
    });
    thisClass.addMethod(/*List<ABSDynamicDelta>*/
    "getDeltas", new ABSClosure() {

        @Override
        public ABSValue exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            List<ABSDynamicDelta> deltas = thisR.getDeltas();
            return ListUtils.toABSList(deltas);
        }
    });
    thisClass.addMethod(/*List<ABSDynamicDelta>*/
    "setDeltas", new ABSClosure() {

        @Override
        public ABSValue exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            // TODO
            return ABSUnit.UNIT;
        }
    });
    thisClass.addMethod(/*ABSDynamicUpdate*/
    "getStateUpdate", new ABSClosure() {

        @Override
        public ABSDynamicUpdate exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            return thisR.getUpdate();
        }
    });
    thisClass.addMethod(/*List<ABSDynamicDelta>*/
    "setStateUpdate", new ABSClosure() {

        @Override
        public ABSValue exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicReconfiguration thisR = (ABSDynamicReconfiguration) t;
            ABSDynamicUpdate upd = (ABSDynamicUpdate) params[0];
            thisR.setUpdate(upd);
            return ABSUnit.UNIT;
        }
    });
}
Also used : ABSDynamicObject(abs.backend.java.lib.runtime.ABSDynamicObject) ABSClosure(abs.backend.java.lib.runtime.ABSClosure) ABSDynamicProduct(abs.backend.java.lib.runtime.ABSDynamicProduct) ABSUnit(abs.backend.java.lib.types.ABSUnit) List(java.util.List) ABSDynamicUpdate(abs.backend.java.lib.runtime.ABSDynamicUpdate) ABSValue(abs.backend.java.lib.types.ABSValue) ABSString(abs.backend.java.lib.types.ABSString) ABSDynamicReconfiguration(abs.backend.java.lib.runtime.ABSDynamicReconfiguration)

Example 5 with ABSDynamicObject

use of abs.backend.java.lib.runtime.ABSDynamicObject in project abstools by abstools.

the class Update method setupMetaAPI.

/*
     * Define the methods of this class
     */
public static void setupMetaAPI() {
    thisClass.setName("Update");
    thisClass.addMethod(/*ABSString*/
    "getName", new ABSClosure() {

        @Override
        public ABSString exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicUpdate update = (ABSDynamicUpdate) t;
            return ABSString.fromString(update.getName());
        }
    });
    thisClass.addMethod(/*ABSUnit*/
    "apply", new ABSClosure() {

        @Override
        public ABSValue exec(ABSDynamicObject t, ABSValue... params) {
            ABSDynamicUpdate update = (ABSDynamicUpdate) t;
            update.apply();
            return ABSUnit.UNIT;
        }
    });
}
Also used : ABSDynamicObject(abs.backend.java.lib.runtime.ABSDynamicObject) ABSClosure(abs.backend.java.lib.runtime.ABSClosure) ABSDynamicUpdate(abs.backend.java.lib.runtime.ABSDynamicUpdate) ABSValue(abs.backend.java.lib.types.ABSValue) ABSString(abs.backend.java.lib.types.ABSString)

Aggregations

ABSClosure (abs.backend.java.lib.runtime.ABSClosure)10 ABSDynamicObject (abs.backend.java.lib.runtime.ABSDynamicObject)10 ABSValue (abs.backend.java.lib.types.ABSValue)8 ABSString (abs.backend.java.lib.types.ABSString)6 ABSDynamicProduct (abs.backend.java.lib.runtime.ABSDynamicProduct)3 ABSDynamicReconfiguration (abs.backend.java.lib.runtime.ABSDynamicReconfiguration)3 ABSDynamicClass (abs.backend.java.lib.runtime.ABSDynamicClass)2 ABSDynamicFeature (abs.backend.java.lib.runtime.ABSDynamicFeature)2 ABSDynamicUpdate (abs.backend.java.lib.runtime.ABSDynamicUpdate)2 COG (abs.backend.java.lib.runtime.COG)2 ABSUnit (abs.backend.java.lib.types.ABSUnit)2 ArrayList (java.util.ArrayList)2 DynamicException (abs.backend.java.codegeneration.dynamic.DynamicException)1 ABSDynamicDelta (abs.backend.java.lib.runtime.ABSDynamicDelta)1 ABSDynamicProductLine (abs.backend.java.lib.runtime.ABSDynamicProductLine)1 ABSProcess (abs.backend.java.lib.types.ABSProcess)1 SimpleTaskScheduler (abs.backend.java.scheduling.SimpleTaskScheduler)1 TaskInfo (abs.backend.java.scheduling.SimpleTaskScheduler.TaskInfo)1 TaskScheduler (abs.backend.java.scheduling.TaskScheduler)1 TaskSchedulingStrategy (abs.backend.java.scheduling.TaskSchedulingStrategy)1