473,396 Members | 1,900 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,396 software developers and data experts.

Q: implementing expression logic - hard coded or dynamic

Hi all,

I have a file consisting fixed width records from which I need to
extract only those lines meeting certain conditions.
These conditions do change and I find myself recoding/compiling for each
set of conditions then running again.

e.g.

scenario 1:
extract lines where field1=100023 and field3 = "PP"

scenario 2:
extract lines where field1 in (1234,2335,12123,213213)

scenario 3:
extract lines where field3 in (PP,OP,TD,WL) and field4 =04 OR (field3 =
PA and field5 = 03)
As you see it can easily be a non-trivial expression that determines
which lines need to be extracted - this is making it very difficult to
code for every eventuality. It's very SQL-esque but it's not possible to
use SQL in this case - the prog reads and processes each line via a stream.
I was thinking about creating an expression builder, allowing the user
to define the expression with any nesting etc and incorporating this
into the code in some dynamic way at runtime.

I cannot seem to work out how i might do this, though.

Anyone have an inkling on how this might best be done?

Grateful for your thoughts

Regards
Jon
May 31 '08 #1
10 2753
Jonathan <bl***@nowhere.comwrote:

<snip>
I was thinking about creating an expression builder, allowing the user
to define the expression with any nesting etc and incorporating this
into the code in some dynamic way at runtime.

I cannot seem to work out how i might do this, though.

Anyone have an inkling on how this might best be done?
If this is an internal tool and you can trust the input (not
necessarily to be correct, but to not do anything insecure etc) you
could put C# code snippets in your config file and then use
CSharpCodeProvider to build the snippets into types with appropriate
context.

LINQ to Objects makes it very easy to express this sort of logic in C#.

(Of course, the latter relies on you running .NET 3.5 so as to compile
C# 3 code at execution time...)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
May 31 '08 #2
There are expression parsers available. As it happens, I'm working on
just such at the moment, but it isn't a postable state yet...
However, if .NET 3.5 is an option, there is an example (in the samples
download) of a dynamic LINQ expression parser that might be suitable.

Marc
May 31 '08 #3
For info, I found a link to the LINQ expression parser on ScottGu's
Blog:
http://weblogs.asp.net/scottgu/archi...y-library.aspx

Marc
May 31 '08 #4
Jon Skeet [C# MVP] wrote:
Jonathan <bl***@nowhere.comwrote:

<snip>
>I was thinking about creating an expression builder, allowing the user
to define the expression with any nesting etc and incorporating this
into the code in some dynamic way at runtime.

I cannot seem to work out how i might do this, though.

Anyone have an inkling on how this might best be done?

If this is an internal tool and you can trust the input (not
necessarily to be correct, but to not do anything insecure etc) you
could put C# code snippets in your config file and then use
CSharpCodeProvider to build the snippets into types with appropriate
context.
Using CSharpCodeProvider takes a little care, though. If you only compile
the code once at startup, it should be no problem, but if you're going to be
recompiling it during runtime you should be aware that loaded assemblies
cannot be unloaded -- they'll sit there and take up memory. You can overcome
this by loading them into a separate AppDomain and unloading this when
you're done, but then you have to deal with marshalling issues. In addition,
compiling code to assemblies and loading these is atrociously slow compared
to using a custom parser, so some form of caching is usually needed to
prevent unnecessary recompiles.

In short, if you need to compile more than once, this solution is not as
convenient as it sounds.
LINQ to Objects makes it very easy to express this sort of logic in C#.
The OP's problem is practically what LINQ was invented for, so I'd certainly
look into it first. Upgrading to .NET 3.5 is a relatively painless affair,
especially if it's a new project.
--
J.
http://symbolsprose.blogspot.com
May 31 '08 #5
Jon Skeet [C# MVP] wrote:
Jonathan <bl***@nowhere.comwrote:

<snip>
>I was thinking about creating an expression builder, allowing the user
to define the expression with any nesting etc and incorporating this
into the code in some dynamic way at runtime.

I cannot seem to work out how i might do this, though.

Anyone have an inkling on how this might best be done?

If this is an internal tool and you can trust the input (not
necessarily to be correct, but to not do anything insecure etc) you
could put C# code snippets in your config file and then use
CSharpCodeProvider to build the snippets into types with appropriate
context.

LINQ to Objects makes it very easy to express this sort of logic in C#.

(Of course, the latter relies on you running .NET 3.5 so as to compile
C# 3 code at execution time...)

Jon,

Much obliged for your thoughts and direction. I've had a quick foray
into the world of dynamic code generation in C# and it looks both
interesting and promising.

If my understanding is correct the following will be true/possible:

o I would need to create a stand-alone utility from generated code.
o I could not "inline" the created code within the current utility
ergo, I would, in effect, call-out to this having compiled it
o I would be able to create complex conditional filters using a simple
library of code snippets and appropriate glueing.
o I will be able to use .NET 2.0
I also had a look at Linq and, whilst it looks like a very useful tool,
it appears to require in-memory data sets. In this case that wouldn't be
practical since the files I am processing contain c. 5 million lines at
c. 1GB and probably wouldn't squeeze into working memory. Correct me if
I have made the wrong assumption!

I have perhaps 3 weeks' experience with C# (although to mitigate I do
have a comp. sci background.) so my attempt to bring this to fruition
will be peppered with a large sprinkling of optimism!

Kind regards
Jonathan
May 31 '08 #6
Marc Gravell wrote:
For info, I found a link to the LINQ expression parser on ScottGu's
Blog:
http://weblogs.asp.net/scottgu/archi...y-library.aspx

Marc
Marc,

Much obliged for your input.

I took a quick look at Linq following your and Jon's first posts but,
having seen what Scott has to say in his blog, I will spend a little
more time exploring this.

Regarding the data set I am using, it is semi-structured - each line
contains a message which has a predefined structure according to its
content type. Each line, however, may have a different content type and,
as such, doesn't follow the pattern expected of a single set of data.

I initially discounted Linq on the understanding that it could be
applied only to in-memory structured data sets but that may have been
the wrong conclusion on my part.

Thanks again, I have much more to look at now and don't feel at such a
dead end.

Regards
Jonathan
May 31 '08 #7
Jeroen Mostert wrote:
Jon Skeet [C# MVP] wrote:
>Jonathan <bl***@nowhere.comwrote:

<snip>
>>I was thinking about creating an expression builder, allowing the
user to define the expression with any nesting etc and incorporating
this into the code in some dynamic way at runtime.

I cannot seem to work out how i might do this, though.

Anyone have an inkling on how this might best be done?

If this is an internal tool and you can trust the input (not
necessarily to be correct, but to not do anything insecure etc) you
could put C# code snippets in your config file and then use
CSharpCodeProvider to build the snippets into types with appropriate
context.
Using CSharpCodeProvider takes a little care, though. If you only
compile the code once at startup, it should be no problem, but if you're
going to be recompiling it during runtime you should be aware that
loaded assemblies cannot be unloaded -- they'll sit there and take up
memory. You can overcome this by loading them into a separate AppDomain
and unloading this when you're done, but then you have to deal with
marshalling issues. In addition, compiling code to assemblies and
loading these is atrociously slow compared to using a custom parser, so
some form of caching is usually needed to prevent unnecessary recompiles.

In short, if you need to compile more than once, this solution is not as
convenient as it sounds.
>LINQ to Objects makes it very easy to express this sort of logic in C#.
The OP's problem is practically what LINQ was invented for, so I'd
certainly look into it first. Upgrading to .NET 3.5 is a relatively
painless affair, especially if it's a new project.

Jeroen,

Thanks for your insight into this area. I had already posted a reply to
Jon summarising what I had understood about it. My thoughts appear to be
a little naive in hindsight.

I will take a more in-depth look at both areas of functionality and work
out how I might apply one or both of them.

Many thanks again - it really helps to get personal input rather than
struggling alone with what are invariably terse tomes!

Regards,
Jonathan
May 31 '08 #8
Jonathan <bl***@nowhere.comwrote:
Much obliged for your thoughts and direction. I've had a quick foray
into the world of dynamic code generation in C# and it looks both
interesting and promising.

If my understanding is correct the following will be true/possible:

o I would need to create a stand-alone utility from generated code.
No, I don't think so.
o I could not "inline" the created code within the current utility
ergo, I would, in effect, call-out to this having compiled it
Nah. Compile to an in-memory assembly and you're away.

As an example, see my "snippet compiler" (Snippy) which you can
download (including source code) from
http://csharpindepth.com/Downloads.aspx

It lets you type in some code, then compiles and executes it
immediately (when you press a button).
o I would be able to create complex conditional filters using a simple
library of code snippets and appropriate glueing.
Yes - or use LINQ :)
o I will be able to use .NET 2.0
So long as you don't need C# 3 features (such as LINQ) absolutely.
I also had a look at Linq and, whilst it looks like a very useful tool,
it appears to require in-memory data sets. In this case that wouldn't be
practical since the files I am processing contain c. 5 million lines at
c. 1GB and probably wouldn't squeeze into working memory. Correct me if
I have made the wrong assumption!
Definitely. It's one of the common misunderstandings about LINQ to
Objects - the processing happens in memory, but unless you invoke any
operators which require the whole data sequence (e.g. ordering) you can
process the data as a stream. See

http://msmvps.com/blogs/jon.skeet/ar...nq-to-objects-
not-just-for-in-memory-collections.aspx

for an example of what I mean (which may be quite like your situation).
I have perhaps 3 weeks' experience with C# (although to mitigate I do
have a comp. sci background.) so my attempt to bring this to fruition
will be peppered with a large sprinkling of optimism!
Best of luck - and we're ready to help :)

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
May 31 '08 #9
Jon Skeet [C# MVP] wrote:
Jonathan <bl***@nowhere.comwrote:
>Much obliged for your thoughts and direction. I've had a quick foray
into the world of dynamic code generation in C# and it looks both
interesting and promising.

If my understanding is correct the following will be true/possible:

o I would need to create a stand-alone utility from generated code.

No, I don't think so.
>o I could not "inline" the created code within the current utility
ergo, I would, in effect, call-out to this having compiled it

Nah. Compile to an in-memory assembly and you're away.

As an example, see my "snippet compiler" (Snippy) which you can
download (including source code) from
http://csharpindepth.com/Downloads.aspx

It lets you type in some code, then compiles and executes it
immediately (when you press a button).
>o I would be able to create complex conditional filters using a simple
library of code snippets and appropriate glueing.

Yes - or use LINQ :)
>o I will be able to use .NET 2.0

So long as you don't need C# 3 features (such as LINQ) absolutely.
>I also had a look at Linq and, whilst it looks like a very useful tool,
it appears to require in-memory data sets. In this case that wouldn't be
practical since the files I am processing contain c. 5 million lines at
c. 1GB and probably wouldn't squeeze into working memory. Correct me if
I have made the wrong assumption!

Definitely. It's one of the common misunderstandings about LINQ to
Objects - the processing happens in memory, but unless you invoke any
operators which require the whole data sequence (e.g. ordering) you can
process the data as a stream. See

http://msmvps.com/blogs/jon.skeet/ar...nq-to-objects-
not-just-for-in-memory-collections.aspx

for an example of what I mean (which may be quite like your situation).
>I have perhaps 3 weeks' experience with C# (although to mitigate I do
have a comp. sci background.) so my attempt to bring this to fruition
will be peppered with a large sprinkling of optimism!

Best of luck - and we're ready to help :)
Jon,

Many thanks again.

All good news as far as I'm concerned - each method looks to be quite
versatile and able to meet my needs.

So, in a masochistic yet familiar manner, I shall be attempting both then.

Optimism abounds eh?
Regards
Jonathan
Jun 1 '08 #10
On Jun 1, 9:47 pm, Jonathan <bl...@nowhere.comwrote:

<snip>
All good news as far as I'm concerned - each method looks to be quite
versatile and able to meet my needs.

So, in a masochistic yet familiar manner, I shall be attempting both then.
:)
Optimism abounds eh?
All sounds good to me. Let us know how you get on - good luck!

Jon
Jun 2 '08 #11

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

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
0
by: raca | last post by:
I am trying to create a generic SOA ServiceInvoker that will accept an XML string that will be used to deserialize an object generated by XSDObjectGen. The hierarchy goes like this:...
7
by: dixie | last post by:
I have been running some code from a form's on open event to run regedit if a registry key does not exist in the registry. It has been working fine, but I have had the path to the registry fix...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
0
by: tjonsek | last post by:
I am working with directories in PHP for the first time. I have code that I've changed multiple times to try different things. I would think this is pretty standard fare so I'm not sure why I can't...
4
by: Basilisk96 | last post by:
This topic is difficult to describe in one subject sentence... Has anyone come across the application of the simple statement "if (object1's attributes meet some conditions) then (set object2's...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
10
by: Jonathan | last post by:
Hi all, I have a file consisting fixed width records from which I need to extract only those lines meeting certain conditions. These conditions do change and I find myself recoding/compiling...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.