📄 categoricalattributeoperations.java
字号:
}
// -----------------------------------------------------------------------
// Comparison operations
// -----------------------------------------------------------------------
/**
* Tests if the given and this categorical attribute are both not
* of unboundedCategories type (and thus also not of unstoredCategories
* type) because this would make them incompatible with respect
* to the basis transformations and comparisons of this class. <p>
*
* Of course, all basis transformations and comparisons can technically
* also be applied to categorical attributes of unbounded type and
* will work correct with respect to their bases. However, this could
* lead to serious interpretation problems.
*
* @param catAtt categorical attribute with which to compare
* @return true if both attributes are of 'default' type, false otherwise
*/
public boolean isComparableType(CategoricalAttribute catAtt) {
if (thisCatAtt.isUnboundedCategories() || catAtt.isUnboundedCategories() )
return false;
return true;
}
/**
* Indicates whether a given categorical attribute "contains" this one.
*
* @param catAtt categorical attribute with which to compare
* @return true if this categorical attribute is contained in given one, false otherwise
*/
public boolean subset(CategoricalAttribute catAtt) {
if ( thisCatAtt.getCategoriesNumber() > catAtt.getCategoriesNumber() )
return false;
for (int i = 0; i < thisCatAtt.getCategoriesNumber(); i++) {
if ( Category.isMissingValue( catAtt.getKey(thisCatAtt.getCategory(i)) ) )
return false;
}
return true;
}
/**
* Indicates whether a given categorical attribute "is contained" in this one.
*
* @param catAtt categorical attribute with which to compare
* @return true if this categorical attribute contains the given one, false otherwise
*/
public boolean superset(CategoricalAttribute catAtt) {
if ( thisCatAtt.getCategoriesNumber() < catAtt.getCategoriesNumber() )
return false;
for (int i = 0; i < catAtt.getCategoriesNumber(); i++) {
if ( Category.isMissingValue( thisCatAtt.getKey(catAtt.getCategory(i)) ) )
return false;
}
return true;
}
/**
* Indicates whether a given categorical attribute "is equal" to this one.
* Here, equal means that the given categorical attribute has the same
* number of categories which are equal to that of this one.
*
* @param catAtt categorical attribute with which to compare
* @return true if categorical attribute is equal, false otherwise
*/
public boolean equals(CategoricalAttribute catAtt) {
if ( thisCatAtt.getCategoriesNumber() != catAtt.getCategoriesNumber() )
return false;
for (int i = 0; i < thisCatAtt.getCategoriesNumber(); i++) {
if ( Category.isMissingValue( catAtt.getKey(thisCatAtt.getCategory(i)) ) )
return false;
}
return true;
}
// -----------------------------------------------------------------------
// Test
// -----------------------------------------------------------------------
/**
* Test.
*
* @param args test parameters (ignored)
*/
public static void main(String[] args)
{
try
{
// ----------------- cutlery == cutlery2 -------------------------
// Create first categorical attribute 'cutlery':
CategoricalAttribute cutlery = new CategoricalAttribute("cutlery");
cutlery.addCategory( new Category("knife") );
cutlery.addCategory( new Category("fork") );
cutlery.addCategory( new Category("spoon") );
System.out.println("attribute 'cutlery': " + cutlery);
// Get category operations object of 'cutlery':
CategoricalAttributeOperations cutleryOp = cutlery.getCatAttOp();
// Create second categorical attribute 'cutlery2':
CategoricalAttribute cutlery2 = new CategoricalAttribute("cutlery2");
cutlery2.addCategory( new Category("spoon") );
cutlery2.addCategory( new Category("knife") );
cutlery2.addCategory( new Category("fork") );
System.out.println("attribute 'cutlery2': " + cutlery2);
// Compare bases of both attributes:
System.out.println("'cutlery' == 'cutlery2': " + cutleryOp.equals(cutlery2));
System.out.println("'cutlery' subset 'cutlery2': " + cutleryOp.subset(cutlery2));
System.out.println("'cutlery' superset 'cutlery2': " + cutleryOp.superset(cutlery2));
// Transforms basis of 'cutlery2' to 'cutlery' basis:
CategoricalAttribute transCutlery2 = cutleryOp.transform(cutlery2);
System.out.println("transformed 'cutlery2': " + transCutlery2);
// Transform key 'knife' from 'cutlery2' to 'cutlery' basis:
Category knife = new Category("knife");
double key = cutlery2.getKey(knife);
double transKey = cutleryOp.transform(cutlery2, key);
System.out.println("key of 'knife' in 'cutlery2': " + key);
System.out.println("transformed key of 'knife' for 'cutlery': " + transKey);
System.out.println("real key of 'knife' in 'cutlery': " + cutlery.getKey(knife));
System.out.println();
// ----------------- cutlery < cutlery2 -------------------------
// Extend 'cutlery2' by new category 'skewer':
cutlery2.addCategory( new Category("skewer") );
System.out.println("attribute 'cutlery': " + cutlery);
System.out.println("attribute 'cutlery2': " + cutlery2);
// Compare bases of both attributes again:
System.out.println("'cutlery' == 'cutlery2': " + cutleryOp.equals(cutlery2));
System.out.println("'cutlery' subset 'cutlery2': " + cutleryOp.subset(cutlery2));
System.out.println("'cutlery' superset 'cutlery2': " + cutleryOp.superset(cutlery2));
// Transforms basis of 'cutlery2' to 'cutlery' basis:
transCutlery2 = cutleryOp.transform(cutlery2);
System.out.println("transformed 'cutlery2': " + transCutlery2);
// Transform key 'knife' from 'cutlery2' to 'cutlery' basis:
key = cutlery2.getKey(knife);
transKey = cutleryOp.transform(cutlery2, key);
System.out.println("key of 'knife' in 'cutlery2': " + key);
System.out.println("transformed key of 'knife' for 'cutlery': " + transKey);
System.out.println("real key of 'knife' in 'cutlery': " + cutlery.getKey(knife));
// Transform key 'skewer' from 'cutlery2' to 'cutlery' basis:
key = cutlery2.getKey( new Category("skewer") );
transKey = cutleryOp.transform(cutlery2, key);
System.out.println("key of 'skewer' in 'cutlery2': " + key);
System.out.println("transformed key of 'skewer' for 'cutlery': " + transKey);
System.out.println();
// ----------------- cutlery <> cutlery2 -------------------------
// Extend 'cutlery' by new category 'teespoon':
cutlery.addCategory( new Category("teespoon") );
System.out.println("attribute 'cutlery': " + cutlery);
System.out.println("attribute 'cutlery2': " + cutlery2);
// Compare bases of both attributes again:
System.out.println("'cutlery' == 'cutlery2': " + cutleryOp.equals(cutlery2));
System.out.println("'cutlery' subset 'cutlery2': " + cutleryOp.subset(cutlery2));
System.out.println("'cutlery' superset 'cutlery2': " + cutleryOp.superset(cutlery2));
// Transforms basis of 'cutlery2' to 'cutlery' basis:
transCutlery2 = cutleryOp.transform(cutlery2);
System.out.println("transformed 'cutlery2': " + transCutlery2);
// Transform key 'knife' from 'cutlery2' to 'cutlery' basis:
key = cutlery2.getKey(knife);
transKey = cutleryOp.transform(cutlery2, key);
System.out.println("key of 'knife' in 'cutlery2': " + key);
System.out.println("transformed key of 'knife' for 'cutlery': " + transKey);
System.out.println("real key of 'knife' in 'cutlery': " + cutlery.getKey(knife));
// Transform key 'skewer' from 'cutlery2' to 'cutlery' basis:
key = cutlery2.getKey( new Category("skewer") );
transKey = cutleryOp.transform(cutlery2, key);
System.out.println("key of 'skewer' in 'cutlery2': " + key);
System.out.println("transformed key of 'skewer' for 'cutlery': " + transKey);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -