crosstab and crossDataSet - java

I'm trying to create a crossTab with three lines and n columns. I used a CrosstabDataset and JRBeanCollectionDataSource to show my Data. My problem is I can only show the last object in my CollectionDataSource I don't have an access to the data in my crossDataSet.
NB:
I used JRDesignCrosstab (java Code) to create the crossTab.
public static JRDesignCrosstab CrosstabPanel(String parameterName , JasperDesign jasperDesign, JRDesignDataset subDataset) throws JRException {
// parameter
JRDesignParameter parameter = new JRDesignParameter();
parameter.setName(parameterName);
parameter.setValueClass(java.lang.Object.class);
jasperDesign.addParameter(parameter);
subDataset.addParameter(parameter);
//Gross Tab
JRDesignCrosstab crosstab = new JRDesignCrosstab();
crosstab.setX(-90);
crosstab.setY(-4);
crosstab.setWidth(600);
crosstab.setHeight(400);
//Expression :
JRDesignExpression expression = new JRDesignExpression("$P{"+parameterName+"}");
//CrosstabDataset
JRDesignCrosstabDataset dataSet = new JRDesignCrosstabDataset();
//datasetrun
JRDesignDatasetRun dsr = new JRDesignDatasetRun();
dsr.setDatasetName(subDataset.getName());
dsr.setDataSourceExpression(expression);
//datasetrun into CrosstabDataset
dataSet.setResetType(ResetTypeEnum.NONE);
dataSet.setDatasetRun(dsr);
crosstab.setDataset(dataSet);
//Bucket Row
JRDesignCrosstabBucket bucket = new JRDesignCrosstabBucket();
JRDesignExpression expressionField = new JRDesignExpression();
expressionField.setText("$F{commissionSimPaye}");
bucket.setValueClassName("net.sf.jasperreports.engine.DataSource");
bucket.setExpression(expressionField);
//Row Group;
JRDesignCrosstabRowGroup rowGroup = new JRDesignCrosstabRowGroup();
rowGroup.setName("rowGroup");
rowGroup.setBucket(bucket);
rowGroup.setWidth(68*2+1);
rowGroup.setTotalPosition(CrosstabTotalPositionEnum.END);
crosstab.addRowGroup(rowGroup);
//Bucket Second Row
bucket = new JRDesignCrosstabBucket();
expressionField = new JRDesignExpression();
expressionField.setText("$F{commissionSimPaye}");
bucket.setValueClassName("net.sf.jasperreports.engine.ReportContext");
bucket.setExpression(expressionField);
//Row Group;
rowGroup = new JRDesignCrosstabRowGroup();
rowGroup.setName("secondRowGroup");
rowGroup.setBucket(bucket);
rowGroup.setWidth(68*2+1);
rowGroup.setTotalPosition(CrosstabTotalPositionEnum.END);
crosstab.addRowGroup(rowGroup);
//Bucket Column
bucket = new JRDesignCrosstabBucket();
expressionField = new JRDesignExpression();
expressionField.setText("$F{commissionSimCalcule}");
bucket.setValueClassName("java.lang.Object");
bucket.setExpression(expressionField);
//ColumnGroup
JRDesignCrosstabColumnGroup ColumnGroup = new JRDesignCrosstabColumnGroup();
ColumnGroup.setName("columnGroup");
ColumnGroup.setBucket(bucket);
ColumnGroup.setHeight(60);
ColumnGroup.setTotalPosition(CrosstabTotalPositionEnum.END);
crosstab.addColumnGroup(ColumnGroup);
JRDesignExpression expressionMesaure = new JRDesignExpression();
expressionMesaure.setText("$F{commissionSimCalcule}");
JRDesignCrosstabMeasure measure = new JRDesignCrosstabMeasure();
measure.setName("ColumContent"+0);
measure.setValueExpression(expressionMesaure);
measure.setValueClassName("java.lang.Object");
crosstab.addMeasure(measure);
expressionMesaure = new JRDesignExpression();
expressionMesaure.setText("$F{commissionSimPaye}");
measure = new JRDesignCrosstabMeasure();
measure.setName("ColumContent"+1);
measure.setValueExpression(expressionMesaure);
measure.setValueClassName("java.lang.Object");
crosstab.addMeasure(measure);
expressionMesaure = new JRDesignExpression();
expressionMesaure.setText("$F{commissionSimAPaye}");
measure = new JRDesignCrosstabMeasure();
measure.setName("ColumContent"+2);
measure.setValueExpression(expressionMesaure);
measure.setValueClassName("java.lang.Object");
crosstab.addMeasure(measure);
//contenu de la cellule
JRDesignTextField textField = new JRDesignTextField();
JRDesignCrosstabCell cell = new JRDesignCrosstabCell();
JRDesignExpression expressionTextField = new JRDesignExpression();
JRDesignCellContents cellContents = new JRDesignCellContents();
textField.setX(0);
textField.setY(0);
textField.setWidth(68);
textField.setHeight(20);
textField.setHorizontalAlignment(HorizontalAlignEnum.RIGHT);
textField.getLineBox().getLeftPen().setLineWidth(1);
textField.getLineBox().getTopPen().setLineWidth(1);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().getBottomPen().setLineWidth(1);
cell.setHeight(20);
cell.setWidth(68);
expressionTextField.setText("$V{ColumContent"+0+"}");
textField.setExpression(expressionTextField);
cellContents.addElement(textField);
cell.setContents(cellContents);
crosstab.addCell(cell);
return crosstab;
}

problem solved, I worked on the structure of JRBeanCollectionDataSource

Related

Getting latest data from AWS custom Cloudwatch in Java

I have a custom metric in AWS cloudwatch and i am putting data into it through AWS java API.
for(int i =0;i<collection.size();i++){
String[] cell = collection.get(i).split("\\|\\|");
List<Dimension> dimensions = new ArrayList<>();
dimensions.add(new Dimension().withName(dimension[0]).withValue(cell[0]));
dimensions.add(new Dimension().withName(dimension[1]).withValue(cell[1]));
MetricDatum datum = new MetricDatum().withMetricName(metricName)
.withUnit(StandardUnit.None)
.withValue(Double.valueOf(cell[2]))
.withDimensions(dimensions);
PutMetricDataRequest request = new PutMetricDataRequest().withNamespace(namespace+"_"+cell[3]).withMetricData(datum);
String response = String.valueOf(cw.putMetricData(request));
GetMetricDataRequest res = new GetMetricDataRequest().withMetricDataQueries();
//cw.getMetricData();
com.amazonaws.services.cloudwatch.model.Metric m = new com.amazonaws.services.cloudwatch.model.Metric();
m.setMetricName(metricName);
m.setDimensions(dimensions);
m.setNamespace(namespace);
MetricStat ms = new MetricStat().withMetric(m);
MetricDataQuery metricDataQuery = new MetricDataQuery();
metricDataQuery.withMetricStat(ms);
metricDataQuery.withId("m1");
List<MetricDataQuery> mqList = new ArrayList<MetricDataQuery>();
mqList.add(metricDataQuery);
res.withMetricDataQueries(mqList);
GetMetricDataResult result1= cw.getMetricData(res);
}
Now i want to be able to fetch the latest data entered for a particular namespace, metric name and dimention combination through Java API. I am not able to find appropriate documenation from AWS regarding the same. Can anyone please help me?
I got the results from cloudwatch by the below code.\
GetMetricDataRequest getMetricDataRequest = new GetMetricDataRequest().withMetricDataQueries();
Integer integer = new Integer(300);
Iterator<Map.Entry<String, String>> entries = dimensions.entrySet().iterator();
List<Dimension> dList = new ArrayList<Dimension>();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
dList.add(new Dimension().withName(entry.getKey()).withValue(entry.getValue()));
}
com.amazonaws.services.cloudwatch.model.Metric metric = new com.amazonaws.services.cloudwatch.model.Metric();
metric.setNamespace(namespace);
metric.setMetricName(metricName);
metric.setDimensions(dList);
MetricStat ms = new MetricStat().withMetric(metric)
.withPeriod(integer)
.withUnit(StandardUnit.None)
.withStat("Average");
MetricDataQuery metricDataQuery = new MetricDataQuery().withMetricStat(ms)
.withId("m1");
List<MetricDataQuery> mqList = new ArrayList<>();
mqList.add(metricDataQuery);
getMetricDataRequest.withMetricDataQueries(mqList);
long timestamp = 1536962700000L;
long timestampEnd = 1536963000000L;
Date d = new Date(timestamp );
Date dEnd = new Date(timestampEnd );
getMetricDataRequest.withStartTime(d);
getMetricDataRequest.withEndTime(dEnd);
GetMetricDataResult result1= cw.getMetricData(getMetricDataRequest);

Custom DataProvider Nattable

I create nattable the following way. But I can get access to the cells only through getters and setters in my Student class. How else can I access cells? Should I create my own BodyDataProvider or use IDataProvider? If it is true, could someone give some examples of implementing such providers?
final ColumnGroupModel columnGroupModel = new ColumnGroupModel();
ColumnHeaderLayer columnHeaderLayer;
String[] propertyNames = { "name", "groupNumber", "examName", "examMark" };
Map<String, String> propertyToLabelMap = new HashMap<String, String>();
propertyToLabelMap.put("name", "Full Name");
propertyToLabelMap.put("groupNumber", "Group");
propertyToLabelMap.put("examName", "Name");
propertyToLabelMap.put("examMark", "Mark");
DefaultBodyDataProvider<Student> bodyDataProvider = new DefaultBodyDataProvider<Student>(students,
propertyNames);
ColumnGroupBodyLayerStack bodyLayer = new ColumnGroupBodyLayerStack(new DataLayer(bodyDataProvider),
columnGroupModel);
DefaultColumnHeaderDataProvider defaultColumnHeaderDataProvider = new DefaultColumnHeaderDataProvider(
propertyNames, propertyToLabelMap);
DefaultColumnHeaderDataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(
defaultColumnHeaderDataProvider);
columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(columnHeaderLayer,
bodyLayer.getSelectionLayer(), columnGroupModel);
columnGroupHeaderLayer.addColumnsIndexesToGroup("Exams", 2, 3);
columnGroupHeaderLayer.setGroupUnbreakable(2);
final DefaultRowHeaderDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider);
DefaultRowHeaderDataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider);
ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, bodyLayer, bodyLayer.getSelectionLayer());
final DefaultCornerDataProvider cornerDataProvider = new DefaultCornerDataProvider(
defaultColumnHeaderDataProvider, rowHeaderDataProvider);
DataLayer cornerDataLayer = new DataLayer(cornerDataProvider);
ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnGroupHeaderLayer);
GridLayer gridLayer = new GridLayer(bodyLayer, columnGroupHeaderLayer, rowHeaderLayer, cornerLayer);
NatTable table = new NatTable(shell, gridLayer, true);
As answered in your previous question How do I fix NullPointerException and putting data into NatTable, this is explained in the NatTable Getting Started Tutorial.
If you need some sample code try the NatTable Examples Application
And from knowing your previous question, your data structure does not work in a table, as you have nested objects where the child objects are stored in an array. So this is more a tree and not a table.

RandomForest with Weka in Java

I am working on a project and I need some examples how to implement RandomForest in Java with weka? I did it with IBk(), it worked. If I do it with RandomForest in the same way, it does not work.
Does anyone have a simple example for me how to implement RandomForest and how to get probability for each class (i did it with IBk withclassifier.distributionForInstance(instance) Function and it returned me probabilities for each class). How can I do it for RandomForest? I will need to get probability of every tree and to combine it?
//example
ConverrterUtils.DataSource source = new ConverterUtils.DataSource ("..../edit.arff);
Instances dataset = source.getDataSet();
dataset.setClassIndex(dataset.numAttributes() - 1);
IBk classifier = new IBk(5); classifier.buildClassifier(dataset);
Instance instance = new SparseInstance(2);
instance.setValue(0, 65) //example data
instance.setValue(1, 120); //example data
double[] prediction = classifier.distributionForInstance(instance);
//now I get the probability for the first class
System.out.println("Prediction for the first class is: "+prediction[0]);
You can calculate the the infogain while buidling the Model in the RandomForest. It is much slower and requires alot of memory while buidling model. I am not so sure about the documentation. you can add options or setValues while buiilding the model.
//numFolds in number of crossvalidations usually between 1-10
//br is your bufferReader
Instances trainData = new Instances(br);
trainData.setClassIndex(trainData.numAttributes() - 1);
RandomForest rf = new RandomForest();
rf.setNumTrees(50);
//You can set the options here
String[] options = new String[2];
options[0] = "-R";
rf.setOptions(options);
rf.buildClassifier(trainData);
weka.filters.supervised.attribute.AttributeSelection as = new weka.filters.supervised.attribute.AttributeSelection();
Ranker ranker = new Ranker();
InfoGainAttributeEval infoGainAttrEval = new InfoGainAttributeEval();
as.setEvaluator(infoGainAttrEval);
as.setSearch(ranker);
as.setInputFormat(trainData);
trainData = Filter.useFilter(trainData, as);
Evaluation evaluation = new Evaluation(trainData);
evaluation.crossValidateModel(rf, trainData, numFolds, new Random(1));
// Using HashMap to store the infogain values of the attributes
int count = 0;
Map<String, Double> infogainscores = new HashMap<String, Double>();
for (int i = 0; i < trainData.numAttributes(); i++) {
String t_attr = trainData.attribute(i).name();
//System.out.println(i+trainData.attribute(i).name());
double infogain = infoGainAttrEval.evaluateAttribute(i);
if(infogain != 0){
//System.out.println(t_attr + "= "+ infogain);
infogainscores.put(t_attr, infogain);
count = count+1;
}
}
//iterating over the hashmap
Iterator it = infogainscores.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey()+" = "+pair.getValue());
System.out.println(pair.getKey()+" = "+pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}

how can I obtain the value of subject of the Stanford Dependencies "nsubj" of a tree?

This is a part of my code:
String sentence="My dog also likes eating sausage.";
LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz");
TokenizerFactory tf = PTBTokenizer.factory(false, new WordTokenFactory());
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
List tokens = tf.getTokenizer(new StringReader(sentence)).tokenize();
lp.parse(tokens); // parse the tokens
Tree t = lp.getBestParse();
How can I get the value of the subject (dog)?
This is a dependency where I would like to extract the Subject:
nsubj(likes-4, dog-2)
Try something like this:
String sentence="My dog also likes eating sausage.";
LexicalizedParser lp = new LexicalizedParser("resources/stanford-parser-2011-06-27/grammar/englishPCFG.ser.gz");
TokenizerFactory tf = PTBTokenizer.factory(false, new WordTokenFactory());
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
List tokens = tf.getTokenizer(new StringReader(sentence)).tokenize();
lp.parse(tokens); // parse the tokens
Tree t = lp.getBestParse();
TreebankLanguagePack languagePack = new PennTreebankLanguagePack();
GrammaticalStructure structure = languagePack.grammaticalStructureFactory().newGrammaticalStructure(t);
Collection<TypedDependency> typedDependencies = structure.typedDependenciesCollapsed();
for(TypedDependency td : typedDependencies) {
if(td.reln().equals(EnglishGrammaticalRelations.NOMINAL_SUBJECT)) {
System.out.println(td);
}
}
which will print:
nsubj(likes-4, dog-2)

Cascading - merge 2 aggregations

I have the following problem whicj I am trying to solve with cascading: I have csv file of records with the structure: o,a,f,i,c
I need to to aggregate the records by o,a,f and to sum the i's and c's per group.
For example:
100,200,300,5,1
100,200,300,6,2
101,201,301,20,5
101,201,301,21,6
should yield:
100,200,300,11,3
101,201,301,41,11
I could not understand how to merge the 2 Every instances that I have (can I aggregate both fields in the same time?).
Do you have any idea?
Yosi
public class CascMain {
public static void main(String[] args){
Scheme sourceScheme = new TextLine(new Fields("line"));
Tap source = new Lfs(sourceScheme, "/tmp/casc/group.csv");
Scheme sinkScheme = new TextDelimited(new Fields("o", "a", "f", "ti", "tc"), ",");
Tap sink = new Lfs(sinkScheme, "/tmp/casc/output/", SinkMode.REPLACE);
Pipe assembly = new Pipe("agg-pipe");
Function function = new RegexSplitter(new Fields("o", "a", "f", "i", "c"), ",");
assembly = new Each(assembly, new Fields("line"), function);
Pipe groupAssembly = new GroupBy("group", assembly, new Fields("o", "a", "f"));
Sum impSum = new Sum(new Fields("ti"));
Pipe i = new Every(groupAssembly, new Fields("i"), impSum);
Sum clickSum = new Sum(new Fields("tc"));
Pipe c = new Every(groupAssembly, new Fields("c"), clickSum);
// WHAT SHOULD I DO HERE
Properties properties = new Properties();
FlowConnector.setApplicationJarClass(properties, CascMain.class);
FlowConnector flowConnector = new FlowConnector(properties);
Flow flow = flowConnector.connect("agg", source, sink, assembly);
flow.complete();
}
}
Use AggregateBy to aggregate multiple fields at the same time:
SumBy impSum = new SumBy(new Fields("i"), new Fields("ti"), long.class);
SumBy clickSum = new SumBy(new Fields("c"), new Fields("tc"), long.class);
assembly = new AggregateBy("totals", Pipe.pipes(assembly), new Fields("o", "a", "f"), 2, impSum, clickSum);

Categories

Resources