<commit_before>package com.incquerylabs.iot.droolsbundle;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.kie.api.time.SessionPseudoClock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HomeioSessionClock {
    static Logger logger = LoggerFactory.getLogger(HomeioSessionClock.class);

    private float timeMultiplier = 1;
    private Date homeioTime = null;
    private Date homeioTimeAddedTime = null;
    private Date realTimeAtLastIncrement = null;
    private long incrementedTimeSinceHomeioTimeAdded = 0;
    private SessionPseudoClock clock = null;

    public HomeioSessionClock(SessionPseudoClock clock) {
        this.clock = clock;
    }

    public void newHomeioTime(Date newHomeioTime) {
        Date realTime = new Date();
        if (homeioTime != null) {
            timeMultiplier = Math.round(
                    (float) elapsedTimeInHomeio(newHomeioTime) / (float) elapsedRealTimeSinceHomeioTime(realTime));

            logger.error("IncQuery droolsbundle: timeMultiplier: " + timeMultiplier);
            clock.advanceTime(elapsedTimeInHomeio(newHomeioTime) - incrementedTimeSinceHomeioTimeAdded,
                    TimeUnit.MILLISECONDS);
        }

        homeioTime = newHomeioTime;
        homeioTimeAddedTime = realTime;
        incrementedTimeSinceHomeioTimeAdded = 0;
    }

    public void advanceClock() {
        clock.advanceTime(getElapsedTime(), TimeUnit.MILLISECONDS);
    }

    public long getHomeioTime() {
        if (homeioTime != null) {
            Date realTime = new Date();
            return homeioTime.getTime() + (long) (elapsedRealTimeSinceHomeioTime(realTime) * timeMultiplier * 0.9);
        }
        return 0;
    }

    private long getElapsedTime() {
        long elapsedTime = 0;
        if (homeioTime != null) {
            Date realTime = new Date();

            if (realTimeAtLastIncrement != null) {
                elapsedTime = (long) (elapsedRealTimeSinceLastIncrement(realTime) * timeMultiplier * 0.9);<commit_msg>HomeIO session clock improved synchronization<commit_after>package com.incquerylabs.iot.droolsbundle;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.kie.api.time.SessionPseudoClock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HomeioSessionClock {
    static Logger logger = LoggerFactory.getLogger(HomeioSessionClock.class);

    private float timeMultiplier = 1;
    private Date homeioTime = null;
    private Date homeioTimeAddedTime = null;
    private Date realTimeAtLastIncrement = null;
    private long incrementedTimeSinceHomeioTimeAdded = 0;
    private SessionPseudoClock clock = null;

    public HomeioSessionClock(SessionPseudoClock clock) {
        this.clock = clock;
    }

    public void newHomeioTime(Date newHomeioTime) {
        Date realTime = new Date();
        if (homeioTime != null) {
            timeMultiplier = Math.round(
                    (float) elapsedTimeInHomeio(newHomeioTime) / (float) elapsedRealTimeSinceHomeioTime(realTime));

            logger.error("IncQuery droolsbundle: timeMultiplier: " + timeMultiplier);
            clock.advanceTime(elapsedTimeInHomeio(newHomeioTime) - incrementedTimeSinceHomeioTimeAdded,
                    TimeUnit.MILLISECONDS);
        }

        homeioTime = newHomeioTime;
        homeioTimeAddedTime = realTime;
        incrementedTimeSinceHomeioTimeAdded = 0;
        realTimeAtLastIncrement = null;
    }

    public void advanceClock() {
        clock.advanceTime(getElapsedTime(), TimeUnit.MILLISECONDS);
    }

    public long getHomeioTime() {
        if (homeioTime != null) {
            return homeioTime.getTime() + incrementedTimeSinceHomeioTimeAdded;
        }
        return 0;
    }

    private long getElapsedTime() {
        long elapsedTime = 0;
        if (homeioTime != null) {
            Date realTime = new Date();

            if (realTimeAtLastIncrement != null) {
                elapsedTime = (long) (elapsedRealTimeSinceLastIncrement(realTime) * timeMultiplier * 0.9);
{'fraction_non_alphanumeric': 0.08290282902829028, 'fraction_numerical': 0.004920049200492005, 'mean_word_length': 2.9591041869522883, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1380087', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1166, 'n_words': 242}