473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Advice on input organisation

Hi,

I am working on a computational program that has to read a number of
parameters (~50) from an input file. The program contains a single class
hierarchy with about a dozen member-classes or inherited classes, each of
which needs some subset of those input parameters. The classes may
individually perform some input validation, and even determine which
parameters are to be read next. Currently, each class performs its own
file input. This doesn't satisfy me, mainly because the program is hard
to read and maintain when the input is dispersed among many different
classes.

What would be a good strategy for organising input in this case?

My first impulse was to at least "outsource" the actual file input into a
separate class, which also keeps track of the current position and
context within the input file and can do meaningful error reporting. The
other classes utilise the input class interface to get the parameters
that they need. But that doesn't quite solve the problem, in my view.

Should I have a class that reads the input file in its entirety, and then
makes the results available to other classes? I see too problems(?) with
that:

- The parameter sequence is not entirely linear. Depending on context,
the input file may contain some parameters and not others. The logic is
currently determined by the classes that read their subsets of input
parameters. I would have to transfer that logic to the input-reading
class, which doesn't sound right.

- If a change in some class places a different requirement on input data,
this change would also have to be implemented in the input-reading class.
This seems to go against the encapsulation principle. Trouble is likely
if the input-class is not synchronised with all the other classes that
require input.

- Individual classes only need access to a subset of the input
parameters, not all of them. Is there some elegant way of giving each
class access only to those parameters that it needs, and not the entire
"catalogue"?

Any ideas?

Thanks.

P.S. I realise that this is not specifically a C++ question, but some
solutions may involve features that are specific to C++.
Jul 23 '05 #1
2 1956
On 20 May 2005 20:49:00 GMT, SophistiCat <no**@public.net> wrote:
Hi,

I am working on a computational program that has to read a number of
parameters (~50) from an input file. The program contains a single class
hierarchy with about a dozen member-classes or inherited classes, each of
which needs some subset of those input parameters. The classes may
individually perform some input validation, and even determine which
parameters are to be read next. Currently, each class performs its own
file input. This doesn't satisfy me, mainly because the program is hard
to read and maintain when the input is dispersed among many different
classes.

What would be a good strategy for organising input in this case?
I would go with your idea of a class which reads the entirety of the
input file but additionally try to divorce the input format from the
input requirements of the computational classes. If you can achieve
this, changes in the computational classes won't affect the parser.
An advantage of parsing the input file and storing it in the
appropriate data structure (which may be the same object as the
parser) is that you could simplify the input-access logic within the
computational classes. The input container could be a map, a sequence
of some type or of a custom type (which may have attributes of maps
and sequences), depending on your exact needs. Without knowing more
about the pattern of access, I can't make precise recommendations.
Could you provide a model of the non-linear access and examples of
input requirements you later mention? You've aroused my curiosity and
problem-solving drive.

If you have control over the format of the file, you could
additionally define the format so that parsing is easier.

[...]Should I have a class that reads the input file in its entirety, and then
makes the results available to other classes? I see too problems(?) with
that:

- The parameter sequence is not entirely linear. Depending on context,
the input file may contain some parameters and not others. The logic is
currently determined by the classes that read their subsets of input
parameters. I would have to transfer that logic to the input-reading
class, which doesn't sound right.
With an appropriate format for input, it sounds more right, for the
parser is creating a structure implied by the format rather than
trying to emulate the input behavior of the computational classes.
- If a change in some class places a different requirement on input data,
this change would also have to be implemented in the input-reading class.
This seems to go against the encapsulation principle. Trouble is likely
if the input-class is not synchronised with all the other classes that
require input.
By defining a format for the input file which is general enough, a
data structure which supports the format could accommodate changes in
the classes' input requirements.

If you can't make the input format/parser class general enough, you
could define methods of the computational classes (or define helper
classes for each computational class) to parse sections of the input
file.
You could also support some form of input requirement registration,
perhaps an interface for the parser which lets instances of the
computational classes tell the parser what their input requirements
are, or an interface for the computational classes which lets the
parser get the input requirements of the computational classes (or
their instances). (On a tangent, what would you call an "instances of
a computational class" within the context of your program? A
computational object?) With this approach, you define a meta-format
and the classes (or instances) define the input format. As a partial
example, parameters could be given different types. Classes (or
rather, instances) register parameters and their types with the
parser. The type of a parameter determines the parser's behavior when
it encounters it.

If you control the input format, you could include tags in the input
which define the type of the parameter; at its most extreme, the input
format could define a programming language and your program becomes an
interpreter. Whether or not you take this approach (parameter markup)
depends on whether you want the input or the computational objects to
drive the computation.
- Individual classes only need access to a subset of the input
parameters, not all of them. Is there some elegant way of giving each
class access only to those parameters that it needs, and not the entire
"catalogue"?

If you want to restrict the input accessible to the computational
objects, you could subclass the input container class and pass an
instance of the appropriate subclass to each computational object.
Unfortunately, this may have the problem you mentioned above wherein
changes in input requirements affect the input container [subclass].

If the input format is determined at runtime by requirement
registration, you could use the same information to give an instance
of a computational object the data it wants in the format it wants
(perhaps using subclasses of the input container class).

What seems most likely to me is that you don't want computational
objects to have to handle parameters which don't affect their
behavior. With the appropriate input container, you won't need to
worry about this.

Am I hitting anywhere near the mark?

Kanenas
Jul 23 '05 #2
Hi,

Thanks for your reply!

Kanenas <kanenas_@t_comcast_d.t_net> wrote in
news:lm********************************@4ax.com:
Without knowing more
about the pattern of access, I can't make precise recommendations.
Could you provide a model of the non-linear access and examples of
input requirements you later mention? You've aroused my curiosity and
problem-solving drive.
Input data consists of ints, floats, single-word strings, or letters. One
or more values per line, followed by an optional comment (lines starting
with # are ignored):

asdfgf Comment
128 64 Comment
2 2 blocks of data to follow:
# Data block 1
1E6
x
# Data block 2
1.5E6
y
# Data blocks end
0 1 0
1 This flag indicates that an extra data block will follow
# Data block begins
9.8
200E6
# Data block ends
dfgh Some more data...
1.0

etc.

As you can see, the code that performs input has to be aware of the format
in which the data is written. Yes, I suppose that a rigourous markup
language, like XML for instance, could probably divorce data parsing and
storage from the eventual consumers of that data (although validation would
have to be postponed until later).
You could also support some form of input requirement registration,
perhaps an interface for the parser which lets instances of the
computational classes tell the parser what their input requirements
are, or an interface for the computational classes which lets the
parser get the input requirements of the computational classes (or
their instances). (On a tangent, what would you call an "instances of
a computational class" within the context of your program? A
computational object?) With this approach, you define a meta-format
and the classes (or instances) define the input format. As a partial
example, parameters could be given different types. Classes (or
rather, instances) register parameters and their types with the
parser. The type of a parameter determines the parser's behavior when
it encounters it.
If I understand you correctly, that's basically what I did: I have a
parser-class that includes template functions for reading values of
different basic types:

// Read the first parameter from the next line.
template<typename T> bool ReadFirst(T& param, std::string description = "")
{ return NewLine() && ReadNext(param, description); }

// Read the next parameter from the current line.
template<typename T> bool ReadNext(T& param, std::string description = "");

Computational or storage classes use the parser class interface to get the
next value from the file. They only need to tell the parser class whether
to read from the current line or start from a new line. The reason I don't
really like this is that input is spread over about a dozen different
classes, and it is rather hard to follow.
(On a tangent, what would you call an "instances of
a computational class" within the context of your program? A
computational object?)
It can be a computational object, a storage object, or a combination of
both. Some of the class instances spring up from the context prompted by
input, which means that it is not possible to instantiate the entire class
structure before all the input is read.
Am I hitting anywhere near the mark?

Kanenas


Yes, thank you very much for your interest and your advice.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Øystein Western | last post by:
Hello! Try to write som code that will get an input string from the user. Futher more, I'd like to have the program to count all the word the user has written. Startet out like this: ...
32
by: Alan Silver | last post by:
Hello, I shamefully admit to be an old web designer, from before the days of CSS. In those heady days, tables were king and were used for every possible kind of alignment. When CSS came along,...
30
by: Stuart Turner | last post by:
Hi Everyone, I'm working hard trying to get Python 'accepted' in the organisation I work for. I'm making some good in-roads. One chap sent me the text below on his views of Python. I wondered...
3
by: bbcat | last post by:
To practice C language,I began to develop a simple project which implements the management of students' information.I have worked it for one week. I plan to finish it for two weeks. Are there...
2
by: Eddie Stone | last post by:
I've been tasked to produce some system wherby affiliates can display our products on their website. At the moment affiliates are simply mirroring the site and there is no organisation. I said I...
4
by: tony | last post by:
I'm designing a survey form page that will be fairly complex and am becoming confident enough with PHP now to tackle most things. (Thanks to everyone here who has helped) Before I go too far...
0
by: phanipep | last post by:
explain what is a indexed sequential file organisation,also give two advantages of sequential of indexed sequential file organisation ?
0
by: sukatoa | last post by:
I currently developing a simple notepad that could compile A86 codes using java.... Im using Windows XP SP2.... When @ compile, i am satisfied with the result... When i try to invoke the .COM...
1
by: shaunhh | last post by:
Hi All, I am trying to use the user input in my form which is using a stored query. Here is the stored query: SELECT Accounts.balance, Accounts.transactionDate, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.