//Copyright (C) 2005 Richard J. Northedge
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//This file is based on the BinaryGISModelReader.java source file found in the
//original java implementation of MaxEnt. That source file contains the following header:
// Copyright (C) 2001 Jason Baldridge and Gann Bierner
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.MachineLearning.IO
{
///
/// A reader for GIS models stored in a binary format. This format is not the one
/// used by the java version of MaxEnt.
/// It has two main differences, designed for performance when loading the data
/// from file: first, it uses big endian data values, which is native for C#, and secondly it
/// encodes the outcome patterns and values in a more efficient manner.
///
///
/// Jason Baldridge
///
///
/// Richard J. Northedge
///
///
/// based on BinaryGISModelReader.java, $Revision: 1.1.1.1 $, $Date: 2001/10/23 14:06:53 $
///
public class BinaryGisModelReader : GisModelReader
{
private readonly Stream _input;
private readonly byte[] _buffer;
private int _stringLength = 0;
private readonly Encoding _encoding = Encoding.UTF8;
///
/// Constructor which directly instantiates the Stream containing
/// the model contents.
///
///
/// The Stream containing the model information.
///
public BinaryGisModelReader(Stream dataInputStream)
{
using (_input = dataInputStream)
{
_buffer = new byte[256];
base.ReadModel();
}
}
///
/// Constructor which takes a filename and creates a reader for it.
///
///
/// The full path and name of the file in which the model is stored.
///
public BinaryGisModelReader(string fileName)
{
using (_input = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
_buffer = new byte[256];
base.ReadModel();
}
}
///
/// Reads a 32-bit signed integer from the model file.
///
protected override int ReadInt32()
{
_input.Read(_buffer, 0, 4);
return BitConverter.ToInt32(_buffer, 0);
}
///
/// Reads a double-precision floating point number from the model file.
///
protected override double ReadDouble()
{
_input.Read(_buffer, 0, 8);
return BitConverter.ToDouble(_buffer, 0);
}
///
/// Reads a UTF-8 encoded string from the model file.
///
protected override string ReadString()
{
_stringLength = _input.ReadByte();
_input.Read(_buffer, 0, _stringLength);
return _encoding.GetString(_buffer, 0, _stringLength);
}
///
/// Reads the predicate data from the file in a more efficient format to that implemented by
/// GisModelReader.
///
///
/// Jagged 2-dimensional array of integers that will contain the outcome patterns for the model
/// after this method is called.
///
///
/// Dictionary that will contain the predicate information for the model
/// after this method is called.
///
protected override void ReadPredicates(out int[][] outcomePatterns, out Dictionary predicates)
{
//read from the model how many outcome patterns there are
int outcomePatternCount = ReadInt32();
outcomePatterns = new int[outcomePatternCount][];
//read from the model how many predicates there are
predicates = new Dictionary(ReadInt32());
//for each outcome pattern in the model
for (int currentOutcomePattern = 0; currentOutcomePattern < outcomePatternCount; currentOutcomePattern++)
{
//read the number of outcomes in this pattern. This number is 1 greater than the real number of outcomes
//in the pattern, because the 0th value contains the number of predicates that use this pattern.
var currentOutcomePatternLength = ReadInt32();
outcomePatterns[currentOutcomePattern] = new int[currentOutcomePatternLength];
//read in the outcomes for this pattern
for (int currentOutcome = 0; currentOutcome