QuickFIX/J Get fields and groups for customized data dictionary - java

How can I get fields and groups using QuickFIX/J for customized data dictionary?
I receive market data transmitted in customized MarketDataSnapshotFullRefresh (type W) FIX messages. As I understood, I can't use the crack method for this. I'm not quite familiar with Java and QuickFIX/J, but when I use QuickFIX/n and Python, I can define classes for fields and groups like that:
class CustomField(fix.StringField):
tag_number = *SomeTagNumber*
def __init__(self, data=None):
args = (self.tag_number,) if data is None else (self.tag_number, data)
super(CustomField, self).__init__(*args)
def getValue(self, message: object) -> str:
try:
if message.getField(self.tag_number):
return message.getField(self.tag_number)
except fix.FieldNotFound:
return None
else: raise
class CustomGroupField(fix.StringField):
tag_number = *SomeTagNumber*
def __init__(self, data=None):
args = (self.tag_number,) if data is None else (self.tag_number, data)
super(CustomGroupField, self).__init__(*args)
def getValue(self, message: object) -> str:
try:
if message.getField(self.tag_number):
return message.getField(self.tag_number)
except fix.FieldNotFound:
return None
else: raise
class XXXGroup(fix.Group):
def __init__(self):
order = fix.IntArray(4)
order[0] = No_XXX_UMD_Entries.tag_number # This is the NoGroup field
order[1] = XXX_UMD_Entry_ID.tag_number # This is the field in the repeating group
order[2] = CustomGroupField.tag_number
order[3] = 0
# fix.Group.__init__(self, order[0], order[1], order)
args = (order[0], order[1], order)
super(XXXGroup, self).__init__(*args)
def getValue(self, field: object) -> str:
try:
if group.getField(tag_number):
return group.getField(tag_number)
except fix.FieldNotFound:
return None
else: raise
And then I can get value inside the fromApp(self, message, sessionID) method like this:
# Get value of the field
some_custom_field = CustomField().getValue(message)
# Get value in the group
group = XXXGroup()
for idx in range(1, no_entries+1):
message.getGroup(idx,group)
custom_gr_field = group.getValue(CustomGroupField)
How can I achieve the same logic in Java using QuickFIX/J? Or maybe there is a better way to work with a custom data dictionary in Java? Maybe you can refer to some examples?

QuickFIX/J use XML files as Dictionary.
To parse message you should create dictionary and use it. The code looks like:
DataDictionary dataDictionary = new DataDictionary("FIX44.xml");
Message message = new Message();
message.fromString(text, dataDictionary, false);
Standard dictionary stored in quickfixj jars resources. For example, dictionary for FIX 4.4 stores in https://github.com/quickfix-j/quickfixj/blob/master/quickfixj-messages/quickfixj-messages-fix44/src/main/resources/FIX44.xml
For custom dictionary you should create a XML file with all additional fields and groups.
You could copy the standard file, rename it, add group and use like: DataDictionary dataDictionary = new DataDictionary("/var/my_dict/myFIX44.xml");

Related

Simple Code to Copy Same Name Properties?

I have an old question sustained in my mind for a long time. When I was writing code in Spring, there are lots dirty and useless code for DTO, domain objects. For language level, I am hopeless in Java and see some light in Kotlin. Here is my question:
Style 1 It is common for us to write following code (Java, C++, C#, ...)
// annot: AdminPresentation
val override = FieldMetadataOverride()
override.broadleafEnumeration = annot.broadleafEnumeration
override.hideEnumerationIfEmpty = annot.hideEnumerationIfEmpty
override.fieldComponentRenderer = annot.fieldComponentRenderer
Sytle 2 Previous code can be simplified by using T.apply() in Kotlin
override.apply {
broadleafEnumeration = annot.broadleafEnumeration
hideEnumerationIfEmpty = annot.hideEnumerationIfEmpty
fieldComponentRenderer = annot.fieldComponentRenderer
}
Sytle 3 Can such code be even simplified to something like this?
override.copySameNamePropertiesFrom (annot) { // provide property list here
broadleafEnumeration
hideEnumerationIfEmpty
fieldComponentRenderer
}
First Priority Requirments
Provide property name list only one time
The property name is provided as normal code, so as to we can get IDE auto complete feature.
Second Priority Requirements
It's prefer to avoid run-time cost for Style 3. (For example, 'reflection' may be a possible implementation, but it do introduce cost.)
It's prefer to generated code like style1/style2 directly.
Not care
The final syntax of Style 3.
I am a novice for Kotlin language. Is it possible to use Kotlin to define somthing like 'Style 3' ?
It should be pretty simple to write a 5 line helper to do this which even supports copying every matching property or just a selection of properties.
Although it's probably not useful if you're writing Kotlin code and heavily utilising data classes and val (immutable properties). Check it out:
fun <T : Any, R : Any> T.copyPropsFrom(fromObject: R, vararg props: KProperty<*>) {
// only consider mutable properties
val mutableProps = this::class.memberProperties.filterIsInstance<KMutableProperty<*>>()
// if source list is provided use that otherwise use all available properties
val sourceProps = if (props.isEmpty()) fromObject::class.memberProperties else props.toList()
// copy all matching
mutableProps.forEach { targetProp ->
sourceProps.find {
// make sure properties have same name and compatible types
it.name == targetProp.name && targetProp.returnType.isSupertypeOf(it.returnType)
}?.let { matchingProp ->
targetProp.setter.call(this, matchingProp.getter.call(fromObject))
}
}
}
This approach uses reflection, but it uses Kotlin reflection which is very lightweight. I haven't timed anything, but it should run almost at same speed as copying properties by hand.
Now given 2 classes:
data class DataOne(val propA: String, val propB: String)
data class DataTwo(var propA: String = "", var propB: String = "")
You can do the following:
var data2 = DataTwo()
var data1 = DataOne("a", "b")
println("Before")
println(data1)
println(data2)
// this copies all matching properties
data2.copyPropsFrom(data1)
println("After")
println(data1)
println(data2)
data2 = DataTwo()
data1 = DataOne("a", "b")
println("Before")
println(data1)
println(data2)
// this copies only matching properties from the provided list
// with complete refactoring and completion support
data2.copyPropsFrom(data1, DataOne::propA)
println("After")
println(data1)
println(data2)
Output will be:
Before
DataOne(propA=a, propB=b)
DataTwo(propA=, propB=)
After
DataOne(propA=a, propB=b)
DataTwo(propA=a, propB=b)
Before
DataOne(propA=a, propB=b)
DataTwo(propA=, propB=)
After
DataOne(propA=a, propB=b)
DataTwo(propA=a, propB=)

Property file based conditional patterns in java

I have a property file (a.txt) which has the values (Example values given below) like below
test1=10
test2=20
test33=34
test34=35
By reading this file, I need to produce an output like below
value = 35_20_34_10
which means => I have a pattern like test34_test2_test33_test1
Note, If the 'test33' has any value other than 34 then I need to produce the value like below
value = 35_20_10
which means => I have a pattern like test34_test2_test1
Now my problem is, every time when the customer is making the change in the logic, I am making the change in the code. So what I expect is, I want to keep the logic (pattern) in another property file so I will be sending the two inputs to the util (one input is the property file (A.txt) another input will be the 'pattern.txt'),
My util has to be compare the A.txt and the business logic 'pattern.txt' and produce the output like
value = 35_20_34_10 (or)
value = 35_20_10
If there an example for such pattern based logic as I expect?
Any predefined util / java class does this?
Any help would be Great.
thanks,
Harry
First of all, svasa's answer makes a lot of sense, but covers different level of
abstraction. I recommend you read his answer too, that pattern should
be useful.
You may wanna look at Apache Velocity and FreeMarker libraries to see how they structure their API.
Those are template engines - they usually have some abstraction of pattern or format, and abstraction of variable/value binding (or namespace, or source). You can render a template by binding it with a binding/namespace, which yields the result.
For example, you may wanna have a pattern "<a> + <b>", and binding that looks like a map: {a: "1", b: "2"}. By binding that binding to that pattern you'll get "1 + 2", when interpreting <...> as variables.
You basically load the pattern from your pattern.txt, then load your data file A.txt (for example, by treating it as properties and using Properties class) and construct binding based on these properties. You'll get your output and possibility to customize the pattern all the time.
You may call the sequences like test34_test2_test33_test1 as a pattern, let me call them as constraints when building something.
To me this problem best fits into a
builder pattern.
When building the value you want, you tell the builder that these are my constraints(pattern) and these are my original properties like below:
new MyPropertiesBuilder().setConstraints(constraints).setProperties(original).buildValue();
Details:
Set some constraints in a separate file where you specify the order of the properties and their values like :
test34=desiredvalue-could-be-empty
test2=desiredvalue-could-be-empty
test33=34
test1=desiredvalue-could-be-empty
The builder goes over the constraints in the order specified, but get the values from the original properties and build the desired string.
One way to achieve your requirement through builder pattern is to define classes like below :
Interface:
public interface IMyPropertiesBuilder
{
public void setConstraints( Properties properties );
public void setProperties( Properties properties );
public String buildValue();
}
Builder
public class MyPropertiesBuilder implements IMyPropertiesBuilder
{
private Properties constraints;
private Properties original;
#Override
public void setConstraints( Properties constraints )
{
this.constraints = constraints;
}
#Override
public String buildValue()
{
StringBuilder value = new StringBuilder();
Iterator it = constraints.keySet().iterator();
while ( it.hasNext() )
{
String key = (String) it.next();
if ( original.containsKey( key ) && constraints.getProperty( key ) != null && original.getProperty( key ).equals( constraints.getProperty( key ) ) )
{
value.append( original.getProperty( key ) );
value.append( "_" );
}
}
return value.toString();
}
#Override
public void setProperties( Properties properties )
{
this.original = properties;
}
}
User
public class MyPropertiesBuilderUser
{
private Properties original = new Properties().load(new FileInputStream("original.properties"));;
private Properties constraints = new Properties().load(new FileInputStream("constraints.properties"));
public String getValue()
{
String value = new MyPropertiesBuilder().setConstraints(constraints).setProperties(original).buildValue();
}
}

How to pass a dictionary when calling a java program as subprocess in python?

I am trying to call a java jar program using python which works fine when i pass the input arguments as string, Now i need to pass dictionary as a input argument.
Here is the code:
Python:
class ExecuteKTR():
def GET(self,r):
web.header('Access-Control-Allow-Origin','*')
web.header('Access-Control-Allow-Credentials', 'true')
secToken = web.input().SecurityToken
Domain = web.input().Domain
fieldnames = ast.literal_eval(web.input().fieldnames)
authResult = Auth.GetSession(secToken,Domain)
if authResult.reason == "OK":
args = ['ktrjob.jar', 'arg1', 'arg2', 'argN'] # Any number of args to be passed to the jar file
result = jarWrapper(*args)
print result
elif authResult.reason == 'Unauthorized':
result = comm.format_response(False,authResult.reason,"Check the custom message",exception=None)
return result
def jarWrapper(*args):
process = Popen(['java', '-jar']+list(args), stdout=PIPE, stderr=PIPE)
ret = []
while process.poll() is None:
line = process.stdout.readline()
print line
if line != '' and line.endswith('\n'):
ret.append(line[:-1])
stdout, stderr = process.communicate()
ret += stdout.split('\n')
if stderr != '':
ret += stderr.split('\n')
ret.remove('')
return ret
Java:
String file = "";
if(args.length != 0)
{
file = args[0];
for ( int i=0;i<=args.length;i++)
{
System.out.print(args[i]);
}
}
I need to pass a dictionary as a parameter to the java application. Sample one is,
{DateTime:12/03/2016,DealerName:'Test'}
Java only allows an Array of Strings as input parameter for a program, as the signature of the main method in Java shows:
public class MainClass {
public static void main(final String[] args)
{
//do stuff
}
}
You need to translate your dictionary in to one (or multiple) strings for the input for your java program and decode the transfered string inside your Java program. One solution would be to write your dictionary as a JSON String and parse a JSON Object from the provided String inside your Java program.
To create the JSON String in python you can use
strJSON = json.dumps(myDict)
In Java there are several libraries to import from Json format, have a look here.
You can use JSON to pass the dictionary as a String since in Python dictionary and JSON are basically the same
Pass a dictionary to a program is rather meaningless: all you can pass to a subprocess are strings.
So you will have to choose a convention. If you need to be able to pass arbitrary objects, JSON is an option since libraries exist in both languages. If the dictionary is very simple and has always same structure, you could also pass only the variable parts. But beware, JSON do not specify any date format, so you will have to choose your own, and be consistent between Python and Java side.

Writing and Reading Repeated Extended Fields in Protobuf

I'm trying to use extensions with Google's Protocol Buffers. I've got one "main" proto file and several other proto files that "extend" the main one.
In my java code, I'm not 100% sure how to add to a repeating message correctly. When I run the java code I've written, the toString() method shows that the extension attributes added, but it doesn't decode properly (probably because I ran a build() call on the added Collar object).
How should I properly add repeating elements to extended items in a proto file?
File1.proto
package protocol_buffer;
option java_outer_classname = "PetClass";
message Pet {
optional string pet_name = 1;
extensions 100 to 199;
}
File2.proto
import "File1.proto";
option java_outer_classname = "CollarClass";
message Collars {
optional string collar_type = 1;
optional string collar_color = 2;
}
extend pet {
repeated Collars collar = 100;
}
MyFile.java
Pet pet = Pet.newBuilder()
.setPetName("Fido")
.addExtension(CollarClass.collar,
Collar.newBuilder()
.setCollarType("round")
.setCollarColor("brown")
.build()
)
.build();
System.out.println(pet.toString());
I figured out my problem. I was correctly adding the extension to "Pet". When parsing a protobuf byte array, you need to add an extension registry so that the function knows what extensions to parse.
ExtensionRegistry registry = ExtensionRegistry.newInstance();
registry.add(CollarClass.collar);
Pet pet = Pet.parseFrom(new FileInputStream(<some file>),registry);

using eval in groovy

How can I use eval in groovy to evaluate the following String:
{key1=keyval, key2=[listitem1, listitem2], key3=keyval2}
All the list items and keyval is a String.
doing Eval.me("{key1=keyval, key2=[listitem1, listitem2], key3=keyval2}") is giving me the following error:
Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
solution: Add an explicit closure parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...} at
I want to get HashMap
Is there no way you can get the data in JSON format? Then you could just use one of the parsers mentioned here.
You can parse that string by translating some of the characters, and writing your own binding to return variable names when Groovy tries to look them up, like so:
class Evaluator extends Binding {
def parse( s ) {
GroovyShell shell = new GroovyShell( this );
shell.evaluate( s )
}
Object getVariable( String name ) { name }
}
def inStr = '{key1=keyval, key2=[listitem1, listitem2], key3=keyval2}'
def inObj = new Evaluator().parse( inStr.tr( '{}=', '[]:' ) )
println inObj
But this is a very brittle solution, and getting the data in a friendlier format (as suggested by #Stefan) is definitely the best way to go...

Categories