Skip to content

Commit

Permalink
extended OclDatasource and Ocl libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Mar 3, 2025
1 parent cf2010f commit 7ab01be
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 2 deletions.
Binary file modified libraries/Ocl.class
Binary file not shown.
22 changes: 20 additions & 2 deletions libraries/Ocl.java
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,22 @@ public static <T> ArrayList<T> intersection(ArrayList<T> a, Collection<T> b)
return res;
}

public static <T> boolean isDisjointFrom(ArrayList<T> a, Collection<T> b)
{ for (int i = 0; i < a.size(); i++)
{ if (b.contains(a.get(i)))
{ return false; }
}
return true;
}

public static <T> boolean isDisjointFrom(Set<T> a, Collection<T> b)
{ for (T x : a)
{ if (b.contains(x))
{ return false; }
}
return true;
}

public static <T,R> HashMap<T,R> intersectAllMap(Collection<Map<T,R>> col)
{ HashMap<T,R> res = new HashMap<T,R>();
if (col.size() == 0)
Expand Down Expand Up @@ -1092,15 +1108,17 @@ public static <T> int count(Collection<T> l, T obj)

public static int count(String s, String x)
{ int res = 0;
if ("".equals(s)) { return res; }
if ("".equals(s))
{ return res; }

int ind = s.indexOf(x);
if (ind == -1) { return res; }
String ss = s.substring(ind+1,s.length());
res++;
while (ind >= 0)
{ ind = ss.indexOf(x);
if (ind == -1 || ss.equals(""))
{ return res; }
{ return res; }
res++;
ss = ss.substring(ind+1,ss.length());
}
Expand Down
8 changes: 8 additions & 0 deletions libraries/Ocl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,14 @@ class Ocl
return result
}

static func hasValue<T>(s : Dictionary<String,T>, v : T) -> Bool
{ for (_,x) in s
{ if (x == v)
{ return true }
}
return false
}

static func exists<T>(s : Dictionary<String,T>, f : (T) -> Bool) -> Bool
{ for (_,v) in s
{ if (f(v))
Expand Down
Binary file modified libraries/OclDatasource.class
Binary file not shown.
17 changes: 17 additions & 0 deletions libraries/OclDatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,23 @@ void OclDatasource::close()
httpConnection = NULL;
}

void OclDatasource::closeFile()
{ if (database != NULL)
{ sqlite3_close(database); }
hasLocalSession = FALSE;
url = "";
host = "";
file = "";
port = 0;
passwd = "";
schema = "";
database = NULL;
protocol = "";
if ( httpRequest != NULL ) WinHttpCloseHandle( httpRequest );
if ( httpConnection != NULL ) WinHttpCloseHandle( httpConnection );
httpRequest = NULL;
httpConnection = NULL;
}

void OclDatasource::commit()
{ }
Expand Down
2 changes: 2 additions & 0 deletions libraries/OclDatasource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class OclDatasource {

void close();

void closeFile();

void connect();

OclDatasource* openConnection();
Expand Down
2 changes: 2 additions & 0 deletions libraries/OclDatasource.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ public void close()
}
}

public void closeFile()
{ this.close(); }

public void commit()
{ if (connection != null)
Expand Down
2 changes: 2 additions & 0 deletions libraries/OclDatasource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ class OclDatasource : InternetCallback
sqlite3_close(self.dbPointer)
}

func closeFile() -> Void
{ self.close() }

func commit() -> Void
{
Expand Down
91 changes: 91 additions & 0 deletions libraries/OrderedMap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package orderedMap {


class OrderedMap<K,T> {
attribute elements : Sequence(K);
attribute items : Map(K,T);

query atIndex(i : int) : T
pre: i > 0 & i <= elements->size()
post:
result = items->at(elements[i]);

query at(k : K) : T
pre: true
post:
result = items->at(k);

query keys() : Set(K)
pre: true
post:
result = items->keys();

query values() : Sequence(T)
pre: true
post:
result = elements->collect( k | items->at(k) );

query includesKey(k : K) : boolean
pre: true
post:
items->includesKey(k) => result = true;

query excludesKey(k : K) : boolean
pre: true
post:
items->excludesKey(k) => result = true;

query includesValue(v : T) : boolean
pre: true
post:
items->includesValue(v) => result = true;

query excludesValue(v : T) : boolean
pre: true
post:
items->excludesValue(v) => result = true;

operation including(k : K , t : T)
pre: true
post: true
activity:
if items->excludesKey(k)
then
elements := elements->append(k)
else skip;
items[k] := t;

operation excludingAt(i : int)
pre: i > 0 & i <= elements->size()
post: true
activity:
var k : K := elements[i];
elements := elements->excludingAt(i);
items := items->excludingKey(k);

operation excludingKey(k : K)
pre: true
post: true
activity:
elements := elements->excluding(k);
items := items->excludingKey(k);

operation excludingValue(v : T)
pre: true
post: true
activity:
var removed : Set(K) := Set{};
for k : elements
do
if items[k] = v
then
removed := removed->including(k)
else skip;
elements := elements - removed;
items := items->antirestrict(removed);

}



}
Binary file modified libraries/SQLStatement.class
Binary file not shown.
31 changes: 31 additions & 0 deletions libraries/SQLStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ public static int typeToSQLType(Object obj)
return Types.OTHER;
}

public static boolean isValidSQL(String stat)
{ if (stat != null &&
stat.length() > 0)
{ int n = stat.length();
int cnt = Ocl.count(stat, "'");
if (stat.charAt(n-1) == ';' &&
cnt % 2 == 0)
{ return true; }
}
return false;
} // quotes are paired

public void close()
{ if (statement != null)
{ try {
Expand Down Expand Up @@ -276,6 +288,20 @@ public void execute(String stat)
}
}

public void executeMany(String stat, ArrayList dataseq)
{
if (statement != null &&
statement instanceof PreparedStatement)
{ try {
for (int i = 0; i < dataseq.size(); i++)
{ ArrayList vals = (ArrayList) dataseq.get(i);
SQLStatement.setParameters(
(PreparedStatement) statement,vals);
statement.execute(stat);
}
} catch (SQLException ex) { }
}
}

public void execute()
{ execute(text); }
Expand All @@ -293,5 +319,10 @@ public OclDatasource getConnection()
public OclIterator getResultSet()
{ return resultSet; }

public static void main(String[] args)
{ System.out.println(SQLStatement.isValidSQL("SELECT 'foo FROM bar;"));
System.out.println(SQLStatement.isValidSQL("SELECT foo "));
}

}

14 changes: 14 additions & 0 deletions libraries/ocldatasource.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ void close_OclDatasource(struct OclDatasource* self)
self->protocol = "";
}

void closeFile_OclDatasource(struct OclDatasource* self)
{ if (self->database != NULL)
{ sqlite3_close(self->database); }
self->hasLocalSession = FALSE;
self->url = "";
self->host = "";
self->file = "";
self->port = 0;
self->passwd = "";
self->schema = "";
self->database = NULL;
self->protocol = "";
}


void commit_OclDatasource(struct OclDatasource* self)
{ }
Expand Down
11 changes: 11 additions & 0 deletions libraries/ocldatasource.km3
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ package ocldatasource {

attribute text : String;


static operation isValidSQL(stat : String) : boolean
pre: true
post: (stat->hasSuffix(";") & (stat->count("'") mod 2) = 0 => result = true);

operation close()
pre: true post: true;

Expand Down Expand Up @@ -70,6 +75,9 @@ package ocldatasource {

operation execute(stat : String) : void
pre: true post: true;

operation executeMany(stat : String, valueseq : Sequence(OclAny)) : void
pre: true post: true;

operation execute() : void
pre: true post: true;
Expand Down Expand Up @@ -179,6 +187,9 @@ package ocldatasource {
operation close() : void
pre: true post: true;

operation closeFile() : void
pre: true post: true;

operation commit() : void
pre: true post: true;

Expand Down
18 changes: 18 additions & 0 deletions libraries/ocldatasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ def parameterMapToFields(n,d) :
res[x-1] = None
return res

def isValidSQL(stat) :
return sqlite3.complete_statement(stat)

def close(self) :
if self.statement != None :
self.statement.close()
Expand Down Expand Up @@ -209,6 +212,18 @@ def execute(self, stat=None) :
if self.connection != None :
self.connection.commit()

def executeMany(self, stat, valueseq) :
if self.statement != None :
if stat == None :
stat = self.text
for vals in valueseq :
self.setParameters(vals)
pars = SQLStatement.parameterMapToFields(
self.maxfield,self.parameters)
self.statement.execute(stat,pars)
if self.connection != None :
self.connection.commit()

def cancel(self) :
if self.statement != None :
self.statement.close()
Expand Down Expand Up @@ -426,6 +441,9 @@ def close(self) :
if self.clientSocket != None :
self.clientSocket.close()

def closeFile(self) :
self.close()

def commit(self) :
if self.con != None :
self.con.commit()
Expand Down

0 comments on commit 7ab01be

Please sign in to comment.