Guest User

TaskSimulation.qml

a guest
Sep 27th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 2.48 KB | None | 0 0
  1. import QtQuick 2.15
  2.  
  3. // Simulates fix amount of Tasks and their activity (random sleep and work)
  4.  
  5. Item
  6. {
  7.     id:taskSimulation
  8.  
  9.     //public properties:
  10.  
  11.     property ListModel taskModel: taskModel
  12.     property int ticks: 0
  13.     property int taskCount: 0
  14.     property int intervalInMsec: 100
  15.  
  16.     //private stuff:
  17.  
  18.     ListModel {
  19.         id: taskModel
  20.         //[taskName, ranges{[{start, duration},...]},...]
  21.     }
  22.  
  23.  
  24.     ListModel {
  25.         id: simModel
  26.     }
  27.  
  28.     function getRandomInt(min, max) {
  29.         min = Math.ceil(min);
  30.         max = Math.floor(max);
  31.         return Math.floor(Math.random() * (max - min + 1)) + min;
  32.     }
  33.  
  34.     readonly property int openState: 0
  35.     readonly property int closeState: 1
  36.  
  37.     function start()
  38.     {
  39.         for (var i = 0; i < taskCount; i++)
  40.         {
  41.             var ranges = []
  42.             taskModel.append({"taskName": "Task"+i.toString(), "ranges":ranges});
  43.             simModel.append({"waitFor": openState, "wait":getRandomInt(3,5)});
  44.         }
  45.         simTimer.running = true
  46.     }
  47.  
  48.     function proceed()
  49.     {
  50.         for (var i = 0; i < taskModel.count; i++)
  51.         {
  52.             var sim = simModel.get(i);
  53.  
  54.             var task = taskModel.get(i)
  55.             var task_ranges = task.ranges
  56.  
  57.             sim.wait = sim.wait - 1;
  58.             if(sim.wait === 0)
  59.             {
  60.                 if( sim.waitFor === closeState)
  61.                 {
  62.                     sim.waitFor = openState;
  63.                 }
  64.                 else if( sim.waitFor === openState)
  65.                 {
  66.                     task_ranges.append({"start":ticks, "duration":0})
  67.                     sim.waitFor = closeState;
  68.                 }
  69.                 sim.wait = getRandomInt(10,25);
  70.             }
  71.             else
  72.             {
  73.                 if( sim.waitFor === closeState)
  74.                 {
  75.                     var rangeCount = task_ranges.count
  76.                     if(rangeCount > 0)
  77.                     {
  78.                         var range = task_ranges.get(rangeCount-1)
  79.                         var duration = range.duration
  80.                         var new_duration = duration+1
  81.                         range.duration = new_duration
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     Timer {
  89.         id: simTimer
  90.         interval: intervalInMsec
  91.         running: false
  92.         repeat: true
  93.         onTriggered: {
  94.             ticks = ticks + 1
  95.             proceed()
  96.         }
  97.     }
  98. }
  99.  
Add Comment
Please, Sign In to add comment