-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolymakeInstanceWrapper.mm
262 lines (196 loc) · 9.61 KB
/
PolymakeInstanceWrapper.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/***********************************************************************
* Created by Andreas Paffenholz on 01/07/14.
* Copyright 2012-2014 by Andreas Paffenholz.
* 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 2, or (at your option) any
* later version: http://www.gnu.org/licenses/gpl.txt.
* 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.
*
* PolymakeInstanceWrapper.mm
* PolyViewer
**************************************************************************/
#import "PolymakeInstanceWrapper.h"
#import "polymake/Main.h"
#import "polymake/Matrix.h"
#import "polymake/Array.h"
#import "polymake/Set.h"
#import "polymake/SparseMatrix.h"
#import "polymake/Rational.h"
@interface PolymakeInstanceWrapper () {
polymake::Main pm_instance;
}
@end
@implementation PolymakeInstanceWrapper
#pragma mark init
- (id)init {
NSLog(@"[PolymakeInstanceWrapper init] entering");
self = [super init];
if ( self ) {
NSLog(@"[PolymakeInstanceWrapper init] leaving");
return self;
}
NSLog(@"[PolymakeInstanceWrapper init] leaving, init failed");
return nil;
}
/********************************************************************
*
* create a scope within our instance
*
********************************************************************/
- (void)createScope {
NSLog(@"[PolymakeInstanceWrapper createScope] entering");
polymake::perl::Scope main_polymake_scope(polymake::perl::Scope(pm_instance.newScope()));
pm_instance.set_application("common");
NSLog(@"[PolymakeInstanceWrapper createScope] leaving");
}
# pragma mark config-info
/***************************************************************/
-(NSArray *)configuredExtensions {
NSMutableArray * extensions = [NSMutableArray array];
NSLog(@"[PolymakeInstanceWrapper configuredExtensions] called");
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"get_configured_extensions" ofType:@"pl"];
polymake::perl::ListResult results = ListCallPolymakeFunction("script",[filePath UTF8String]);
for (int i=0, end=results.size(); i<end; ++i) {
NSString * ext = [[NSString alloc] initWithUTF8String:results[i]];
[extensions addObject:ext];
}
NSLog(@"[PolymakeInstanceWrapper configuredExtensions] found the following configured extensions: %@", extensions);
return extensions;
}
# pragma mark database
/********************************************************************
*
* functions not bound to a particular polymake object
* executed in the main scope of polymake
*
********************************************************************/
// get the list of databases
-(NSArray *)databaseNames {
NSLog(@"[PolymakeInstanceWrapper databaseNames] entering");
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"get_db_list" ofType:@"pl"];
polymake::perl::ListResult results = ListCallPolymakeFunction("script",[filePath UTF8String]);
if ( results.size() == 1 ) {
NSLog(@"[PolymakeInstanceWrapper databaseNames] checking success");
const char* dbstring = results[0];
char * err = strstr(dbstring, "ERROR");
if (err != NULL) {
char * err_db = strstr(dbstring, "connect to server");
char * err_timeout = strstr(dbstring, "timed out");
if (err_db != NULL) {
[self showCommandFailedAlert:@"could not connect to database server"];
} else if (err_timeout != NULL) {
[self showCommandFailedAlert:@"database query time out"];
} else {
[self showCommandFailedAlert:@"an unknown error occured"];
}
return nil;
}
}
NSMutableArray * databases = [NSMutableArray array];
for (int i=0, end=results.size(); i<end; ++i) {
NSLog(@"[PolymakeInstanceWrapper databaseNames] in loop");
const char* dbstring = results[i];
if (strlen(dbstring) > 0) {
NSString * db = [[NSString alloc] initWithUTF8String:dbstring];
[databases addObject:db];
}
}
NSLog(@"[PolymakeInstanceWrapper databaseNames] leaving");
return databases;
}
// given a database get all collections
-(NSArray *)collectionNamesofDatabase:(NSString *)db {
NSLog(@"[PolymakeInstanceWrapper collectionNamesofDatabase] entering with database %@", db);
if ( [db isEqualToString:@""] )
return nil;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"get_db_collection_list" ofType:@"pl"];
polymake::perl::ListResult results = ListCallPolymakeFunction("script",[filePath UTF8String],[db cStringUsingEncoding:NSUTF8StringEncoding]);
NSLog(@"[PolymakeInstanceWrapper collectionNamesofDatabase] found %d results", results.size());
NSMutableArray * collections = [NSMutableArray array];
if ( results.size() == 1 ) {
NSLog(@"[PolymakeInstanceWrapper collectionNamesOfDatabase] checking success");
const char* dbstring = results[0];
char * err = strstr(dbstring, "ERROR");
if (err != NULL) {
char * err_db = strstr(dbstring, "connect to server");
char * err_timeout = strstr(dbstring, "timed out");
if (err_db != NULL) {
[self showCommandFailedAlert:@"could not connect to database server"];
} else if (err_timeout != NULL) {
[self showCommandFailedAlert:@"database query time out"];
} else {
[self showCommandFailedAlert:@"an unknown error occured"];
}
return collections;
}
}
for (int i=0, end=results.size(); i<end; ++i) {
const char* collstring = results[i];
if (strlen(collstring) > 0) {
NSLog(@"[PolymakeInstanceWrapper collectionNamesofDatabase] in loop, adding %s", collstring);
NSString * coll = [[NSString alloc] initWithUTF8String:results[i]];
[collections addObject:coll];
}
}
NSLog(@"[PolymakeInstanceWrapper collectionNamesofDatabase] leaving");
return collections;
}
/***************************************************************/
// get amount many IDs for all elements in a collection of a database, skipping the first start many elements
-(NSArray *)idsForDatabase:(NSString *)db andCollection:(NSString *)coll withAddtionalProperties:(NSString *)additionalProps restrictToAmount:(NSNumber *)amount startingAt:(NSNumber *)start {
NSMutableArray * ids = [NSMutableArray array];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"get_db_ListOfIDs" ofType:@"pl"];
polymake::perl::ListResult results =
ListCallPolymakeFunction("script",[filePath UTF8String],[db cStringUsingEncoding:NSUTF8StringEncoding],
[coll cStringUsingEncoding:NSUTF8StringEncoding],
(long)amount,
(long)start,
[additionalProps cStringUsingEncoding:NSUTF8StringEncoding]);
if ( results.size() == 1 ) {
NSLog(@"[PolymakeInstanceWrapper idsForDatabase andCollection withAdditionalProperties restrictToAmount startingAt] checking success");
const char* dbstring = results[0];
char * err = strstr(dbstring, "ERROR");
if (err != NULL) {
char * err_db = strstr(dbstring, "connect to server");
char * err_timeout = strstr(dbstring, "timed out");
if (err_db != NULL) {
[self showCommandFailedAlert:@"could not connect to database server"];
} else if (err_timeout != NULL) {
[self showCommandFailedAlert:@"database query time out"];
} else {
[self showCommandFailedAlert:@"an unknown error occured"];
}
return ids;
}
}
NSLog(@"[PolymakeInstanceWrapper collectionNamesofDatabase] retrieved: %d", results.size());
for (int i=0, end=results.size(); i<end; ++i) {
NSString * id = [[NSString alloc] initWithUTF8String:results[i]];
[ids addObject:id];
}
return ids;
}
/***************************************************************/
// get the number of elements in a collection of a database
-(NSInteger)countForDatabase:(NSString *)db andCollection:(NSString *)coll withAddtionalProperties:(NSString *)additionalProps {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"get_db_CountOfIDs" ofType:@"pl"];
int count = CallPolymakeFunction("script",[filePath UTF8String],[db cStringUsingEncoding:NSUTF8StringEncoding],
[coll cStringUsingEncoding:NSUTF8StringEncoding],
[additionalProps cStringUsingEncoding:NSUTF8StringEncoding]);
return count;
}
/***************************************************************/
// error function
- (void)showCommandFailedAlert:(NSString *)reason {
NSAlert *alert = [NSAlert alertWithMessageText:reason
defaultButton:@"OK"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@""];
[alert runModal];
}
@end