Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any suggestions / Comments on this Problem

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
Afternoon.
I have to write an aplication to view files created using a certain medical transfer protocol.

Question 1.

Looking at the specs, this protocol has 27 different data types. These 27 types are effectively variants on Byte, Float and char. Some of them are pretty basic like a 32 bit int (can treat as a 4 byte number), and string, but others have certain constraints attached (they might insist on only using certain characters, or a maximum number range)

Would I be better defining these as 27 structs, and writing some supporting code to ensure that each is validated as it is assigned to the struct?, or creating 27 classes, allowing each class to ensure that as part of it's constructor it can except if the data type is not correct ?

Or would I be better off again storing them internally in a language type (probably C++), and just write 27 validation functions, and 27 conversion functions to make them look correct for display ?

My last thought for doing it would be to make a template class, that will validate the type to be correct, and return a variable of the correct type, but as there are different rules for all the variations, I suspect that a template won't really save me a great deal of work.

Any ideas / comments most welcome here, as I want to make sure I start on this design only once !

Cheers,

K
 
... as I want to make sure I start on this design only once !

Don't. Do not be afraid to alter your design if you need it. Off course it does not hurt to think before you do something!

The way to model it depends on what you need to do with it. It you have some kind of byte stream that you have to parse and display only once, it may be shortest and clearest to have only one switch statement in the parser that calls one the 27 display / conversion routines.

However, if you need to do more with the data (export it, for example), you will want a "Variable" or "Quantity" or whatever superclass. I'm not into C++, but I guess that this is the template class you are referring to.

It will still be a lot of work, but that is part of the specs. The only savings are in "steering through the code". You will have to code all conversions / displays somehow. But if you can select the correct subclass once (upon creation of an object), then all selections are made beforehand. If you use some sort of general class, you will have to make the selection for each task that needs to be done within the object.

Best regards
 
Kalisto,

Firstly, I think your problem is a classic case for Class design. (Constructors, destrctors, operator & function overloading, etc).

Secondly, where is Question 2? ;-P

good luck!

In the sweat of thy brow shall you eat your bread.
-Bible

 
I think it fell down the back of the armchair with the rest of my design :-/

Currently re-thinking it all, as apart from the 27 'types', There is a dictionary of about a thousand elements, each of which is one of the 27 types, but represents different data. As any file can have from 1 .. all of these different dictionary elements, and one of my windows is going to be an info which details all you need to know about this element, I need to either look at storing all my elements in memory as well, or having them arranged in a file system for easy (fast) access.

Ho Hum, back to the drawing board,

K
 
Hi Kalisto,

I was wondering if the modelling of the constraints that decorate the different types is a good solution for your problem.

I must know more about the constraints that come with the 27 data types, but it may possible to make a classification of those constrained data types. Most of your data types seem to be numerics (byte, float, char, etc.), we can try to write a first taxonomy in Eiffel syntax :

Code:
-deferred class CONSTRAINED_NUMERIC[G]
    -- Declare operator +, -, =, assignement, etc.
 end

-class LOW_BOUNDED_NUMERIC[G] inherit
         CONSTRAINED_NUMERIC[G]
 feature
      low_bound: G is do end
      set_low_bound (new_low_bound: G) is
        do
        ensure
               low_bound = new_low_bound
        end
 end


-class HIGH_BOUNDED_NUMERIC[G] inherit
         CONSTRAINED_NUMERIC[G]
 feature
      high_bound: G is do end
      set_high_bound (new_high_bound: G) is
        do
        ensure
               high_bound = new_high_bound
        end
 end


-class RANGE_NUMERIC[G] inherit
         LOW_BOUNDED_NUMERIC[G]
            redefine set_low_bound end
         HIGH_BOUNDED_NUMERIC[G]
            redefine set_high_bound end
 feature
      set_low_bound (new_low_bound: G) is
        require
               new_low_bound <= high_bound
        do
        end

      set_high_bound (new_high_bound: G) is
        require
               new_high_bound >= low_bound
        do
        end
 invariant
      low_bound <= high_bound
 end


-class MULTIPLE_RANGE_NUMERIC[G] inherit
         RANGE_NUMERIC[G]
 feature
        add_range (new_range: RANGE_NUMERIC[G]) is
           require
                  does_not_overlap (new_range)
           do
                  ranges.put (new_range)
           ensure
                  ranges.count = old ranges.count + 1
           end

         does_not_overlap (range: RANGE_NUMERIC[G]: BOOLEAN is
            do
                   -- Check that there are no overlappings
            ensure
                   ranges.is_empty => Result
            end
 feature {None} --secret feature
        ranges : LIST[RANGE_NUMERIC[G]]
 invariant
        -- no overlapping range
 end

The generic parameter is the actual type of the numeric, REAL, INTEGER, CHARACTER, etc.

Each class that inherits from the generic class CONSTRAINED_NUMERIC[G] must redifine operators +, -, etc. and specify the right assertions.

From this you should be able to specify clearly the constraints of your data types.

--
Globos
 
You are right in that the majority of the 27 types conform to the more standard types (int, float, long string, short string, etc) but some have different validation methods (an int has to be 16 bit, unsigned. A Name has to contain only letters in the language that the file is specified in, and also allow for .-^ )

I see what you are proposing is a kind of Object Hierarchy, with maybe the top level being abstract.
It then becomes a code problem, as if I know that my type is Foo (from reading in the type of the data from the stream), I have to have a large if then, or case construct to create the appropriate object and return it so that it can be filled with data.

My Present solution is more crude, hence the question in here. At present I have a list of the 27 types read in from a file, and when I know what the type foo is from the stream, I do a search on the list for a match with the name, (The list is made of name / function pointer pairs). I can then call a validation function. But this means that my code is storing just arrays of valid data, along with a marker as to how they should be interpreted (float, bool etc).

I think I need to sit down and rethink the whole design again (revision 3 now)
 
>an int has to be 16 bit, unsigned
To make a data type with this constraint, you can use a RANGE_NUMERIC[INTEGER], instantiated with bounds (0, 65535).

>A Name has to contain only letters in the language [...]
We can define a class CONSTRAINED_STRING and instantiate it with an array of char (or simply a string) standing for the langage to use.


I didn't understand clearly your next problem. What I understand is that the types of the file's data are defined in the header part of that file.
What is your validation function about? What has to be validated? The type read from the stream(I don't see what is the validation of a type in that case), or/and that a data satisfy the constraints defined by its type?
After reading the stream, what has to be done with the data (computation, printing, display, etc.)?

If it's possible, can you give an example of input file you have?

Sorry, it's me that ask questions now!

--
Globos
 
Sorry, its the data that has to be validated, we have a problem and suspect that it is due to some software not working quite to the spec.

the data is to be displayed (just so we can check by eye that it is correct for now). I am just thinking of the future, we have our own software in house that would benefit from being able to talk in the same protocol, so I am trying to allow for output as well.

I can't really show you the file due to data protection act in the uk. But the data is of the form [type][length (only used for strings)][data][type][length (only used for strings)][data]....

To complicate matters slightly, the data can actually be a nested set of [tag][len][data] groups as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top