profile syntax: syntactic sugar
This commit is contained in:
parent
8676f5e081
commit
6811ea524e
5 changed files with 245 additions and 122 deletions
|
|
@ -31,9 +31,18 @@ final class BExpression
|
|||
|
||||
// Parse the expression and all subexpression
|
||||
public static BExpression parse( BExpressionContext ctx, int level ) throws Exception
|
||||
{
|
||||
return parse( ctx, level, null );
|
||||
}
|
||||
|
||||
private static BExpression parse( BExpressionContext ctx, int level, String optionalToken ) throws Exception
|
||||
{
|
||||
boolean brackets = false;
|
||||
String operator = ctx.parseToken();
|
||||
if ( optionalToken != null && optionalToken.equals( operator ) )
|
||||
{
|
||||
operator = ctx.parseToken();
|
||||
}
|
||||
if ( "(".equals( operator ) )
|
||||
{
|
||||
brackets = true;
|
||||
|
|
@ -145,6 +154,16 @@ final class BExpression
|
|||
exp.typ = VARIABLE_EXP;
|
||||
exp.variableIdx = idx;
|
||||
}
|
||||
else if ( "true".equals( operator ) )
|
||||
{
|
||||
exp.numberValue = 1.f;
|
||||
exp.typ = NUMBER_EXP;
|
||||
}
|
||||
else if ( "false".equals( operator ) )
|
||||
{
|
||||
exp.numberValue = 0.f;
|
||||
exp.typ = NUMBER_EXP;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
|
|
@ -163,17 +182,17 @@ final class BExpression
|
|||
// parse operands
|
||||
if ( nops > 0 )
|
||||
{
|
||||
exp.op1 = BExpression.parse( ctx, level+1 );
|
||||
exp.op1 = BExpression.parse( ctx, level+1, exp.typ == ASSIGN_EXP ? "=" : null );
|
||||
}
|
||||
if ( nops > 1 )
|
||||
{
|
||||
if ( ifThenElse ) checkExpectedToken( ctx, "then" );
|
||||
exp.op2 = BExpression.parse( ctx, level+1 );
|
||||
exp.op2 = BExpression.parse( ctx, level+1, null );
|
||||
}
|
||||
if ( nops > 2 )
|
||||
{
|
||||
if ( ifThenElse ) checkExpectedToken( ctx, "else" );
|
||||
exp.op3 = BExpression.parse( ctx, level+1 );
|
||||
exp.op3 = BExpression.parse( ctx, level+1, null );
|
||||
}
|
||||
if ( brackets )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import java.util.TreeMap;
|
|||
|
||||
import btools.util.BitCoderContext;
|
||||
import btools.util.Crc32;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public final class BExpressionContext
|
||||
|
|
@ -172,7 +173,7 @@ public final class BExpressionContext
|
|||
decode( ld2, false, ab );
|
||||
for( int inum = 0; inum < lookupValues.size(); inum++ ) // loop over lookup names
|
||||
{
|
||||
if ( ld2[inum] != ld[inum] ) throw new RuntimeException( "assertion failed encoding " + getKeyValueDescription(false, ab) );
|
||||
if ( ld2[inum] != ld[inum] ) throw new RuntimeException( "assertion failed encoding inum=" + inum + " val=" + ld[inum] + " " + getKeyValueDescription(false, ab) );
|
||||
}
|
||||
|
||||
return ab;
|
||||
|
|
@ -471,6 +472,51 @@ public final class BExpressionContext
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate random values for regression testing
|
||||
*/
|
||||
public int[] generateRandomValues( Random rnd )
|
||||
{
|
||||
int[] data = createNewLookupData();
|
||||
data[0] = 2*rnd.nextInt( 2 ); // reverse-direction = 0 or 2
|
||||
for( int inum = 1; inum < data.length; inum++ )
|
||||
{
|
||||
int nvalues = lookupValues.get( inum ).length;
|
||||
data[inum] = 0;
|
||||
if ( inum > 1 && rnd.nextInt( 10 ) > 0 ) continue; // tags other than highway only 10%
|
||||
data[inum] = rnd.nextInt( nvalues );
|
||||
}
|
||||
lookupDataValid = true;
|
||||
return data;
|
||||
}
|
||||
|
||||
public void assertAllVariablesEqual( BExpressionContext other )
|
||||
{
|
||||
int nv = variableData.length;
|
||||
int nv2 = other.variableData.length;
|
||||
if ( nv != nv2 ) throw new RuntimeException( "mismatch in variable-count: " + nv + "<->" + nv2 );
|
||||
for( int i=0; i<nv; i++ )
|
||||
{
|
||||
if ( variableData[i] != other.variableData[i] )
|
||||
{
|
||||
throw new RuntimeException( "mismatch in variable " + variableName(i) + " " + variableData[i] + "<->" + other.variableData[i]
|
||||
+ "\ntags = " + getKeyValueDescription( false, encode() ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String variableName( int idx )
|
||||
{
|
||||
for( Map.Entry<String,Integer> e : variableNumbers.entrySet() )
|
||||
{
|
||||
if ( e.getValue().intValue() == idx )
|
||||
{
|
||||
return e.getKey();
|
||||
}
|
||||
}
|
||||
throw new RuntimeException( "no variable for index" + idx );
|
||||
}
|
||||
|
||||
/**
|
||||
* add a new lookup-value for the given name to the given lookupData array.
|
||||
* If no array is given (null value passed), the value is added to
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
package btools.expressions;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import btools.util.BitCoderContext;
|
||||
import btools.util.Crc32;
|
||||
import java.util.Random;
|
||||
|
||||
public final class ProfileComparator
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
if ( args.length != 4 )
|
||||
{
|
||||
System.out.println( "usage: java ProfileComparator <lookup-file> <profile1> <profile2> <nsamples>" );
|
||||
return;
|
||||
}
|
||||
|
||||
File lookupFile = new File( args[0] );
|
||||
File profile1File = new File( args[1] );
|
||||
File profile2File = new File( args[2] );
|
||||
int nsamples = Integer.parseInt( args[3] );
|
||||
testContext( lookupFile, profile1File, profile2File, nsamples, "way" );
|
||||
testContext( lookupFile, profile1File, profile2File, nsamples, "node" );
|
||||
|
||||
}
|
||||
|
||||
private static void testContext( File lookupFile, File profile1File, File profile2File, int nsamples, String contextName )
|
||||
{
|
||||
// read lookup.dat + profiles
|
||||
BExpressionMetaData meta1 = new BExpressionMetaData();
|
||||
BExpressionMetaData meta2 = new BExpressionMetaData();
|
||||
BExpressionContext expctx1 = new BExpressionContext(contextName, 4096, meta1 );
|
||||
BExpressionContext expctx2 = new BExpressionContext(contextName, 4096, meta2 );
|
||||
meta1.readMetaData( lookupFile );
|
||||
meta2.readMetaData( lookupFile );
|
||||
expctx1.parseFile( profile1File, "global" );
|
||||
expctx2.parseFile( profile2File, "global" );
|
||||
|
||||
Random rnd = new Random();
|
||||
for( int i=0; i<nsamples; i++ )
|
||||
{
|
||||
int[] data = expctx1.generateRandomValues( rnd );
|
||||
expctx1.evaluate( data );
|
||||
expctx2.evaluate( data );
|
||||
|
||||
expctx1.assertAllVariablesEqual( expctx2 );
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue