Skip to content

Commit

Permalink
samples & runtimes for apl 4.2307.0
Browse files Browse the repository at this point in the history
  • Loading branch information
i317814 committed Mar 2, 2023
1 parent 8f6814f commit 2ca1b4d
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 56 deletions.
Binary file modified dataForHANACloud.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion runtimes/cpp/DateUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ bool hasFiveWeeks(int iYear, int iMonth)
return true;
else if ((30 == lNbDays) && ((2 == lFirstDay) || (3 == lFirstDay)))
return true;
else if ((29 == lNbDays) && (3 == lNbDays))
else if ((29 == lNbDays) && (3 == lFirstDay))
return true;
else
return false;
Expand Down
Binary file modified runtimes/java/KxJRT.jar
Binary file not shown.
12 changes: 6 additions & 6 deletions runtimes/java/src/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static int ymdToScalar(int iYear, int iMonth, int iDay) {
lScalar += (iYear * 365 + iYear / 4 - iYear / 100 + iYear / 400);
return lScalar;
}
public static int getDayInMonth(Date iDate) {
public static int getDaysInMonth(Date iDate) {
Calendar lCalendar = Calendar.getInstance();
lCalendar.setTime(iDate);
int lMonthDay = 31;
Expand Down Expand Up @@ -159,14 +159,14 @@ else if (lCalendar.get(Calendar.MONTH) == Calendar.DECEMBER) {
}

public static boolean hasFiveWeeks(Date iDate) {
int lMonthDay = getDayInMonth(iDate);
int lNbDays = getDaysInMonth(iDate);
int lFirstDay = getDayOfWeek(iDate);

if ((31 == lMonthDay) && (lFirstDay >= 1) && (lFirstDay <=3))
if ((31 == lNbDays) && (lFirstDay >= 1) && (lFirstDay <=3))
return true;
else if ((30 == lMonthDay) && ((2 == lFirstDay) || (3 == lFirstDay)))
else if ((30 == lNbDays) && ((2 == lFirstDay) || (3 == lFirstDay)))
return true;
else if ((29 == lMonthDay) && (3 == lMonthDay))
else if ((29 == lNbDays) && (3 == lFirstDay))
return true;
else
return false;
Expand Down Expand Up @@ -209,7 +209,7 @@ public static int getWeekOfMonth(Date iDate) {
lWMonth1 = 4 + (l5Weeks ? 1 : 0);
}

int lNbDaysInMonth = getDayInMonth(iDate);
int lNbDaysInMonth = getDaysInMonth(iDate);

Calendar lNewDate = Calendar.getInstance();
lNewDate.set(lCalendar.get(Calendar.YEAR),
Expand Down
2 changes: 1 addition & 1 deletion runtimes/java/src/IKxJModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package KxJRT;

/**
* The <a href="http://www.kxen.com">Automatic Analytics</a> Java model interface.
* The Automatic Analytics Java model interface.
* Generated Java code for K2R and K2S implements this interface.
* @version 1.0
*/
Expand Down
27 changes: 3 additions & 24 deletions runtimes/java/src/KxFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public boolean hasMoreLines() {
while (lCurrentLine.equals("")) {
try {
lCurrentLine = mBuffer.readLine();
if (lCurrentLine == null) {
return false;
}
}
catch ( IOException e ) {
return false;
Expand Down Expand Up @@ -174,30 +177,6 @@ private void initFields( String iLine ) {
mFields = new String[lFieldsCount];
}

private void printLineError() {
KxLog.getInstance().print("More fields are found\n");
System.err.println( "error : found more fields than exprected line "+
mRowCounter +".");
}

private void printFieldError( int iFieldIndex ) {
KxLog.getInstance().print( "warning : invalid type for field "+
iFieldIndex+ " line " + mRowCounter + "." );
}

private void printEmptyField( int iFieldIndex ) {
if (null != mNameFields) {
KxLog.getInstance().print( "warning : empty value for " +
mNameFields[iFieldIndex] +" at line " +
mRowCounter + "." );
}
else {
KxLog.getInstance().print( "warning : empty value for fields " +
iFieldIndex +" at line " +
mRowCounter + "." );
}
}

public boolean isEmpty( int iVariableIndex, String iMissingString ) {
String lValue = mFields[ iVariableIndex ];
if( lValue == null
Expand Down
2 changes: 1 addition & 1 deletion runtimes/java/src/KxFileReaderWithNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void init() {
}
}

public String[] getVariables() {
final public String[] getVariables() {
return mVariables;
}

Expand Down
11 changes: 0 additions & 11 deletions runtimes/java/src/KxInputReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,6 @@ public void initFields( String iLine ) {
}
}

private void printLineError() {
KxLog.getInstance().print("More fields are found\n");
System.err.println( "error : found more fields than exprected line "+
mRowCounter +".");
}

private void printFieldError( int iFieldIndex ) {
KxLog.getInstance().print( "warning : invalid type for field "+
iFieldIndex+ " line " + mRowCounter + "." );
}

private void printEmptyField( int iFieldIndex ) {
KxLog.getInstance().print("warning : empty value for fields " +
iFieldIndex +" at line " +
Expand Down
2 changes: 1 addition & 1 deletion runtimes/java/src/KxJApplyOnFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.lang.*;

/**
* Applies a <a href="http://www.kxen.com">Automatic Analytics</a> Java model on a flat file
* Applies a Automatic Analytics Java model on a flat file
* with variable names on the first line.
* @see KxJRT.KxFileReaderWithNames KxFileReaderWithNames
* @see KxJRT.KxFileReader KxFileReader
Expand Down
2 changes: 1 addition & 1 deletion runtimes/java/src/KxJApplyOnInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.TreeMap;

/**
* Applies a <a href="http://www.kxen.com">Automatic Analytics</a> Java model on a line
* Applies a Automatic Analytics Java model on a line
* @see KxJRT.KxInputReader KxInputReader
*/
public class KxJApplyOnInput {
Expand Down
12 changes: 7 additions & 5 deletions runtimes/java/src/KxJApplyOnJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ else if( iArgs[i].equalsIgnoreCase("-usage") ||
lApplyScore = new KxJApplyOnJDBC(lModel);

loadDriver(lDriver);

Connection lConnection = newConnection(lDatabase, lUser, lPwd);
Statement lStatement = lConnection.createStatement();

ResultSet lResult = lStatement.executeQuery(lQuery);
try(Connection lConnection = newConnection(lDatabase, lUser, lPwd);
Statement lStatement = lConnection.createStatement())
{

lApplyScore.apply(lResult, lOut);
ResultSet lResult = lStatement.executeQuery(lQuery);

lApplyScore.apply(lResult, lOut);
}

} catch(Exception e )
{
Expand Down
8 changes: 5 additions & 3 deletions runtimes/java/src/KxJApplyWithMapWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.text.ParseException;

public class KxJApplyWithMapWrapper {
public static String mDesc = "Automatic Analytics command line score calculator.\n Expected input:\n \n KxJApplyWithMapWrapper ScorerClass var1=value1 var2=value2 ...\n \n where \"ScorerClass\" is the name of the class generated by Automatic Analytics (which should\n be in the classpath)";

public String mScorerClassName;
private IKxJModel mScorerModel;

Expand Down Expand Up @@ -120,7 +118,11 @@ else if( iArgs[i].equalsIgnoreCase("-usage") ) {
BufferedReader lBuffer = new BufferedReader(lStreamReader);

// skip the first row which contains the header file
String lCurrentLine = lBuffer.readLine();
String lCurrentLine = lBuffer.readLine();
if (lCurrentLine == null) {
System.out.println("Empty input file.");
return;
}
String[] lVariables = lCurrentLine.split(lSeparator);

for (String lOutput:lMapWrapper.mScorerModel.getModelOutputVariables())
Expand Down
2 changes: 0 additions & 2 deletions runtimes/java/src/KxJDBCInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public float floatValue( int iIndex ) {
mMissing = false;
}
catch( SQLException e ) {
lValue = (float)0.0;
mMissing = true;
}
return lValue;
Expand All @@ -112,7 +111,6 @@ public double doubleValue( int iIndex ) {
mMissing = false;
}
catch( SQLException e ) {
lValue = 0.0;
mMissing = true;
}
return lValue;
Expand Down
64 changes: 64 additions & 0 deletions sql/any/apl_samples/binary-classification/segmented_apply_hce.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
-- @required(hanaMinimumVersion,4.00.000)
-- ================================================================
-- APL_AREA, APPLY_MODEL, using a binary format for the model
-- This script demonstrates the application of the binary classification model
-- to predict the target and to get the individual contributions per input
--
-- Assumption 1: The users & privileges have been created & granted (see apl_admin_ex.sql).
-- Assumption 2: There's a valid trained model (created by APL) in the MODEL_TRAIN_BIN table.
-- @depend(segmented_create_train_hce.sql)
connect USER_APL password Password1;
SET SESSION 'APL_CACHE_SCHEMA' = 'APL_CACHE';

drop table APPLY_OPERATION_LOG;
create table APPLY_OPERATION_LOG like "SAP_PA_APL"."sap.pa.apl.base::BASE.T.OPERATION_LOG";

drop table APPLY_SUMMARY;
create table APPLY_SUMMARY like "SAP_PA_APL"."sap.pa.apl.base::BASE.T.SUMMARY";

-- USER_APL.APPLY_OUT definition
DROP TYPE APPLY_OUT_T;
CREATE TYPE APPLY_OUT_T AS TABLE ("KxIndex" BIGINT,"class" INTEGER,"gb_score_class" DOUBLE, "Seg" INTEGER);

DROP TABLE APPLY_OUT;
CREATE TABLE APPLY_OUT LIKE APPLY_OUT_T;


DO BEGIN
declare header "SAP_PA_APL"."sap.pa.apl.base::BASE.T.FUNCTION_HEADER";
declare config "SAP_PA_APL"."sap.pa.apl.base::BASE.T.OPERATION_CONFIG_EXTENDED";
declare model "SAP_PA_APL"."sap.pa.apl.base::BASE.T.MODEL_BIN_OID";
declare out_log "SAP_PA_APL"."sap.pa.apl.base::BASE.T.OPERATION_LOG";
declare out_sum "SAP_PA_APL"."sap.pa.apl.base::BASE.T.SUMMARY";
declare apply_out APPLY_OUT_T;

insert into :model select * from MODEL_TRAIN_BIN;
datatset = SELECT * from "USER_APL"."ADULT01_SORTED";

:header.insert(('Oid', '#42'));
:header.insert(('LogLevel', '2'));
:header.insert(('ModelFormat', 'bin'));
:header.insert(('MaxTasks', '2')); -- define nb parallel tasks to use for train

:config.insert(('APL/SegmentColumnName', 'Seg',null)); -- define the column used as the segmentation colum

CALL _SYS_AFL.APL_APPLY_MODEL__OVERLOAD_4_3(:header, :model, :config, :datatset,apply_out,out_log,out_sum);

insert into APPLY_OUT select * from :apply_out;
insert into APPLY_OPERATION_LOG select * from :out_log;
insert into APPLY_SUMMARY select * from :out_sum;
END;

SELECT "Seg", count(*) from APPLY_OUT group by "Seg";

-- Nb trained models
select count(*) from APPLY_SUMMARY where "KEY" = 'AplTaskElapsedTime';

-- Average time to train a segment
select AVG(to_double("VALUE")) from APPLY_SUMMARY where "KEY" = 'AplTaskElapsedTime';

-- Total time
select * from APPLY_SUMMARY where "KEY" = 'AplTotalElapsedTime';

-- Get models in error
select * from APPLY_OPERATION_LOG where "LEVEL" = 0;

0 comments on commit 2ca1b4d

Please sign in to comment.