📄 olapengine.java
字号:
// ----------------- Methods of JOLAP connection interface ------------------
public void close() throws OLAPException {
}
public ConnectionMetaData getMetaData() throws OLAPException {
throw new UnsupportedOperationException();
}
public javax.jmi.reflect.RefPackage getTopLevelPackage() throws OLAPException {
return this;
}
public List getSchemas() {
ArrayList schemas = new ArrayList();
schemas.add(schema);
return schemas;
}
public javax.olap.metadata.Schema getCurrentSchema() {
return schema;
}
public void setCurrentSchema(javax.olap.metadata.Schema schema) {
this.schema = schema;
this.init = false;
}
public List getDimensions() throws OLAPException {
return (List) schema.getDimension();
}
public List getCubes() throws OLAPException {
return (List) schema.getCube();
}
public javax.olap.query.querycoremodel.CubeView createCubeView() throws OLAPException {
return createCubeView(null);
}
public javax.olap.query.querycoremodel.CubeView createCubeView(javax.olap.metadata.Cube cube) throws OLAPException {
return new CubeView(this, (Cube) cube);
}
public javax.olap.query.querycoremodel.DimensionView createDimensionView() throws OLAPException {
return new DimensionView();
}
public javax.olap.query.querycoremodel.EdgeView createEdgeView() throws OLAPException {
return new EdgeView();
}
public javax.olap.query.querycoremodel.MeasureView createMeasureView() throws OLAPException {
return new MeasureView();
}
// -----------------------------------------------------------------------
// OLAP-specific methods
// -----------------------------------------------------------------------
/**
* Initialization of the OLAP engine. Must be carried out before queries
* can be executed.
*
* @throws OLAPException cannot initialize OLAP engine
*/
public void init() throws OLAPException {
if (schema == null)
throw new OLAPException("no schema defined");
try {
// Create categorical attributes, if required:
MiningInputStream inputStreamTrans = createCategoricalAttributes();
// Create multidimensional stream:
multiStream = new MultidimensionalStream(inputStreamTrans);
// Init multidimensional stream:
multiStream.readMultidimensionalStreamData();
multiMetaData = multiStream.getMetaData();
// Determine all category hierarchies:
buildHierarchies();
}
catch (MiningException ex) {
throw new OLAPException( ex.toString() );
}
init = true;
}
/** Prefix of numeric attributes transformed into categorical ones. */
public static final String CATEG_PREFIX = "cat_";
/**
* Create transformations which transform numeric in categorical attributes.
*
* @return transformation which creates all required categorical attributes
* @throws OLAPException OLAP exception
* @throws MiningException Transformation exception
*/
private MiningInputStream createCategoricalAttributes() throws OLAPException, MiningException {
// Find all numeric attributes that need to be transformed:
Vector numAttNames = new Vector();
Collection dimensions = schema.getDimension();
Iterator it = dimensions.iterator();
for (int i = 0; i < dimensions.size(); i++) {
Dimension dimension = (Dimension) it.next();
if (dimension instanceof MeasureDimension)
continue;
for (int j = 0; j < dimension.getNumericAttributeNames().size(); j++) {
String name = (String) dimension.getNumericAttributeNames().elementAt(j);
if ( numAttNames.indexOf(name) == -1)
numAttNames.addElement(name);
}
}
int nNumAtt = numAttNames.size();
if (nNumAtt == 0) {
// If input stream does not support 'move', then to mining array stream:
Enumeration suppinp = inputStream.getSupportedStreamMethods();
boolean allowsMove = false;
while ( suppinp.hasMoreElements() ) {
String meth = (String) suppinp.nextElement();
if ( meth.equals("move") )
allowsMove = true;
}
if (!allowsMove) {
MiningArrayStream mas = new MiningArrayStream(inputStream);
inputStream = mas;
}
return inputStream;
}
// Define transformation:
MiningTransformationFactory mtf = new MiningTransformationFactory();
for (int i = 0; i < nNumAtt; i++) {
String numAttName = (String) numAttNames.elementAt(i);
Categorization cat = new Categorization();
cat.setSourceName( numAttName );
cat.setTargetName( CATEG_PREFIX + numAttName );
cat.setRemoveSourceAttribute(false);
mtf.addOneToOneMapping(cat);
};
MiningTransformationStep mts = mtf.createMiningTransformationStep();
// Run transformation and write to mining array stream:
MiningFilterStream mfs = new MiningFilterStream(inputStream, mts);
MiningArrayStream mas = new MiningArrayStream(mfs);
MiningDataSpecification transMetaData = mas.getMetaData();
// Include all generated categorical attributes in the dimensions:
it = dimensions.iterator();
for (int i = 0; i < dimensions.size(); i++) {
Dimension dimension = (Dimension) it.next();
if (dimension instanceof MeasureDimension)
continue;
if (dimension.getNumericAttributeNames().size() == 0)
continue;
int nAtt = dimension.getFeatureSize();
Vector dimensionAttributes = new Vector();
for (int j = 0; j < nAtt; j++) {
org.omg.cwm.objectmodel.core.Feature att = dimension.getFeature(j);
if ( !(att instanceof MiningAttribute) )
continue;
MiningAttribute mAtt = (MiningAttribute) att;
if (mAtt instanceof CategoricalAttribute)
dimensionAttributes.addElement(mAtt);
else {
String tAttName = CATEG_PREFIX + mAtt.getName();
MiningAttribute tAtt = transMetaData.getMiningAttribute(tAttName);
if (tAtt == null)
throw new OLAPException("transformed attribute '" + tAttName + "' not found");
if ( !(tAtt instanceof CategoricalAttribute) )
throw new OLAPException("transformed attribute '" + tAttName + "' must be categorical");
dimensionAttributes.addElement(tAtt);
}
};
dimension.setDimensionAttributes(dimensionAttributes);
for (int j = 0; j < dimensionAttributes.size(); j++)
System.out.print("-----" + dimensionAttributes.elementAt(j));
System.out.println();
}
return mas;
}
/**
* Build hierarchy objects of hierarchical dimensions.
*
* @throws OLAPException OLAP exception
* @throws MiningException Mining exception
*/
private void buildHierarchies() throws OLAPException, MiningException {
// Find hierarchical dimensions:
Vector hierDimensions = new Vector();
Collection dimensions = schema.getDimension();
Iterator it = dimensions.iterator();
for (int i = 0; i < dimensions.size(); i++) {
javax.olap.metadata.Dimension dimension = (javax.olap.metadata.Dimension) it.next();
if (dimension instanceof HierarchicalDimension)
hierDimensions.addElement(dimension);
}
int nHierDim = hierDimensions.size();
if (nHierDim == 0)
return;
// Get index arrays:
IntVector[] hierDims = new IntVector[nHierDim];
CategoryHierarchy[] cah = new CategoryHierarchy[nHierDim];
for (int i = 0; i < nHierDim; i++) {
HierarchicalDimension hierDim = (HierarchicalDimension) hierDimensions.elementAt(i);
hierDims[i] = new IntVector();
int nlev = hierDim.getNumberOfLevels();
for (int j = 0; j < nlev; j++) {
CategoricalAttribute ca = hierDim.getLevelAttribute(j);
int ind = multiMetaData.getAttributeIndex(ca);
if (ind < -1)
throw new OLAPException("invalid attribute '" + ca.getName()
+ "' in hierarchical dimension '" + hierDim.getName() + "'");
hierDims[i].addElement(ind);
};
cah[i] = hierDim.getCatHierarchy();
};
// Build relationships:
multiStream.reset();
while ( multiStream.next() ) {
MiningVector mv = multiStream.read();
for (int i = 0; i < nHierDim; i++) {
for (int j = 0; j < hierDims[i].size()-1; j++) {
// Parent category:
int parInd = hierDims[i].IntegerAt(j);
CategoricalAttribute parAtt = (CategoricalAttribute) multiMetaData.getMiningAttribute(parInd);
double key = mv.getValue(parInd);
Category parcat = parAtt.getCategory(key);
// Child category:
int childInd = hierDims[i].IntegerAt(j+1);
CategoricalAttribute childAtt = (CategoricalAttribute) multiMetaData.getMiningAttribute(childInd);
key = mv.getValue(childInd);
Category childcat = childAtt.getCategory(key);
// Add to hierarchy:
cah[i].addRelationship(parcat, childcat);
}
}
};
for (int i = 0; i < nHierDim; i++)
cah[i].printRelationships();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -