-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathowljs-grep.js
executable file
·121 lines (103 loc) · 4.3 KB
/
owljs-grep.js
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
var Parser = require('ringo/args').Parser;
var system = require('system');
var markdown = require('owljs/io/markdown');
var {OWL} = require("owljs");
// have this available for evaluation of user functions
importPackage(Packages.org.semanticweb.owlapi.model);
function main(args) {
var script = args.shift();
var parser = new Parser(system.args);
var grepFunc = null;
parser.addOption('h', 'help', null, 'Display help');
parser.addOption('v', 'invertMatch', null, 'Invert (negate) match');
parser.addOption('o', 'outputFile', 'File', 'output file (defaults to stdout)');
parser.addOption('f', 'grepFunctionFile', 'File', 'file containing js that evals to grepFunc. If specified no FUNCTION arg is specified');
parser.addOption('e', 'grepEntities', null, 'True if entities (classes, individuals, properties) are to be grepped, no axioms ');
parser.addOption('m', 'match', 'Regexp', 'regular expression applied to serialization of each axiom. E.g. /epithelium/. If specified, no FUNCTION arg is specified.');
parser.addOption('t', 'toOutputFormat', 'OWLOntologyFormat', 'output format (defaults to RDFXML)');
parser.addOption('j', 'jsFrames', null, 'writes output as js frames. TODO');
parser.addOption('M', 'markdown', null, 'writes output as markdown.');
var options = parser.parse(args);
if (options.help) {
print("Usage: owljs-grep OPTIONS [FUNCTION] OWLFILE\n");
print("Filters axioms from an ontology using a custom function. See owl.grepAxioms() for more detauls");
print("\nOptions:");
print(parser.help());
print("\nExample:");
print("owljs-grep 'function(ax){ return ax.isLogicalAxiom() }' foo.owl");
print("\nExample: (use saved function, negate query, write in functional notation)");
print("owljs-grep -v -t ofn -f lib/owlfunc/axiomIsLogical.js foo.owl");
system.exit('-1');
}
var repl;
var owl = new OWL();
owl.addCatalog();
if (options.toOutputFormat != null) {
console.log("Setting format to "+options.toOutputFormat);
owl.setDefaultFormat(options.toOutputFormat);
}
if (options.match != null) {
var mf = eval(options.match);
if (options.grepEntities) {
grepFunc = function(obj) { var robj = markdown.renderOWLObject(obj,owl); return mf.test(robj); };
}
else {
grepFunc = function(ax) { return mf.test(ax.toString()); };
}
}
if (options.grepFunctionFile != null) {
var fs = require("fs");
grepFunc = eval(fs.read(options.grepFunctionFile));
}
if (grepFunc == null) {
var grepFuncStr = args.shift();
grepFunc = eval(grepFuncStr);
}
args.forEach(function(fn) { owl.loadFile(fn) } );
if (options.load != null) {
owl.log("Loading "+options.load);
}
if (options.grepEntities) {
var filteredObjects = owl.grepObjects(grepFunc, options.invertMatch, true);
owl.log("#filteredObjects = " + filteredObjects.length);
if (options.jsFrames) {
repl = require("owljs/repl");
repl.owlinit(owl);
for (var k in filteredObjects) {
var obj = filteredObjects[k];
print(repl.render(owl.getFrame(obj)));
}
}
if (options.markdown) {
for (var k in filteredObjects) {
var obj = filteredObjects[k];
print(repl.render(markdown.renderOWLObject(obj,owl)));
}
}
else {
owl.log("Saving to " + options.outputFile);
owl.save(options.outputFile);
}
}
else {
var filteredAxioms = owl.grepAxioms(grepFunc, options.invertMatch, true);
owl.log("#filteredAxioms = " + filteredAxioms.length);
if (options.jsFrames) {
var repl = require("owljs/repl");
repl.owlinit(owl);
//repl.owl = owl;
for (var k in filteredAxioms) {
var ax = filteredAxioms[k];
print(repl.render(ax));
}
}
else {
owl.log("Saving to " + options.outputFile);
owl.save(options.outputFile);
}
}
}
// call the main method; ringo specific
if (require.main == module.id) {
main(system.args);
}