Skip to content

Commit

Permalink
Improved date/map support
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Feb 18, 2025
1 parent db57b4c commit 6d1c0fd
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 30 deletions.
45 changes: 45 additions & 0 deletions libraries/OCLC++11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,51 @@ class UmlRsdsLib {
return str.substr(i, j-i+1);
}

static bool includesKey(map<string, _T>* m, string k)
{
if (m->find(k) == m->end())
{
return false;
}
return true;
}

static bool excludesKey(map<string, _T>* m, string k)
{
if (m->find(k) == m->end())
{
return true;
}
return false;
}

static bool includesValue(map<string, _T>* s, _T val)
{
for (auto iter = s->begin(); iter != s->end(); ++iter)
{
_T value = iter->second;
if (val == value)
{
return true;
}
}

return false;
}

static bool excludesValue(map<string, _T>* s, _T val)
{
for (auto iter = s->begin(); iter != s->end(); ++iter)
{
_T value = iter->second;
if (val == value)
{
return false;
}
}

return true;
}

static bool includesAllMap(map<string,_T>* sup, map<string,_T>* sub)
{ map<string,_T>::iterator iter;
Expand Down
2 changes: 1 addition & 1 deletion libraries/Ocl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


/******************************
* Copyright (c) 2003--2024 Kevin Lano
* Copyright (c) 2003--2025 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
Expand Down
Binary file modified libraries/OclDate.class
Binary file not shown.
75 changes: 63 additions & 12 deletions libraries/OclDate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;


class OclDate implements Comparable {
Expand All @@ -10,6 +11,19 @@ class OclDate implements Comparable {
static final long HOURMS = 3600000L;
static final long MINUTEMS = 60000L;
static final long SECONDMS = 1000L;
static final HashMap<String,String> dayname;

static
{ dayname = new HashMap<String,String>();
dayname.put("0","Sunday");
dayname.put("1","Monday");
dayname.put("2","Tuesday");
dayname.put("3","Wednesday");
dayname.put("4","Thursday");
dayname.put("5","Friday");
dayname.put("6","Saturday");
dayname.put("7","Sunday");
}

static OclDate createOclDate()
{ OclDate result = new OclDate();
Expand All @@ -26,18 +40,23 @@ static OclDate createOclDate()
int hour = 0;
int minute = 0;
int second = 0;
int microsecond = 0;

public static OclDate newOclDate()
{ Date dd = new Date();
OclDate d = new OclDate();
d.time = dd.getTime();
d.year = dd.getYear();
d.month = dd.getMonth();
d.time = dd.getTime(); // milliseconds since 1st January 1970
d.year = dd.getYear() + 1900;
d.month = dd.getMonth() + 1;
d.day = dd.getDate(); // day of month
d.weekday = dd.getDay();
d.hour = dd.getHours();
d.minute = dd.getMinutes();
d.second = dd.getSeconds();
d.second = dd.getSeconds();

int milliseconds = (int) (d.time % 1000);
d.microsecond = 1000*milliseconds;

OclDate.systemTime = d.time;
return d;
}
Expand All @@ -48,10 +67,10 @@ public static OclDate newOclDate_Time(long t)

OclDate d = new OclDate();
d.time = t;
d.year = dd.getYear();
d.month = dd.getMonth();
d.year = dd.getYear() + 1900;
d.month = dd.getMonth() + 1;
d.day = dd.getDate(); // day of month
d.weekday = dd.getDay();
d.weekday = dd.getDay(); // 0 is Sunday
d.hour = dd.getHours();
d.minute = dd.getMinutes();
d.second = dd.getSeconds();
Expand Down Expand Up @@ -156,6 +175,20 @@ public long getTime()
public int getYear()
{ return year; }

public String getYearDays()
{ OclDate d0 = newOclDate();
d0.month = 1;
d0.day = 1;
d0.hour = 0;
d0.minute = 0;
d0.second = 0;
int days = OclDate.daysBetweenDates(d0,this);
String zero = "";
if (days < 100)
{ zero = "0"; }
return (year) + zero + days;
}

public int getMonth()
{ return month; }

Expand All @@ -164,6 +197,9 @@ public int getDate()

public int getDay()
{ return weekday; }

public String getDayName()
{ return OclDate.dayname.get("" + weekday); }

public int getHours()
{ return hour; }
Expand All @@ -183,6 +219,9 @@ public int getSeconds()
public int getSecond()
{ return second; }

public int getMicrosecond()
{ return microsecond; }

public OclDate addYears(int y)
{ long newtime = time + y*YEARMS;
return newOclDate_Time(newtime);
Expand Down Expand Up @@ -462,12 +501,24 @@ public static void setSystemTime(long t)
{ OclDate.systemTime = t; }

public static void main(String[] args)
{ OclDate d1 = OclDate.newOclDate_YMD(2023,7,1);
OclDate d2 = OclDate.newOclDate_YMD(2024,11,1);
int dd = OclDate.daysBetweenDates(d1,d2);
System.out.println(d2.isLeapYear());
{ // OclDate d1 = OclDate.newOclDate_YMD(2023,7,1);
// OclDate d2 = OclDate.newOclDate_YMD(2024,11,1);
// int dd = OclDate.daysBetweenDates(d1,d2);
// System.out.println(d2.isLeapYear());

long syst = OclDate.getSystemTime();
long coboltimer = (long) ((syst % 86400000)/2.4)*1000;
System.out.println(coboltimer);

/* System.out.println(OclDate.getSystemTime());
OclDate d = OclDate.newOclDate();
System.out.println(d.getYearDays());
System.out.println(d.getDayName());
System.out.println(d.getMicrosecond());
System.out.println(OclDate.getSystemTime());
OclDate d1 = OclDate.newOclDate();
System.out.println(d1.getMicrosecond());
*/
}

}
Expand Down
17 changes: 16 additions & 1 deletion libraries/ocl.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,23 @@ def includesAll(supset,subset) :
return True



def excludesAll(supset,subset) :
for x in subset :
if x in supset :
return False
return True

def includesValue(mp, x) :
for k in mp :
if mp[k] == x :
return True
return False

def excludesValue(mp, x) :
for k in mp :
if mp[k] == x :
return False
return True

def iterate(sq,init,f) :
acc = init
Expand Down Expand Up @@ -542,6 +552,7 @@ def sequenceSubrange(l, i, j) :
result.append(l[k])
return result


def insertAt(x,i,s) :
# i must be > 0
if i <= 0 :
Expand Down Expand Up @@ -1142,5 +1153,9 @@ def values(m) :
# ss = [1, 4, 6, 7, 2]
# print(listSubrange(ss, 2, -1))

# mp = dict({"a": 1, "b": 2})

# print(excludesValue(mp,2))
# print(excludesValue(mp,4))


62 changes: 53 additions & 9 deletions libraries/ocldate.km3
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class OclDate
stereotype component;

static attribute systemTime : long;
static attribute dayname : Map(String,String) := Map{ "0" |-> "Sunday", "1" |-> "Monday", "2" |-> "Tuesday", "3" |-> "Wednesday", "4" |-> "Thursday", "5" |-> "Friday", "6" |-> "Saturday", "7" |-> "Sunday" };

attribute time : long;
attribute year : int;
Expand All @@ -14,14 +15,31 @@ class OclDate
attribute hour : int;
attribute minute : int;
attribute second : int;
attribute microsecond : int;

static operation newOclDate() : OclDate
pre: true
post: OclDate->exists( d | d.time = OclDate.systemTime & result = d );

static operation newOclDate_String(s : String) : OclDate
pre: true post: true
activity: var items : Sequence(String) ; items := s->allMatches("[0-9]+"); var d : OclDate; d := OclDate.newOclDate_Time(0) ; if items->size() >= 3 then d.year := items->at(1)->toInteger() ; d.month := items->at(2)->toInteger() ; d.day := items->at(3)->toInteger() else skip ; if items->size() >= 6 then d.hour := items->at(4)->toInteger() ; d.minute := items->at(5)->toInteger() ; d.second := items->at(6)->toInteger() else skip ; return d;
activity:
var items : Sequence(String) ;
items := s->allMatches("[0-9]+");
var d : OclDate; d := OclDate.newOclDate_Time(0) ;
if items->size() >= 3
then
d.year := items->at(1)->toInteger() ;
d.month := items->at(2)->toInteger() ;
d.day := items->at(3)->toInteger()
else skip ;
if items->size() >= 6
then
d.hour := items->at(4)->toInteger() ;
d.minute := items->at(5)->toInteger() ;
d.second := items->at(6)->toInteger()
else skip ;
return d;

static operation newOclDate_Time(t : long) : OclDate
pre: true
Expand Down Expand Up @@ -109,29 +127,55 @@ class OclDate
pre: true
post: result = month;

operation getYearDays() : String
pre: true
post: true
activity:
var d0 : OclDate := OclDate.newOclDate_YMDHMS(year,1,1,0,0,0) ;
var days : int := OclDate.daysBetweenDates(d0,self) ;
var zero : String := "" ;
if days < 100 then zero := "0" else skip ;
return year + zero + days;

operation getDate() : int
pre: true post: result = day;
pre: true
post: result = day;

operation getDay() : int
pre: true post: result = weekday;
pre: true
post: result = weekday;

operation getDayName() : String
pre: true
post: result = OclDate.dayname["" + weekday];

operation getHour() : int
pre: true post: result = hour;
pre: true
post: result = hour;

operation getHours() : int
pre: true post: result = hour;
pre: true
post: result = hour;

operation getMinute() : int
pre: true post: result = minute;
pre: true
post: result = minute;

operation getMinutes() : int
pre: true post: result = minute;
pre: true
post: result = minute;

operation getSecond() : int
pre: true post: result = second;
pre: true
post: result = second;

operation getSeconds() : int
pre: true post: result = second;
pre: true
post: result = second;

operation getMicrosecond() : int
pre: true
post: result = microsecond;

operation addYears(y : int) : OclDate
pre: true
Expand Down
Loading

0 comments on commit 6d1c0fd

Please sign in to comment.