Auto shut down for KDB instance - kdb

I am trying to set an auto shutdown on a KDB instance 24 hours after the launch.
I have been playing around with the .z.exit and \t but could not figure it out.

You need to set the timer function .z.ts (https://code.kx.com/q/ref/dotz/#zts-timer) which is triggered at each interval set by \t to terminate the q process using exit (https://code.kx.com/q/ref/exit/), e.g.
q) set timer to 24 hours (milliseconds)
q)\t 24*60*60*1000
q).z.ts:{exit 0}

On startup you could define your start time, then set a timer to check if the current time is 24 hours later and then exit.
startTime:.z.P;
.z.exit:{"Whatever you want to happen on exit"};
.z.ts:{if[1D<.z.P-startTime;exit 0]};
system"t 60000"; /set timer to whatever frequency

Related

Change machine delay time for rejected source in AnyLogic

Agent moving from machine block (machine block delay is 9 minutes) to milling block. At the milling block, 7 percent gets rejected and goes back to the machine block.
Now rejected agents move from machine to milling block, but this time the machine block delay time is 12 minutes. The question is, how can I achieve this 12 minute delay time now?
*** The logic image is given below:***
In the agent type of of your agents going through the process, create a variable of type boolean and call it rejected with initial value false. On Exit (false) of the your selectOutput1 write:
agent.rejected = true
The delay of the machine block should be
agent.rejected ? 12 : 9

How to call suspend function in anylogic?

I create an integer to count the number of assemblies (e.g. countAssembler)
On exit from FA1 (countAssembler++;)
Then I have an event triggered by a condition such that when the count of assemblies reaches 10 ((countAssembler==10)), it suspends the FA1 for two hours using suspend function.
But how do I implement the suspend function? Do you have any ideas?
note that the suspend function doesn't suspend the assembler, but an item that is currently being processed in the assembler...
have a variable countAssembler count the number of items produced... then
on exit you write countAssembler++;
on enter delay you write the following:
if(countAssembler==10){
self.suspend(agent);
create_MyDynamicEvent(2, HOUR,agent);
}
on the dynamic event you write:
assembler.resume(agent);
Don't forget to add the parameter needed in the dynamic event:
Note that the suspend will start when part 11 starts being assembled, which means that the machine will be suspended for 2 hours + the time between the 10th assembly end and then 11th assembly start... you can fix that easily but im not including it in my answer

How to detect (say) 2 seconds of inactivity in swift?

This question could be rephrased as: How to invoke a function if 2 seconds pass without an event (re)occurring?
I'm playing with SFSpeechRecogniser. While the user is speaking it sends continuous updates (maybe 2-3 per second). I'm trying to detect when the user stops speaking. If I don't receive any updates for (say) 2 seconds, I can assume that the user has paused speaking.
How to implement this in Swift?
I am aware that I could do:
var timer : Timer?
func f() {
on_event = { result, error in
print( "Got event, restarting timer" )
self.timer?.invalidate()
self.timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { _ in
print( "2s inactivity detected" )
self.timer?.invalidate()
NotificationCenter.default.post( name: inactivity_notification, object: nil )
}
}
}
But is it possible to do it without repeatedly creating and destroying the Timer instance (and thus creating a boatload of temporary Timer instances that never get used)?
One way to do it is to:
Record the current time when an event occurs
Set up a recurring timer with a granularity you are comfortable with (for example 0.25 seconds).
When the timer pops, check difference between current time and last event time. If that is greater than 2 seconds, fire your notification.
This is what I'd do if I had to recognize that a person had stopped typing for 2 seconds. Invalidating and creating timers at typing speed would be a lot of churn. You can tune this to your requirements depending on how close to exactly 2 seconds you need to be.
You could also do this by just having a timeSinceLastEvent variable, and set it to 0 when an event occurs. The recurring timer would increment this by the granularity, and check if it has reached 2 seconds and fire the notification if it had. This is cruder than doing the time math since the timer interval isn't guaranteed, but simpler.
Timer's .fireDate property is writable.
So every time a speech event occurs just do timer.fireDate = Date(timeIntervalSinceNow: 2)

How to count the number of people when time equal to 120 seconds during evacuation using the function in anylogic?

I am working on fire evacuation of a building floor and would like to count the number of people remaining inside the building after 120 seconds? The timer should start once the evacuation process begins, which is by alarm that goes off after a certain amount of time using an event feature.
I know how to count the total number of people inside the building using the function component getPeopleInsideCount and a text with getPeopleInsideCount(). But I don't know what code to use for my problem.
Below is the code:
return pedOffice.countPeds() + pedStudents.countPeds() - pedSink.sink.count();
With this, it will count the people in the building floor and it will stop counting after 120 seconds...
Step 1:
create an event with trigger type timeout, and mode: user control, and timeout=120 seconds.
Step 2:
create a variable called stopCounting as a boolean with initial value false
create a variable called peopleRemaining as an int
Step 3:
when the evacuation begins run the code:
event.restart();
Step 4:
in your event use the following code:
stopCounting=true;
peopleRemaining=getPeopleInsideCount();
Step 5
In your text use the following code instead of getPeopleInsideCount()
stopCounting ? peopleRemaining : getPeopleInsideCount()
Add a dynamic event that returns the count you need.
Once your alarm goes off, you can call that dynamic event 120 seconds later using create_MyDynamicEvent(120, SECOND);
That will execute the event code 120 seconds later.
cheers

Alternative for Timers

I am having a simple game with a player, a moving background and moving walls, kinda like flappy bird. My player is able to collect a powerUp. It is transforming after and put into a special level.
I want this level to run for like 10 seconds and then call a backtransformation.
Currently I am using a timer which just calls the backtransformation after 10 seconds. I am having trouble now when the player pauses the game, the timer is still running.
--> what I found on stackoverflow is that you can't resume a timer, you can just invalidate and restart it. This would miss my target, otherwise the player pauses the game every 9 seconds and can stay in the super Level forever.
Do you guys have any idea how I can solve my problem or like an alternative to use Timers in my code?
I appreciate any help
Edit: Here is how I simply used the timer
// transform and go into super level added here
self.transform()
timer = Timer.scheduledTimer(withTimeInterval:10, repeats: false) {
timer in
self.backtransform()
}
When you start the timer record the current time as a Double using
let start = Date().timeIntervalSinceReferenceDate
var remaining = 10.0
When the user pauses the timer, calculate the amount of time that has passed with:
let elapsed = Date().timeIntervalSinceReferenceDate - start
And the amount of remaining time for your timer:
remaining -= elapsed
If the user resumes the timer, set it to remaining, not 10 seconds.