timeBench.action.analytical.MultiPredicatePatternDiscovery Maven / Gradle / Ivy
package timeBench.action.analytical;
import ieg.prefuse.data.ParentChildNode;
import java.util.ArrayList;
import java.util.Hashtable;
import prefuse.action.Action;
import prefuse.data.expression.BinaryExpression;
import prefuse.data.expression.CompositePredicate;
import prefuse.data.expression.Expression;
import prefuse.data.expression.NotPredicate;
import prefuse.data.expression.Predicate;
import timeBench.calendar.JavaDateCalendarManager;
import timeBench.data.TemporalDataException;
import timeBench.data.TemporalDataset;
import timeBench.data.TemporalDatasetProvider;
import timeBench.data.TemporalElement;
import timeBench.data.TemporalObject;
import timeBench.data.expression.TemporalElementArrayExpression;
/**
*
*
*
* Added: / TL
* Modifications:
*
*
* @author Tim Lammarsch
*
*/
public class MultiPredicatePatternDiscovery extends Action implements TemporalDatasetProvider {
TemporalDataset sourceDataset;
TemporalDataset resultDataset;
TemporalDataset eventDataset;
Predicate[] predicates; // Bertone: Intervals
int coherenceSettings;
Hashtable sourceToResult = new Hashtable();
ArrayList resultRoots = new ArrayList();
public static int ONLY_COHERENT = 0;
public static int SPACING_ALLOWED = 1;
public static int OVERLAP_ALLOWED = 2;
public static int SPACING_OVERLAP_ALLOWED = 3; // 1&2
public static String predicateColumn = "_predicate";
public MultiPredicatePatternDiscovery(TemporalDataset sourceDataset,TemporalDataset eventDataset,Predicate[] predicates) {
this(sourceDataset,eventDataset,predicates,ONLY_COHERENT);
}
public MultiPredicatePatternDiscovery(TemporalDataset sourceDataset,TemporalDataset eventDataset,Predicate[] predicates, int coherenceSettings) {
this.sourceDataset = sourceDataset;
this.eventDataset = eventDataset;
this.predicates = predicates;
this.coherenceSettings = coherenceSettings;
}
/* (non-Javadoc)
* @see prefuse.action.Action#run(double)
*/
@Override
public void run(double frac) {
try {
resultDataset = new TemporalDataset(sourceDataset.getDataColumnSchema());
if (sourceDataset.getNodeTable().getColumnNumber(ParentChildNode.DEPTH) == -1) {
sourceDataset.getNodeTable().addColumn(ParentChildNode.DEPTH, int.class, 0);
String[] andc = sourceDataset.getAdditionalNonDataColums();
String[] andc2 = new String[andc.length+1];
for(int i=0; i checkList = new ArrayList();
// check if this combination is to be added
check(checkList,iSource,iEvent,i);
}
}
}
} else {
// for all patterns of existing length (might be events)
for (TemporalObject iSource : sourceDataset.roots()) {
// and for all predicates
for (int i=0; i checkList = new ArrayList();
// check if this combination is to be added
check(checkList,iSource,iEvent,i);
}
}
}
}
for (Long rootId : resultRoots) {
resultDataset.getTemporalObject(rootId).setRoot(true);
}
} catch (TemporalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void check(ArrayList checkList,TemporalObject currentObject,TemporalObject newObject,int predicate) throws TemporalDataException {
// recursively build list of existing pattern
checkList.add(currentObject);
if(currentObject.getChildCount() > 0) {
for(TemporalObject iChild : currentObject.childObjects()) {
check((ArrayList)checkList.clone(),iChild,newObject,predicate);
}
} else {
// if this is an acceptable pattern
if (satisfies(checkList,newObject,predicates[predicate])) {
// add new event
checkList.add(newObject);
resultDataset.setDepth(checkList.size());
// check whether a pattern based on the same pattern exists in result
for(int i=0; i0) {
int rowNumber = resultDataset.getTemporalObject(sourceToResult.get(checkList.get(i-1).getId())).linkWithChild(newReference).getRow();
resultDataset.getEdgeTable().setLong(rowNumber, predicateColumn,
sourceDataset.getEdge(checkList.get(i),checkList.get(i-1)).getLong(predicateColumn));
}
}
}
// add new event to pattern
TemporalObject newReference = resultDataset.addCloneOf(newObject);
// Set depth according to position in pattern
newReference.setInt(ParentChildNode.DEPTH, checkList.size()-1);
int rowNumber = resultDataset.getTemporalObject(sourceToResult.get(checkList.get(checkList.size()-2).getId())).linkWithChild(newReference).getRow();
resultDataset.getEdgeTable().setLong(rowNumber, predicateColumn, predicate);
}
}
}
/* (non-Javadoc)
* @see timeBench.data.TemporalDatasetProvider#getTemporalDataset()
*/
@Override
public TemporalDataset getTemporalDataset() {
return resultDataset;
}
/**
* @param iSource
* @param temporalObject
* @param instant
* @return
* @throws TemporalDataException
*/
private boolean satisfies(ArrayList checkedObjects, TemporalObject newObject, Predicate template)
throws TemporalDataException {
if (checkedObjects.size() > 0) {
// check for coherence
if ( coherenceSettings != SPACING_OVERLAP_ALLOWED) {
// check only last against others; assume this is called once for each new TemporalObject
TemporalElement last = newObject.getTemporalElement();
if ( (coherenceSettings & SPACING_ALLOWED) == 0) {
if (last.getFirstInstant().getInf() - checkedObjects.get(checkedObjects.size()-1).getTemporalElement().getLastInstant().getSup() > 1)
return false;
}
if ( (coherenceSettings & OVERLAP_ALLOWED) == 0) {
for(int i=0; i= last.getFirstInstant().getInf())
return false;
}
}
}
}
updateTemporalElementExpressions(template,checkedObjects);
return template.getBoolean(newObject);
}
/**
* @param template
* @param checkedObjects
*/
private void updateTemporalElementExpressions(Expression template,
ArrayList checkedObjects) {
if(template instanceof TemporalElementArrayExpression)
((TemporalElementArrayExpression)template).updateBuffer(checkedObjects);
else if (template instanceof NotPredicate) {
updateTemporalElementExpressions((Expression)((NotPredicate)template).getPredicate(),
checkedObjects);
} else if (template instanceof BinaryExpression) {
updateTemporalElementExpressions(((BinaryExpression)template).getLeftExpression(), checkedObjects);
updateTemporalElementExpressions(((BinaryExpression)template).getRightExpression(), checkedObjects);
} else if(template instanceof CompositePredicate) {
CompositePredicate composite = (CompositePredicate)template;
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy