📄 multidimensionalstream.java
字号:
// Prepare ordering:
nAttOrd = ordAtts.length;
attTypeOrd = new boolean[nAttOrd];
resTypeOrd = new int[nAttOrd];
indOrd = new int[nAttOrd];
for (int i = 0; i < nAttOrd; i++) {
// Get ordering attribute and check if valid:
String oname = ordAtts[i].getName();
MiningAttribute oAtt = metaData.getMiningAttribute(oname);
if (oAtt == null)
throw new MiningException("unknown attribute name in ordering attribute");
indOrd[i] = metaData.getAttributeIndex(oAtt);
if (oAtt instanceof CategoricalAttribute)
attTypeOrd[i] = true;
else
attTypeOrd[i] = false;
if ( ordAtts[i].getOrderType() == OrderAttribute.DOWN)
this.resTypeOrd[i] = +1;
else
this.resTypeOrd[i] = -1;
};
// Sort:
Quicksort(0, nSelVecOrd-1);
// Copy back:
IntVector tVec = new IntVector();
for (int i = 0; i < nSelVecOrd; i++) {
int ip = selVecOrd[i];
tVec.addElement( selVec.IntegerAt(ip) );
};
selVec = tVec;
}
/**
* Compares its two arguments for order. Returns a negative integer,
* zero, or a positive integer as the first argument is less than,
* equal to, or greater than the second. Compares all ordering attributes
* in their initial order.
*
* @param i1 first integer object to be compared
* @param i2 second integer object to be compared
* @return a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the second
*/
private int compare(int i1, int i2) {
int ip1 = selVecOrd[i1];
int ip2 = selVecOrd[i2];
MiningVector mv1;
MiningVector mv2;
try { // move operation required!!
mv1 = miningInputStream.read( selVec.IntegerAt(ip1) );
mv2 = miningInputStream.read( selVec.IntegerAt(ip2) );
}
catch (MiningException ex) {
return 0;
}
for (int i = 0; i < nAttOrd; i++) {
double a = mv1.getValue( indOrd[i] );
double b = mv2.getValue( indOrd[i] );
if ( Category.isMissingValue(a) || Category.isMissingValue(b) )
continue;
int comp = 0;
if (attTypeOrd[i]) {
CategoricalAttribute catAtt = (CategoricalAttribute)
metaData.getMiningAttribute( indOrd[i] );
Category catA = catAtt.getCategory(a);
Category catB = catAtt.getCategory(b);
if ( catA == null || catB == null )
continue;
String sA = catA.toString();
String sB = catB.toString();
comp = sA.compareTo(sB);
}
else {
comp = (int) (a - b);
if (comp == 0) {
if (a > b) comp = 1;
else if (a < b) comp = -1;
}
}
int res = comp*resTypeOrd[i];
if (res != 0)
return res;
}
return 0;
}
/**
* Sorts the selVecOrd vector with compare metrics method by means
* of the SelectionSort algorithm. Always the smallest element
* is founded and swapped.
*
* @param l index of first element (inclusive) to be sorted
* @param r index of last element (inclusive) to be sorted
*/
private void SelectionSort(int l, int r)
{
for (int i = l; i < r; i++)
{
int min = i;
for (int j=i+1; j <= r; j++)
if ( compare(j, min) > 0 ) min = j;
if (min == i) continue;
int it = selVecOrd[min];
selVecOrd[min] = selVecOrd[i];
selVecOrd[i] = it;
}
}
/**
* Partitioning of whole set into two subsets. The elements of the
* first subset are lower or equal than the elements of the second subset.
* This method is used for sorting in Quicksort.
*
* @param l index of first element (inclusive) to be partitioned
* @param r index of last element (inclusive) to be partitioned
* @return boundary element between subsets
*/
private int Partition(int l, int r)
{
int ind = l;
int lm = l-1;
int rm = r+1;
for(;;)
{
do rm--;
while ( compare(ind, rm) > 0 );
do lm++;
while ( compare(lm, ind) > 0 );
if (lm < rm)
{
int it = selVecOrd[rm];
selVecOrd[rm] = selVecOrd[lm];
selVecOrd[lm] = it;
if (ind == lm) ind = rm;
else if (ind == rm) ind = lm;
}
else return rm;
}
}
/**
* Sorts the selVecOrd vector with compare metrics method by means
* of the Quickort algorithm. If the difference r-l is small, the
* SelectionSort method is applied.
*
* @param l index of first element (inclusive) to be sorted
* @param r index of last element (inclusive) to be sorted
*/
private void Quicksort(int l, int r)
{
if(l < (r-SORT_SMALL_SET))
{
int split_pt = Partition(l, r); // find Pivot element
Quicksort(l, split_pt); // sort left subset
Quicksort(split_pt + 1, r); // sort right subset
}
else
SelectionSort(l, r); // use selection sort for small data
}
// -----------------------------------------------------------------------
// General stream methods
// -----------------------------------------------------------------------
/**
* Opens multidimensional stream. Passes same method to source
* input stream.
*
* @exception MiningException if a mining source access error occurs
*/
public void open() throws MiningException
{
miningInputStream.open();
}
/**
* Closes multidimensional stream. Passes same method to source
* input stream.
*
* @exception MiningException if a mining source access error occurs
*/
public void close() throws MiningException
{
miningInputStream.close();
}
/**
* Determines meta data of multidimensional stream. Passes same method
* to source input strean.
*
* @return meta data of stream
* @throws MiningException if a mining source access error occurs
*/
public MiningDataSpecification recognize() throws MiningException
{
return miningInputStream.recognize();
}
// -----------------------------------------------------------------------
// Methods of cursor positioning
// -----------------------------------------------------------------------
/**
* Reset cursor using the reset method of the mining input stream.
*
* @exception MiningException if a mining source access error occurs
*/
public void reset() throws com.prudsys.pdm.Core.MiningException
{
cursorPosition = -1;
miningInputStream.reset();
}
/**
* Advances cursor position using the next method of mining input
* stream.
*
* @return true if there is a further vector to read, elso false
* @exception MiningException if a mining source access error occurs
*/
public boolean next() throws MiningException
{
cursorPosition++;
if( cursorPosition >= getVectorsNumber() )
{
return false;
}
int newPosition = cursorPosition;
if (selVec != null) newPosition = selVec.IntegerAt(cursorPosition);
if (allowsMove)
miningInputStream.move( newPosition );
else {
if (cursorPosition == 0) {
miningInputStream.next();
for (int i = 0; i < newPosition; i++)
miningInputStream.next();
}
else {
int oldPosition = cursorPosition-1;
if (selVec != null) oldPosition = selVec.IntegerAt(cursorPosition-1);
for (int i = oldPosition; i < newPosition; i++)
miningInputStream.next();
}
}
return true;
}
/**
* Move cursor to specified position using the move
* method of the mining input stream.
*
* @param position new position of the cursor
* @return true if cursor could be positioned, false if not
* @exception MiningException if a mining source access error occurs
*/
public boolean move(int position) throws MiningException
{
if (position < 0 || position >= getVectorsNumber())
return false;
int newPosition = position;
if (selVec != null) newPosition = selVec.IntegerAt(position);
if (allowsMove)
miningInputStream.move( newPosition );
else {
miningInputStream.reset();
miningInputStream.next();
for (int i = 0; i < newPosition; i++)
miningInputStream.next();
}
return true;
}
// -----------------------------------------------------------------------
// Methods of reading from the stream
// -----------------------------------------------------------------------
/**
* Read mining vector at current cursor position using the read method
* of the source input stream.
*
* @return mining vector at current cursor position
* @exception MiningException if a mining source access error occurs
*/
public MiningVector read() throws MiningException
{
return miningInputStream.read();
}
// -----------------------------------------------------------------------
// Test
// -----------------------------------------------------------------------
/**
* Example of using a MultidimensionalStream selection.
*
* @param args arguments (ignored)
*/
public static void main(String[] args) {
try {
// Open data source:
MiningCsvStream inputStream0 = new MiningCsvStream("data/csv/carsales.csv");
inputStream0.open();
MiningArrayStream inputStream = new MiningArrayStream(inputStream0);
MiningDataSpecification metaData = inputStream.getMetaData();
// Open multidimensional object and read data:
System.out.println("read MultidimensionalStream data...");
MultidimensionalStream ms = new MultidimensionalStream(inputStream);
ms.readMultidimensionalStreamData();
// Run selection for 'carsales' using SelectionAttribute:
System.out.println("run MultidimensionalStream selection...");
// First selection using array of selection attributes:
SelectAttribute[] selAtt = new SelectAttribute[2];
selAtt[0] = new SelectAttribute("country", "Greece");
selAtt[1] = new SelectAttribute("product", "Alfa");
selAtt[1].addCategory("Citroen");
IntVector selVec = ms.runSelections(selAtt);
System.out.println("selVec: ");
for (int i = 0; i < selVec.size(); i++)
System.out.print(selVec.IntegerAt(i) + " ");
System.out.println();
System.out.println("Selected stream: " + ms);
// Add new selection attribute:
SelectAttribute sa = new SelectAttribute("time", "Tue");
selVec = ms.addSelection(sa);
System.out.println("Selected stream: " + ms);
// Remove all selection attributes:
ms.removeSelection(selAtt[0]);
ms.removeSelection(selAtt[1]);
ms.removeSelection(sa);
System.out.println("Selected stream: " + ms);
// Run same selection for 'carsales' using Predicate:
MiningAttribute product = metaData.getMiningAttribute("product");
MiningAttribute country = metaData.getMiningAttribute("country");
MiningAttribute time = metaData.getMiningAttribute("time");
SimplePredicate sp1 = new SimplePredicate(country, "Greece", SimplePredicate.EQUAL);
SimplePredicate sp2 = new SimplePredicate(time, "Tue", SimplePredicate.NOT_EQUAL);
SimpleSetPredicate sp3 = new SimpleSetPredicate(product, SimpleSetPredicate.IS_IN , 2);
sp3.setValue("Alfa", 0);
sp3.setValue("Citroen", 1);
CompoundPredicate cp = new CompoundPredicate(CompoundPredicate.AND, 3);
cp.setPredicate(sp1, 0);
cp.setPredicate(sp2, 1);
cp.setPredicate(sp3, 2);
ms.runSelectionsPredicate(cp);
System.out.println("Selected stream: " + ms);
// Define ordering:
OrderAttribute[] ordAtt = new OrderAttribute[2];
ordAtt[0] = new OrderAttribute("product", OrderAttribute.UP );
ordAtt[1] = new OrderAttribute("sales", OrderAttribute.DOWN);
// ms.runOrdering(oAtt);
ms.setOrdering(ordAtt);
selVec = ms.runSelections(selAtt);
System.out.println("Selected stream: " + ms);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -