org.openprovenance.prov.validation.Validate Maven / Gradle / Ivy
The newest version!
package org.openprovenance.prov.validation;
import java.util.*;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.openprovenance.prov.configuration.Configuration;
import org.openprovenance.prov.model.Document;
import org.openprovenance.prov.model.Entity;
import org.openprovenance.prov.model.Name;
import org.openprovenance.prov.model.Bundle;
import org.openprovenance.prov.model.Namespace;
import org.openprovenance.prov.model.QualifiedName;
import org.openprovenance.prov.model.SpecializationOf;
import org.openprovenance.prov.model.Statement;
import org.openprovenance.prov.model.StatementOrBundle;
import org.openprovenance.prov.model.ProvFactory;
import org.openprovenance.prov.model.ProvUtilities;
import org.openprovenance.prov.validation.matrix.SparseMatrix;
import org.openprovenance.prov.validation.report.Dependencies;
import org.openprovenance.prov.validation.report.MalformedStatements;
import org.openprovenance.prov.validation.report.MergeReport;
import org.openprovenance.prov.validation.report.SpecializationReport;
import org.openprovenance.prov.validation.report.TypeOverlap;
import org.openprovenance.prov.validation.report.ValidationReport;
public class Validate {
static Logger logger = LogManager.getLogger(Validate.class);
private static String fileName = "config.properties";
public static final String validatorVersion = getPropertiesFromClasspath(fileName).getProperty("validator.version");
public static final String longValidatorVersion = validatorVersion + " (" + getPropertiesFromClasspath(fileName).getProperty("timestamp") + ")";
public static final String longVersion = "Validator " + longValidatorVersion + ", ProvToolbox " + Configuration.longToolboxVersion;
private static Properties getPropertiesFromClasspath(String propFileName) {
return Configuration.getPropertiesFromClasspath(Validate.class, propFileName);
}
ProvUtilities u = new ProvUtilities();
final Config config;
public final Indexer ind;
final Expansion expa;
public final Uniqueness uniq;
final Unification unif;
final Inference inf;
final public EventIndexer evtIdx;
final public Types typeChecker;
public Constraints constraints;
final ProvFactory p;
final Name name;
final Gensym g;
public Validate(Config config) {
this.config = config;
this.ind = new Indexer(config.p, config.om);
this.p=ind.p;
this.name=p.getName();
this.g=ind.g;
this.typeChecker=new Types(ind,u,config);
this.expa = new Expansion(ind,config,typeChecker);
this.uniq = new Uniqueness(ind);
this.unif = new Unification(ind, uniq);
this.inf = new Inference(ind,typeChecker);
this.evtIdx = new EventIndexer(ind);
}
public Indexer getIndexer() {
return ind;
}
public Constraints getConstraints() {
return constraints;
}
public EventIndexer getEventIndexer() {
return evtIdx;
}
public Inference getInference() {
return inf;
}
public ValidationReport validate(Document b) throws java.io.IOException {
Pair res=validate(u.getStatement(b),true);
ValidationReport report=res.car;
Document doc = res.cdr;
for (Bundle bu: u.getNamedBundle(b)) {
List statments= bu.getStatement();
Validate validator=new Validate(config);
Pair buRes=validator.validate(statments,false);
ValidationReport buRep=buRes.car;
buRep.setId(bu.getId());
report.getValidationReport().add(buRep);
Bundle bundle=u.getNamedBundle(buRes.cdr).get(0);
List ll=doc.getStatementOrBundle();
ll.add(bundle);
// add a bit of provenance about the new bundle
// state that it was derived from the input bundle
Entity e=p.newEntity(bundle.getId());
e.getType().add(p.newType(name.PROV_BUNDLE,name.PROV_QUALIFIED_NAME));
ll.add(e);
ll.add(p.newWasDerivedFrom((QualifiedName)null, bundle.getId(), bu.getId()));
// chain namespaces
bundle.getNamespace().setParent(doc.getNamespace());
}
Namespace.withThreadNamespace(doc.getNamespace());
new Cleanup(u).cleanup(config, u.getStatement(doc), u.getNamedBundle(doc));
return report;
}
public Pair validate(List statements, boolean isDocument)
throws
java.io.IOException {
//ProvSerialiser serial = ProvSerialiser.getThreadProvSerialiser();
ValidationReport report;
logger.debug("Expanding terms");
expa.expansion(config, statements);
// ValidationReport report2 = constraints.getReport();
// serial.serialiseValidationReport(new File(reportFile + "index2"),
// report2, true);
logger.debug("Indexing bundle");
ind.index(statements);
//System.out.println("********** found entities " + u.getEntity(b));
logger.debug("... Indexing complete");
logger.debug("Applying uniqueness constraints");
ind.merger.updated = true; // always enter the loop once
int iteration = 0;
while (ind.merger.updated == true) {
logger.debug("Applying uniqueness constraints: iteration "
+ iteration++);
ind.merger.updated = false;
unif.applyUnification();
uniq.uniqueAll(config);
}
logger.debug("Substitution is " + ind.merger.unificationSubstitution);
logger.debug("Unique startTable is " + uniq.wasStartedByTable);
unif.removeUnifiedEntries();
unif.applyUnification();
typeChecker.addDeclaredTypes();
// comp.applyUnification();
inf.inferIntervalBeginningAndEnds(config);
inf.computeSpecializationClosure(config);
inf.computeAlternateClosure(config);
inf.specializationAttributesInference(config);
logger.debug("spec table " + inf.specializationTable);
inf.specializationIsNotReflexive(config);
logger.debug("Save normal form");
// logger.info(inf.summary());
Document doc = completeDocument();
//if (out!=null) serial.serialiseDocument(new File(out), doc, true);
logger.debug("Event index");
evtIdx.createEventIndex();
logger.debug("... event index complete");
logger.debug("Constraints");
logger.debug("constraint checking");
this.constraints = new Constraints(typeChecker, ind, inf, evtIdx);
constraints.constraints(config);
logger.debug("... constraint checking complete");
//logger.debug("matrix is: \n" + constraints.getMatrix().displayMatrix2());
logger.debug("maximum is "
+ constraints.getMatrix().getMaximum());
List