Data Structure applicable for applying conditions - java

I have a csv file containing 10k row data as shown below.
20131210,0,0,00981231231110,0123,123p1.
20131210,0,0,00981231231120,0123,123p1.
20131210,0,0,00981231231130,0123,123p1.
20131210,0,0,00981231231140,0123,123p1.
20131210,0,0,00981231231150,0123,123p1.
Also i have following xml file as shown below
<validatecondition>
<ID>
00981231231110
</ID>
<SVC_ID>
TMC
</SVC_ID>
<applyrate>
12.0Dollars
</applyrate>
<ID>
00981231231120
</ID>
<applyrate>
2.0Dollars
</applyrate>
.
.
.
.
many conditions
</validatecondition>
node
|-- 00981231231110
| |-- TMC
| | |-- applyrate
| | | |-- 1.00
| | |
| | |
| |
| |
|
+-- 00981231231120
| |-- applyrate
| | |-- 111.00
|
+-- 00981231231130
| |-- RMC
| | |-- applyrate
| | | |-- 11.00
| | |
| | |
| |
|
|
I have apply the above conditions on each line and derive the rates accordingly. Currently the logic iterates sequentially each node and checks whether ID matches value in each line and apply rates. Is there any graph data structure that i can apply to rate quickly?

If you have to look up something the answer is in most cases is a Map. So in your case you have to look up by Id.
So in your case you can create a HashMap<Long, XmlData> (or something like this). Then you iterate over the csv data and look up from the Map using the Id.
This will speed up your alogithm from O(n^2) to O(n) in theory since list lookup is O(n) and you have to do it n times but HashMap's lookup is O(1) (amortized).
If you can't use HashMap for some reason you can try ordering your xml data and use a binary search algorithm. In that case you can end up with O(n log n) which is still better than O(n^2).

Related

Get Predicates with Prefix from an RDF

When extracting subject, property, and object from a RDF file, I want to replace the IRI of the predicate with the keyword it corresponds to. For example, A general SPARQL query returns these results:
| <http://extbi.dk/resource/727> | <http://extbi.dk/p/population> | "21,749"
| <http://extbi.dk/resource/727> | <http://extbi.dk/p/region> | "Central"
| <http://extbi.dk/resource/727> | <http://extbi.dk/p/id> | "727"
What I want to do is: If the prefix keyword for http://extbi.dk/p/ is schema, then my desired result is:
| <http://extbi.dk/resource/727> | <schema:population> | "21,749"
| <http://extbi.dk/resource/727> | <schema:region> | "Central"
| <http://extbi.dk/resource/727> | <schema:id> | "727"
I am using Apache Jena.
Prefixes are handled in Jena using PrefixMapping objects.
This example should return QName or null if it doesnt exists:
Node n;
PrefixMapping prefixes = new PrefixMapping.Factory.create();
qnameFor(n.getURI());
shortForm(String URI) can also be used to simplify an URI based on the "original" URI resource.
Here is the link to the Javadoc : Link.

Is there a quick way to print the Textual representation of file structure?

I'm working on a large project in Eclipse, and I'd like to quickly print out the file structure for various reasons, one of them being that I'd like to ask a question about it here on Stackoverflow.
For example, I'd like to print something like
my_proj
|--src
| |-java
| | |-Hello.java
| | |-Goodbye.java
| |
| |-xml
|--res
| |-html
| |-css
....
etc....
Is there a quick way to generate this sort of representation?

Suggest framework for external rule storage

There is a situation:
I've got 2 .xlsx files:
1. With bussines data
for example:
-----------------------------------------
| Column_A | Column_B| Column_C | Result |
-----------------------------------------
| test | 562.03 | test2 | |
------------------------------------------
2. With bussiness rules
for example:
-------------------------------------------------------------------------
| Column_A | Column_B | Column_C | Result |
-------------------------------------------------------------------------
| EQUALS:test | GREATER:100 | EQUALS:test2 & NOTEQUALS:test | A |
--------------------------------------------------------------------------
| EQUALS:test11 | GREATER:500 | EQUALS:test11 & NOTEQUALS:test | B |
--------------------------------------------------------------------------
With condition in each cell.
One row contains list of these conditions and composes one rule.
All rules will be processed iteratively. But of course, I think, it would be better to construct some 'decision tree' or 'classification flow-chart'.
So, my task is: to store these conditions functionality (methods like EQUALS, GREATER, NOTEQUALS) in some external file or some other resource. To have a possibility to change it without compilation into java bytecode. To have a dynamic solution, not to hard code in java methods.
I found DROOLS http://drools.jboss.org/ as a whay that can work with such cases. But maybe there are another frameworks that can work with such issues?
JavaScript, DynamicSQL, DB solution is not suitable.

Create dynamic classes / objects to be included in a list

I have a xlsx file, that has some tabs with different data. I want to be able to save each row of a tab in a list. The first thing that comes to mind is a list of lists, but I was wondering if there is another way. I'd like to save that information in a object, with all its benefits, but can't think of a way to generate/create such diverse objects on the fly. The data in the xlsx is diverse and ideally the program is agnostic of any content.
So instead of e.g. create a list for each row, than put that list in another list for each tab and each tab in another list, I'd like to store the information that each row represents in a single object and just have a list of different objects.
A small graphic to visualize the problem :
+--------------------------------------------------------------------+
|LIST |
| |
| +------------------+ +------------------+ +-----------------+ |
| | Class1 | | Class2 | | Class3 | |
| |------------------| |------------------| |-----------------| |
| | var1 | | var1 | | var5 | |
| | var2 | | var2 | | var6 | |
|... | var3 | | | | var7 |...|
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| +------------------+ +------------------+ +-----------------+ |
| |
+--------------------------------------------------------------------+
How about a generic class Row which will contain all the information in a row from your file. Then you simply create a list of Rows. Methods for the Row can allow you to get each column.
Without knowing more about the data, you will not be able to write classes to encapsulate it. You could "dynamically" create classes to create new source code. But then the question is, how would you use the new classes?
Well since you want to avoid a "list of lists" kind of solution there would be another way.
This might not be very efficient or fast but I don't have any experience with it, so maybe it isn't too bad. Here's the idea:
For each Row:
Use javassist to create as many fields as needed dynamically that contain each cell's information. Then create an instance of this class and store it in your list of rows. You could also add a field with information about this particular row (e.g. how many fields there are or their names or types or whatever you might need).
The number of fields or methods could also be determined using Reflection.
To get started with javassist there's a tutorial here.
Besides that I don't think there's much to do that does not involve some sort of List<List<SomeType>>

How can i get only the first level of childrens in Hibernate

If I have this three structure in a table,
-A
|
*---B
| |
| *---C
| |
| *---D
| |
| *---E
| |
| *---F
| |
| *---G
*---H
*---I
|
*---J
assuming list() method is called and it returns a colleccion of B and H.
In this scenario I would like hibernate obtain C,D,I and J in a single query.(lazy=false is not working because I dont need E,F and G, just the FIRST LEVEL)
thanks a lot
I don't think there is any way to achieve this. Let others know if you find something

Categories