Skip to content

Commit

Permalink
new schema.jar now works with default generateCore and generateExtens…
Browse files Browse the repository at this point in the history
…ion. file export is now possible.
  • Loading branch information
Lancewiu committed May 15, 2019
1 parent 8bc82b8 commit 4677f04
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 30 deletions.
38 changes: 38 additions & 0 deletions +spec/getNamespaceInfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
%returns sources given namespace java object
function s = getNamespaceInfo(namespaceMap)
errid = 'MATNWB:INVALIDFILE';
errmsg = 'Could not read namespace file. Invalid format.';

assert(namespaceMap.containsKey('namespaces'), errid, errmsg);
namespaceIter = namespaceMap.get('namespaces').iterator();

%spawn empty struct. Produce struct array of all defined namespaces
s = struct('filenames', {}, 'name', {}, 'dependencies', {}, 'version', {});
sidx = 1;
while namespaceIter.hasNext()
namespace = namespaceIter.next();
assert(namespace.containsKey('name')...
&& namespace.containsKey('schema')...
&& namespace.containsKey('version'),...
errid, errmsg);
name = namespace.get('name');
version = namespace.get('version');
filenames = {};
dependencies = {};
schemaIter = namespace.get('schema').iterator();
while schemaIter.hasNext()
schemaFile = schemaIter.next();
if schemaFile.containsKey('source')
filenames{end+1} = schemaFile.get('source');
elseif schemaFile.containsKey('namespace')
dependencies{end+1} = schemafile.get('namespace');
else
error(errid, errmsg);
end
end
s(sidx) = struct('name', name,...
'filenames', {filenames},...
'dependencies', {dependencies},...
'version', version);
end
end
17 changes: 17 additions & 0 deletions +spec/getSourceInfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function processed = getSourceInfo(classmap)
%given containers.Map of (file/module)->(string) returns (file/module)->HashMap
% representing the schema file.
processed = containers.Map;
schema = Schema();
classkeys = keys(classmap);

for i=1:length(classkeys)
ck = classkeys{i};
cval = classmap(ck);
try
processed(ck) = schema.read(cval);
catch ME
error('MATNWB:INVALIDFILE',...
'Data for namespace source `%s` is invalid', ck);
end
end
12 changes: 0 additions & 12 deletions +yaml/getNamespaceInfo.m

This file was deleted.

8 changes: 0 additions & 8 deletions +yaml/getSourceInfo.m

This file was deleted.

26 changes: 22 additions & 4 deletions generateExtension.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function generateExtension(source)
%
% Example:
% generateCore('schema\core\nwb.namespace.yaml');
% generateExtension('schema\core\myextension.namespace.yaml')
% generateExtension('schema\myext\myextension.namespace.yaml')
%
% See also GENERATECORE
validateattributes(source, {'char', 'string'}, {'scalartext'});
Expand All @@ -25,10 +25,28 @@ function generateExtension(source)
if ~any(strcmp(javaclasspath(), javapath))
javaaddpath(javapath);
end
schema = Schema();
[localpath, ~, ~] = fileparts(source);
[filenames, nm, dep] = yaml.getNamespaceInfo(source);
schema = yaml.getSourceInfo(localpath, filenames{:});
extSchema = struct('name', nm, 'schema', schema, 'dependencies', {dep});
assert(2 == exist(source, 'file'),...
'MATNWB:FILE', 'Path to file `%s` could not be found.', source);
fid = fopen(source);
namespace_map = schema.read(fread(fid, '*char') .');
fclose(fid);
namespace = spec.getNamespaceInfo(namespace_map);

schema_map = containers.Map;
for i=1:length(namespace.filenames)
filename = namespace.filenames{i};
fid = fopen(fullfile(localpath, filename));
schema_map(filename) = fread(fid, '*char') .';
fclose(fid);
end
schema = spec.getSourceInfo(schema_map);

extSchema = struct('name', namespace.name,...
'schema', schema,...
'dependencies', {namespace.dependencies},...
'version', namespace.version);
namespacePath = fullfile(nwbloc, 'namespaces');
if 7 ~= exist(namespacePath, 'dir')
mkdir(namespacePath);
Expand Down
Binary file modified jar/schema.jar
Binary file not shown.
13 changes: 7 additions & 6 deletions nwbExport.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ function nwbExport(nwb, filename)
% nwbRead(nwb,filename) Writes the nwb object to a file at filename.
%
% Example:
% %Generate Matlab code for the NWB objects from the core schema.
% %This only needs to be done once.
% % Generate Matlab code for the NWB objects from the core schema.
% % This only needs to be done once.
% generateCore('schema\core\nwb.namespace.yaml');
% %Create some fake fata and write
% % Create some fake fata and write
% nwb = nwbfile;
% nwb.epochs = types.core.Epochs;
% nwb.epochs.stim = types.Epoch;
% nwbExport(nwb, 'epoch.nwb');
% nwb.session_start_time = datetime('now');
% nwb.identifier = 'EMPTY';
% nwb.session_description = 'empty test file';
% nwbExport(nwb, 'empty.nwb');
%
% See also GENERATECORE, GENERATEEXTENSION, NWBFILE, NWBREAD
validateattributes(nwb, {'nwbfile'}, {'nonempty'});
Expand Down

0 comments on commit 4677f04

Please sign in to comment.