Skip to content

Commit

Permalink
Remove % operations. Add in-between check for until_less.
Browse files Browse the repository at this point in the history
  • Loading branch information
anmaped committed Apr 19, 2024
1 parent f6b8503 commit 7ac7964
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 16 deletions.
36 changes: 35 additions & 1 deletion src/rmtld3/formulas.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "rmtld3.h"
#include "terms.h"

typedef bool with_in_between;

/**
* Proposition
*/
Expand All @@ -54,7 +56,7 @@ three_valued_type prop(T &trace, const proposition &p, timespan &t) {
/**
* Until (<)
*/
template <typename T, typename E, timespan b>
template <typename T, typename E, timespan b, typename S = void>
three_valued_type until_less(T &trace, timespan &t) {
auto eval_fold = [](T &trace,
timespan &t) -> std::pair<four_valued_type, timespan> {
Expand Down Expand Up @@ -115,6 +117,38 @@ three_valued_type until_less(T &trace, timespan &t) {

trace.debug();

// check in-between; enable it when needed
constexpr const int v = std::is_void<S>::value;
if (v == false) {
size_t store_cursor = trace.get_cursor();
timespan t1, t2, t3 = c_time;
typename T::buffer_t::event_t e1, e2;
while (true) {
trace.increment_cursor();
if (trace.length() <= 1 || trace.read(e1) != trace.AVAILABLE ||
trace.read_next(e2) != trace.AVAILABLE) {
break;
}

t1 = e1.getTime();
t2 = e2.getTime();
t3 += (t2 - t1);

if (t3 >= t1)
break;

DEBUGV_RMTLD3("t1=%d t2=%d t3=%d\n", t1, t2, t3);

size_t store_cursor2 = trace.get_cursor();
trace.set(t3);
trace.decrement_cursor();
symbol = eval_b(trace, t3, symbol);
trace.set_cursor(store_cursor2);
};
trace.set_cursor(store_cursor);
// end check in-between
}

// check if symbol converged and stop!
if (symbol != FV_SYMBOL)
break;
Expand Down
39 changes: 24 additions & 15 deletions src/rmtld3/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ template <typename R, typename P> class RMTLD3_reader : public R {
*/
size_t &get_cursor();

/**
* Increment current cursor
*/
typename R::error_t increment_cursor();

/**
* Decrement current cursor
*/
typename R::error_t decrement_cursor();

/**
Expand Down Expand Up @@ -123,8 +130,7 @@ typename R::error_t RMTLD3_reader<R, P>::set(timespan &t) {
if (t > e.getTime())
return R::AVAILABLE;

// set cursor back
cursor = (size_t)(cursor - 1) % (R::buffer.size + 0);
decrement_cursor();
DEBUGV_RMTLD3("backward cursor=%d\n", cursor);
}

Expand All @@ -133,8 +139,7 @@ typename R::error_t RMTLD3_reader<R, P>::set(timespan &t) {
if (t < e.getTime())
return R::AVAILABLE;

// set cursor back
cursor = (size_t)(cursor + 1) % (R::buffer.size + 0);
increment_cursor();
DEBUGV_RMTLD3("forward cursor=%d\n", cursor);
}

Expand Down Expand Up @@ -163,23 +168,27 @@ template <typename R, typename P> size_t &RMTLD3_reader<R, P>::get_cursor() {
template <typename R, typename P>
typename R::error_t RMTLD3_reader<R, P>::increment_cursor() {

// check if cursor is bounded between bot and top
// check if current cursor is bounded between bot and top
if (R::top == cursor)
return R::UNAVAILABLE;

cursor = (size_t)(cursor + 1) % R::buffer.size;
// cursor = (size_t)(cursor + 1) % R::buffer.size;
if (++cursor >= R::buffer.size)
cursor = 0;

return R::AVAILABLE;
}

template <typename R, typename P>
typename R::error_t RMTLD3_reader<R, P>::decrement_cursor() {

// check if cursor is bounded between _bot and _top
// check if current cursor is bounded between bot and top
if (R::bottom == cursor)
return R::UNAVAILABLE;

cursor = (size_t)(cursor - 1) % R::buffer.size;
// cursor = (size_t)(cursor - 1) % R::buffer.size;
if (--cursor < 0)
cursor = R::buffer.size - 1;

return R::AVAILABLE;
}
Expand All @@ -195,8 +204,7 @@ RMTLD3_reader<R, P>::pull(typename R::buffer_t::event_t &e) {
if (R::buffer.read(e, cursor) != R::buffer.OK)
return R::BUFFER_READ;

// update local cursor
cursor = (size_t)(cursor + 1) % (R::buffer.size + 0);
increment_cursor();
}

DEBUGV_RMTLD3("pull-> %d (%d,%d)\n", length(), cursor, R::top);
Expand All @@ -217,8 +225,9 @@ typename R::error_t
RMTLD3_reader<R, P>::read_next(typename R::buffer_t::event_t &e) {

return ((length() > 1 &&
!(cursor == R::top) && // [TODO: check this: !(cursor == R::top)]
(R::buffer.read(e, (size_t)(cursor + 1) % (R::buffer.size + 0))) ==
cursor != R::top && // [TODO: check this: !(cursor == R::top)]
(R::buffer.read(e, ((cursor + 1) >= R::buffer.size) ? 0
: cursor + 1)) ==
R::buffer.OK))
? R::AVAILABLE
: R::UNAVAILABLE;
Expand All @@ -231,9 +240,9 @@ RMTLD3_reader<R, P>::read_previous(typename R::buffer_t::event_t &e) {
DEBUGV_RMTLD3("consumed=%d available=%d total=%d\n", consumed(), length(),
R::length());
return ((consumed() > 0 &&
!(cursor ==
R::bottom) && // [TODO: check this: !(cursor == R::bottom)]
(R::buffer.read(e, (size_t)(cursor - 1) % (R::buffer.size + 0))) ==
cursor != R::bottom && // [TODO: check this: !(cursor == R::bottom)]
(R::buffer.read(e, ((cursor - 1) < 0) ? R::buffer.size - 1
: cursor - 1)) ==
R::buffer.OK))
? R::AVAILABLE
: R::UNAVAILABLE;
Expand Down
131 changes: 131 additions & 0 deletions tests/rtmlib_rmtld3_always_less_implies_eventually.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* rtmlib is a Real-Time Monitoring Library.
*
* Copyright (C) 2018-2023 André Pedro
*
* This file is part of rtmlib.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* rmtlib_rmtld3 with always
*/

#include <cstring>

#include <circularbuffer.h>
#include <reader.h>
#include <rmtld3/formulas.h>
#include <rmtld3/reader.h>

enum prop {
PROP_a = 2,
PROP_b = 1,
};
template <typename T> class Eval_always_a_1 {
public:
static three_valued_type eval_phi1(T &trace, timespan &t) {
auto sf = T_TRUE;
return sf;
};
static three_valued_type eval_phi2(T &trace, timespan &t) {
return T_UNKNOWN;
};
};
template <typename T> class Eval_always_b_1 {
public:
static three_valued_type eval_phi1(T &trace, timespan &t) {
auto sf = prop<T>(trace, PROP_b, t);
return sf;
};
static three_valued_type eval_phi2(T &trace, timespan &t) {
return T_UNKNOWN;
};
};

template <typename T> class Eval_until_less_2 {
public:
static three_valued_type eval_phi1(T &trace, timespan &t) {
auto sf = T_TRUE;
return sf;
};
static three_valued_type eval_phi2(T &trace, timespan &t) {
auto sf = [](T &trace, timespan &t) {
auto x = [](T &trace, timespan &t) {
auto x = [](T &trace, timespan &t) {
auto x = prop<T>(trace, PROP_a, t);
return b3_not(x);
}(trace, t);
auto y = [](T &trace, timespan &t) {
auto x = always_less<T, Eval_always_a_1<T>, 3>(trace, t);
auto y = always_equal<T, Eval_always_b_1<T>, 3>(trace, t);
return b3_and(x, y);
}(trace, t);
return b3_or(x, y);
}(trace, t);
return b3_not(x);
}(trace, t);
return sf;
};
};

extern "C" int rtmlib_rmtld3_always_less_implies_eventually();

int rtmlib_rmtld3_always_less_implies_eventually() {

typedef Event<int> event_t;
typedef RTML_buffer<event_t, 100> buffer_t;
typedef RMTLD3_reader<RTML_reader<buffer_t>, int> trace_t;

buffer_t buf;
unsigned long int index = 0;

event_t e[6] = {
event_t(PROP_a, 2), event_t(PROP_b, 5), event_t(3, 6),
event_t(PROP_b, 7), event_t(3, 8), event_t(4, 20),
};

for (int i = 0; i < 6; i++)
buf.push(e[i]);

// check first element of the trace
{
event_t event;
buf.read(event, index);
assert(event.getTime() == 2 && event.getData() == PROP_a);
};

int tzero = 0.;
trace_t trace = trace_t(buf, tzero);

trace.synchronize();

auto _mon0_compute = [](trace_t &trace, timespan &t) -> three_valued_type {
return [](trace_t &trace, timespan &t) {
auto x = until_less<trace_t, Eval_until_less_2<trace_t>, 10, with_in_between>(trace, t);
return b3_not(x);
}(trace, t);
};

timespan t = 2;
auto _out = _mon0_compute(trace, t);

if (strcmp(out_p(_out), "false") == 0)
printf("%s \033[0;32msuccess.\e[0m\n", __FILE__);
else
printf("%s \033[0;31mFail.\e[0m\n", __FILE__);

return 0;
}

0 comments on commit 7ac7964

Please sign in to comment.