private List<KeyValue> convertAndOrderField(HashMap<String, Integer> tmpField, Integer threshold) { List<Entry<String, Integer>> list = new ArrayList<>(tmpField.entrySet()); Collections.sort(list, new Comparator<Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o2.getValue().compareTo(o1.getValue()); //Note: exchanged o2 and o1 for descending order! } }); List<KeyValue> result = new ArrayList<>(); int i = 0; for (Iterator<Entry<String, Integer>> it = list.iterator(); it.hasNext(); i++) { if (i >= threshold) break; Entry<String, Integer> entry = it.next(); result.add(new DefaultKeyValue(entry.getKey(), entry.getValue())); } return result; }
public String[][] getRPCFriendlyAddressing() { String[][] toReturn = new String[this.addressing.length][2] ; for(int i=0;i<toReturn.length;i++) for (KeyValue val : this.addressing) { toReturn[i][0] = ( String ) val.getKey( ); toReturn[i][1] = ( String ) val.getValue( ); } return toReturn; }
public String [ ] getAddressingKeys ( ) { final String result[] = new String [ this.getAddressing( ).length ]; int counter = 0; for ( final KeyValue predicate : this.getAddressing( ) ) result[ counter++ ] = ( String ) predicate.getKey( ); return result; }
public String [ ] getAddressingValues ( ) { final String result[] = new String [ this.getAddressing( ).length ]; int counter = 0; for ( final KeyValue predicate : this.getAddressing( ) ) result[ counter++ ] = ( String ) predicate.getValue( ); return result; }
/** * Note that the key and value both are trimmed before being inserted into * the data strcture. * * @return */ public TreeMap < String , String > getMainClassInitialParams ( ) { if ( !this.isGetMainClassInitParamsInitialized ) { this.isGetMainClassInitParamsInitialized = true; for ( final KeyValue param : this.mainClassInitialParams ) { this.mainClassInitParams.put( param.getKey( ).toString( ).toLowerCase( ) , param.getValue( ).toString( ) ); } } return this.mainClassInitParams; }
public String toString ( ) { final StringBuilder builder = new StringBuilder( "Input Stream [" ); for ( final InputStream inputStream : this.getInputStreams( ) ) { builder.append( "Input-Stream-Name" ).append( inputStream.getInputStreamName( ) ); builder.append( "Input-Stream-Query" ).append( inputStream.getQuery( ) ); builder.append( " Stream-Sources ( " ); if ( inputStream.getSources( ) == null ) builder.append( "null" ); else for ( final StreamSource ss : inputStream.getSources( ) ) { builder.append( "Stream-Source Alias : " ).append( ss.getAlias( ) ); for ( final AddressBean addressing : ss.getAddressing( ) ) { builder.append( "Stream-Source-wrapper >" ).append( addressing.getWrapper( ) ).append( "< with addressign predicates : " ); for ( final KeyValue keyValue : addressing.getPredicates( ) ) builder.append( "Key=" ).append( keyValue.getKey( ) ).append( "Value=" ).append( keyValue.getValue( ) ); } builder.append( " , " ); } builder.append( ")" ); } builder.append( "]" ); return "VSensorConfig{" + "name='" + this.name + '\'' + ", priority=" + this.priority + ", mainClass='" + this.mainClass + '\'' + ", description='" + this.description + '\'' + ", outputStreamRate=" + this.outputStreamRate + ", addressing=" + this.addressing + ", outputStructure=" + this.outputStructure + ", storageHistorySize='" + this.storageHistorySize + '\'' + builder.toString( ) + ", mainClassInitialParams=" + this.mainClassInitialParams + ", lastModified=" + this.lastModified + ", fileName='" + this.fileName + '\'' + ", logger=" + this.logger + ", nameInitialized=" + this.nameInitialized + ", isStorageCountBased=" + this.isStorageCountBased + ", parsedStorageSize=" + this.parsedStorageSize + '}'; }
public void preprocess_addressing() { if (!addressing_processed) { for (KeyValue kv:getAddressing()) if (kv.getKey().toString().equalsIgnoreCase("altitude")) cached_altitude=Double.parseDouble(kv.getValue().toString()); else if (kv.getKey().toString().equalsIgnoreCase("longitude")) cached_longitude=Double.parseDouble(kv.getValue().toString()); else if (kv.getKey().toString().equalsIgnoreCase("latitude")) cached_latitude=Double.parseDouble(kv.getValue().toString()); addressing_processed=true; } }
public AddressBean ( final String wrapper , KeyValue... newPredicates ) { this.wrapper = wrapper; if (newPredicates == null) this.predicates=EMPTY_PREDICATES; else this.predicates = newPredicates; }
public String getPredicateValueWithException ( String key ) { key = key.trim( ); for ( KeyValue predicate : this.predicates ) { if ( predicate.getKey( ).toString( ).trim( ).equalsIgnoreCase( key ) ) { final String value = ( String ) predicate.getValue( ); if (value.trim().length()>0) return ( value); } } throw new RuntimeException("The required parameter: >"+key+"<+ is missing.from the virtual sensor configuration file."); }
/** * Note that the key for the value is case insensitive. * * @param key * @return */ public String getPredicateValue ( String key ) { key = key.trim( ); for ( KeyValue predicate : this.predicates ) { if ( predicate.getKey( ).toString( ).trim( ).equalsIgnoreCase( key ) ) return ( ( String ) predicate.getValue( )); } return null; }
public String toString ( ) { final StringBuffer result = new StringBuffer( "[" ).append( this.getWrapper( ) ); for ( final KeyValue predicate : this.predicates ) { result.append( predicate.getKey( ) + " = " + predicate.getValue( ) + "," ); } result.append( "]" ); return result.toString( ); }
public void testConnectToExistingMySQLDB ( ) { StreamExporterVirtualSensor vs = new StreamExporterVirtualSensor( ); ArrayList < KeyValue > params = new ArrayList < KeyValue >( ); params.add( new KeyValueImp( StreamExporterVirtualSensor.PARAM_URL , url ) ); params.add( new KeyValueImp( StreamExporterVirtualSensor.PARAM_USER , user ) ); params.add( new KeyValueImp( StreamExporterVirtualSensor.PARAM_PASSWD , passwd ) ); config.setMainClassInitialParams( params ); vs.setVirtualSensorConfiguration( config ); assertTrue( vs.initialize( ) ); }
@Test(groups = {"unit"}) public void testReadColumns() throws Exception { Map<String, String> testResultMap = new HashMap<String,String>(); testResultMap.put(columnNames.get(0), columnValues.get(0)); testResultMap.put(columnNames.get(1), columnValues.get(1)); ColumnSlice columnSlice = mock(ColumnSlice.class); HColumn column1 = mock(HColumn.class); HColumn column2 = mock(HColumn.class); when(column1.getName()).thenReturn(columnNames.get(0)); when(column1.getValue()).thenReturn(columnValues.get(0)); when(column2.getName()).thenReturn(columnNames.get(1)); when(column2.getValue()).thenReturn(columnValues.get(1)); when(columnSlice.getColumns()).thenReturn(Arrays.asList(column1, column2)); when(executionResult.get()).thenReturn(columnSlice); //========================= List<KeyValue> actualResult = columnFamilyTestDao.readColumns(rowKey, new ColumnMapper<KeyValue,String,String>() { @Override public KeyValue mapColumn(String columnName, String columnValue) { return new DefaultKeyValue(columnName, columnValue); } }); //========================= Map<String,String> resultMap = new HashMap<String,String>(); for (KeyValue kv : actualResult) { resultMap.put((String)kv.getKey(), (String)kv.getValue()); } assertEquals(resultMap, testResultMap); }
public void evaluate(LoggedInInfo loggedInInfo, Denominator deno, Numerator numer,List<KeyValue> additionalFields){ denominator = deno; numerator = numer; List demoList = deno.getDenominatorList(); denominatorCount = demoList.size(); setReportResultList(new ArrayList<Hashtable<String,Object>>()); for (int i = 0; i < demoList.size(); i++){ String demo = (String) demoList.get(i); boolean bool = numer.evaluate(loggedInInfo, demo); //Object obj = numer.getOutputValues(); // PROBLEM IS THAT THIS WILL ALWAYS HAVE A VALUE Hashtable<String,Object> h = new Hashtable<String,Object>(); h.put("_demographic_no",demo); h.put("_report_result",new Boolean(bool)); if (additionalFields != null){ for(KeyValue field:additionalFields){ String key = (String) field.getKey(); String val = (String) field.getValue(); EctMeasurementsDataBeanHandler ect = new EctMeasurementsDataBeanHandler(Integer.valueOf(demo), val); Collection<EctMeasurementsDataBean> v = ect.getMeasurementsDataVector(); //Execute for the value and attach it to the key in the hashtable //Object obj = if(v.iterator().hasNext()){ h.put(key, v.iterator().next()); } } } getReportResultList().add(h); // if (obj != null){ // getReportResultList().add(obj); // } if (bool){ numeratorCount++; } } }
/** * @return Returns the addressing. */ public KeyValue[] getAddressing ( ) { return this.addressing; }
/** * @param addressing The addressing to set. */ public void setAddressing ( KeyValue [] addressing ) { this.addressing = addressing; }
public void setMainClassInitialParams ( final ArrayList < KeyValue > mainClassInitialParams ) { this.mainClassInitialParams = mainClassInitialParams; }
public static VSensorConfig vsensor(VsConf vs){ VSensorConfig v=new VSensorConfig(); v.setMainClass(vs.processing().className()); v.setDescription(vs.description()); v.setName(vs.name()); v.setIsTimeStampUnique(vs.processing().uniqueTimestamp()); if (vs.poolSize().isDefined()) v.setLifeCyclePoolSize(((Integer)vs.poolSize().get())); if (vs.processing().rate().isDefined()) v.setOutputStreamRate(((Integer)vs.processing().rate().get())); v.setPriority(vs.priority()); KeyValueImp [] addr=new KeyValueImp[vs.address().size()]; Iterable<String> keys=JavaConversions.asJavaIterable(vs.address().keys()); int i=0; for (String k:keys){ addr[i]=new KeyValueImp(k,vs.address().apply(k)); i++; } v.setAddressing(addr); InputStream[] is=new InputStream[vs.streams().size()]; for (int j=0;j<is.length;j++){ is[j]=stream(vs.streams().apply(j)); } v.setInputStreams(is); if (vs.processing().webInput().isDefined()){ WebInputConf wic=vs.processing().webInput().get(); v.setWebParameterPassword(wic.password()); WebInput[] wi=new WebInput[wic.commands().size()]; for (int j=0;j<wi.length;j++){ wi[j]=webInput(wic.commands().apply(j)); } v.setWebInput(wi); } DataField [] out=new DataField[(vs.processing().output().size())]; for (int j=0;j<out.length;j++){ out[j]=dataField(vs.processing().output().apply(j)); } v.setOutputStructure(out); Map<String,String> init=vs.processing().initParams(); ArrayList<KeyValue> ini=new ArrayList<KeyValue>(); Iterable<String> initkeys=JavaConversions.asJavaIterable(init.keys()); for (String ik:initkeys){ logger.trace("keys:"+ik); ini.add(new KeyValueImp(ik.toLowerCase(),init.apply(ik))); } v.setMainClassInitialParams(ini); StorageConfig st=new StorageConfig(); if (vs.storageSize().isDefined()) st.setStorageSize(vs.storageSize().get()); if (vs.storage().isDefined()){ StorageConf sc=vs.storage().get(); if (sc.identifier().isDefined()) st.setIdentifier(sc.identifier().get()); st.setJdbcDriver(sc.driver()); st.setJdbcURL(sc.url()); st.setJdbcUsername(sc.user()); st.setJdbcPassword(sc.pass()); } if (st.getStorageSize()!=null || st.getJdbcURL()!=null) v.setStorage(st); return v; }
public KeyValue[] getPredicates() { return this.predicates; }
public void testLogStatementIntoMySQLDB ( ) { StreamExporterVirtualSensor vs = new StreamExporterVirtualSensor( ); // configure parameters ArrayList < KeyValue > params = new ArrayList < KeyValue >( ); params.add( new KeyValueImp( StreamExporterVirtualSensor.PARAM_URL , url ) ); params.add( new KeyValueImp( StreamExporterVirtualSensor.PARAM_USER , user ) ); params.add( new KeyValueImp( StreamExporterVirtualSensor.PARAM_PASSWD , passwd ) ); config.setMainClassInitialParams( params ); vs.setVirtualSensorConfiguration( config ); vs.initialize( ); // configure datastream Vector < DataField > fieldTypes = new Vector < DataField >( ); Object [ ] data = null; for ( String type : DataTypes.TYPE_NAMES ) fieldTypes.add( new DataField( type , type , type ) ); int i = 0; for ( Object value : DataTypes.TYPE_SAMPLE_VALUES ) data[ i++ ] = value; long timeStamp = new Date( ).getTime( ); StreamElement streamElement = new StreamElement( fieldTypes.toArray( new DataField[] {} ) , ( Serializable [ ] ) data , timeStamp ); // give datastream to vs vs.dataAvailable( streamName , streamElement ); // clean up and control boolean result = true; try { DriverManager.registerDriver( new com.mysql.jdbc.Driver( ) ); Connection connection = DriverManager.getConnection( url , user , passwd ); Statement statement = connection.createStatement( ); statement.execute( "SELECT * FROM " + streamName ); System.out.println( "result" + result ); result = statement.getResultSet( ).last( ); System.out.println( "result" + result ); } catch ( SQLException e ) { // TODO Auto-generated catch block e.printStackTrace( ); result = false; } assertTrue( result ); }
public void evaluate(Denominator deno, Numerator numer,List<KeyValue> additionalFields){ denominator = deno; numerator = numer; List demoList = deno.getDenominatorList(); denominatorCount = demoList.size(); setReportResultList(new ArrayList<Hashtable<String,Object>>()); for (int i = 0; i < demoList.size(); i++){ String demo = (String) demoList.get(i); boolean bool = numer.evaluate(demo); //Object obj = numer.getOutputValues(); // PROBLEM IS THAT THIS WILL ALWAYS HAVE A VALUE Hashtable<String,Object> h = new Hashtable<String,Object>(); h.put("_demographic_no",demo); h.put("_report_result",new Boolean(bool)); if (additionalFields != null){ for(KeyValue field:additionalFields){ String key = (String) field.getKey(); String val = (String) field.getValue(); EctMeasurementsDataBeanHandler ect = new EctMeasurementsDataBeanHandler(demo, val); Collection<EctMeasurementsDataBean> v = ect.getMeasurementsDataVector(); //Execute for the value and attach it to the key in the hashtable //Object obj = if(v.iterator().hasNext()){ h.put(key, v.iterator().next()); } } } getReportResultList().add(h); // if (obj != null){ // getReportResultList().add(obj); // } if (bool){ numeratorCount++; } } }
/** * Constructs a new entry from the specified <code>KeyValue</code>. * * @param pair the pair to copy, must not be null * @throws NullPointerException if the entry is null */ public DefaultMapEntry(final KeyValue pair) { super(pair.getKey(), pair.getValue()); }