# Copyright 2006 Instituto de Investigaciones Dr. José María Luis Mora / # Instituto de Investigaciones Estéticas. # See COPYING.txt and LICENSE.txt for redistribution conditions. # # D.R. 2006 Instituto de Investigaciones Dr. José María Luis Mora / # Instituto de Investigaciones Estéticas. # Véase COPYING.txt y LICENSE.txt para los términos bajo los cuales # se permite la redistribución. require 'kr_logic/def_files/parser' require 'kr_logic/def_files/interpreter' module KRLogic module DefFiles Parser.prepare @@def_files = Array.new @@global_index = Hash.new def DefFiles.process path = AppConfig.def_file_dir d = Dir.new(path) def_file_names = d.entries.reject{|f| not (f =~ /.*\.def$/)} def_file_names.each do |def_file_name| begin f = File.new(path + def_file_name,"r") def_file = DefFile.new(def_file_name, path) def_file.contents = Parser.parse(f.read, def_file) @@def_files.push(def_file) ensure f.close end end Interpreter.interpret(@@def_files) end module Indexer def Indexer.indexed(index, cl, i) index[cl][i] end def Indexer.all_indexed(index, cl) i = index[cl] i ? i : [] end def Indexer.count_indexed(index, cl) index[cl].size end def Indexer.get_index(index, cl) index[cl] = Array.new unless index.has_key?(cl) index[cl] end def Indexer.add_to_index(index, element) Indexer.get_index(index, element.class).push(element) end end def DefFiles.all_elements(cl) Indexer.all_indexed(@@global_index, cl) end def DefFiles.count_elements(cl) Indexer.count_indexed(@@global_index, cl) end def DefFiles.add_to_global_index(element) Indexer.add_to_index(@@global_index, element) end class DefFile include RJBM::JAccess j_interface('mx.org.pescador.definitionsfiles.DefFile') # Available from java: only the :name attribute attr_accessor(:name, :location, :contents, :realm) def initialize(name, location) @name = name @location = location @index = Hash.new j_bind end def element(cl, i=0) Indexer.indexed(@index, cl, i) end def count_elements(cl) Indexer.count_indexed(@index, cl) end def add_to_index(element) Indexer.add_to_index(@index, element) end end class DefFileContents attr_reader(:elements) @@allowed_contents = [Realm, DefinitionSet] def initialize(elements) @elements = elements @elements.each do |e| raise "#{e.class.name} not allowed as top-level element: line #{e.line}, #{e.file}." unless (@@allowed_contents.include?(e.class)) end end end end end