All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openprovenance.prov.validation.Indexer Maven / Gradle / Ivy

The newest version!
package org.openprovenance.prov.validation;

import java.util.HashSet;
import java.util.List;
import java.util.Hashtable;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Set;

import org.openprovenance.prov.model.*;
import org.openprovenance.prov.model.extension.QualifiedAlternateOf;
import org.openprovenance.prov.model.extension.QualifiedHadMember;
import org.openprovenance.prov.model.extension.QualifiedSpecializationOf;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Indexer {

	static Logger logger = LogManager.getLogger(Indexer.class);
	final  ProvUtilities u = new ProvUtilities();

	public final Gensym g;

	public final Namespace namespace = new Namespace();
	Merger merger;
	final ProvFactory p;



	public Indexer(ProvFactory p, ObjectMaker om) {
		this.p=p;
		this.g = new Gensym(namespace, p,om);
		registerNamespaces(namespace);
		this.merger = new Merger(p, u, qualifiedNameMismatch, this);
        mentionOfTable = new Hashtable<>();
    }

	public void registerNamespaces(Namespace namespace) {
		namespace.register(NamespacePrefixMapper.XSD_PREFIX,
				NamespacePrefixMapper.XSD_NS);
		namespace.register(Gensym.VAL_PREFIX, g.this_VAL_URI);
		namespace.registerDefault(g.this_VAL_URI);
		namespace.register("ex", "http://example.org/");
		// TODO: hard coded namespace for example!
	}

	public void index(Document doc) {
		List recs = u.getStatement(doc);
		index(recs);
	}


	public void index(Bundle bundle) {
		List recs = u.getStatement(bundle);
		index(recs);
	}

	class IndexAction implements StatementAction {


		public void doAction(Entity e) {

			String uri = e.getId().getUri();
			Entity entry = entityTable.get(uri);
			if (entry == null) {
				entry = p.newEntity(e); // create a fresh copy TODO: why?
				p.addAttributes(e, entry);
				entityTable.put(uri, entry);
			} else {
				entityTable.put(uri, resolveDuplicate(e, entry));
			}
			addTypeAttributes(e);
		}


		public void doAction(Activity a) {
			String uri = a.getId().getUri();
			Activity entry = activityTable.get(uri);
			if (entry == null) {
				activityTable.put(uri, a);
			} else {
				activityTable.put(uri, resolveDuplicate(a, entry));
			}
			addTypeAttributes(a);
		}


		public void doAction(Agent ag) {
			String uri = ag.getId().getUri();
			Agent entry = agentTable.get(uri);
			if (entry == null) {
				agentTable.put(uri, ag);
			} else {
				agentTable.put(uri, resolveDuplicate(ag, entry));
			}
			addTypeAttributes(ag);
		}


		public void doAction(WasGeneratedBy gen) {
			if (gen.getId() == null) g.setId(gen);
			String uri = gen.getId().getUri();
			WasGeneratedBy entry = wasGeneratedByTable.get(uri);
			if (entry == null) {
				wasGeneratedByTable.put(uri, gen);
			} else {
				wasGeneratedByTable.put(uri, resolveDuplicate(gen, entry));
			}
			addTypeAttributes(gen);

		}


		public void doAction(Used use) {
			if (use.getId() == null) g.setId(use);
			String uri = use.getId().getUri();
			Used entry = usedTable.get(uri);
			if (entry == null) {
				usedTable.put(uri, use);
			} else {
				usedTable.put(uri, resolveDuplicate(use, entry));
			}
			addTypeAttributes(use);

		}


		public void doAction(WasInvalidatedBy inv) {
			if (inv.getId() == null) g.setId(inv);
			String uri = inv.getId().getUri();
			WasInvalidatedBy entry = wasInvalidatedByTable.get(uri);
			if (entry == null) {
				wasInvalidatedByTable.put(uri, inv);
			} else {
				wasInvalidatedByTable.put(uri, resolveDuplicate(inv, entry));
			}
			addTypeAttributes(inv);
		}


		public void doAction(WasInformedBy inf) {
			if (inf.getId() == null) g.setId(inf);
			String uri = inf.getId().getUri();
			WasInformedBy entry = wasInformedByTable.get(uri);
			if (entry == null) {
				wasInformedByTable.put(uri, inf);
			} else {
				wasInformedByTable.put(uri, resolveDuplicate(inf, entry));
			}
			addTypeAttributes(inf);
		}


		public void doAction(WasStartedBy start) {
			if (start.getId() == null) g.setId(start);
			String uri = start.getId().getUri();
			WasStartedBy entry = wasStartedByTable.get(uri);
			if (entry == null) {
				wasStartedByTable.put(uri, start);
			} else {
				logger.debug("index(): calling resolveDuplicate  ++++++++ ");
				wasStartedByTable.put(uri, resolveDuplicate(start, entry));
			}
			addTypeAttributes(start);

		}


		public void doAction(WasEndedBy end) {
			if (end.getId() == null) g.setId(end);
			String uri = end.getId().getUri();
			WasEndedBy entry = wasEndedByTable.get(uri);
			if (entry == null) {
				wasEndedByTable.put(uri, end);
			} else {
				wasEndedByTable.put(uri, resolveDuplicate(end, entry));
			}
			addTypeAttributes(end);

		}


		public void doAction(WasDerivedFrom der) {
			if (der.getId() == null) g.setId(der);
			String uri = der.getId().getUri();
			WasDerivedFrom entry = wasDerivedFromTable.get(uri);
			if (entry == null) {
				wasDerivedFromTable.put(uri, der);
			} else {
				wasDerivedFromTable.put(uri, resolveDuplicate(der, entry));
			}
			addTypeAttributes(der);

		}

		public void doAction(WasAssociatedWith assoc) {
			if (assoc.getId() == null) g.setId(assoc);
			String uri = assoc.getId().getUri();
			WasAssociatedWith entry = wasAssociatedWithTable.get(uri);
			if (entry == null) {
				wasAssociatedWithTable.put(uri, assoc);
			} else {
				wasAssociatedWithTable.put(uri, resolveDuplicate(assoc, entry));
			}
			addTypeAttributes(assoc);
		}



		public void doAction(WasAttributedTo attr) {
			if (attr.getId() == null) g.setId(attr);
			String uri = attr.getId().getUri();
			WasAttributedTo entry = wasAttributedToTable.get(uri);
			if (entry == null) {
				wasAttributedToTable.put(uri, attr);
			} else {
				wasAttributedToTable.put(uri, resolveDuplicate(attr, entry));
			}
			addTypeAttributes(attr);

		}


		public void doAction(ActedOnBehalfOf del) {
			if (del.getId() == null) g.setId(del);
			String uri = del.getId().getUri();
			ActedOnBehalfOf entry = actedOnBehalfOfTable.get(uri);
			if (entry == null) {
				actedOnBehalfOfTable.put(uri, del);
			} else {
				actedOnBehalfOfTable.put(uri, resolveDuplicate(del, entry));
			}
			addTypeAttributes(del);

		}


		public void doAction(WasInfluencedBy infl) {
			if (infl.getId() == null) g.setId(infl);
			String uri = infl.getId().getUri();
			WasInfluencedBy entry = wasInfluencedByTable.get(uri);
			if (entry == null) {
				wasInfluencedByTable.put(uri, infl);
			} else {
				wasInfluencedByTable.put(uri, resolveDuplicate(infl, entry));
			}
			addTypeAttributes(infl);
		}


		public void doAction(AlternateOf alt) {
			alternateOfList.add(alt);
		}


		public void doAction(SpecializationOf spec) {
			specializationOfList.add(spec);

		}

		public void doAction(QualifiedAlternateOf alt) {
			alternateOfList.add(alt);
		}


		public void doAction(QualifiedSpecializationOf spec) {
			specializationOfList.add(spec);

		}

		public void doAction(MentionOf men) {
			String uri = men.getSpecificEntity().getUri();
			MentionOf entry = mentionOfTable.get(uri);
			if (entry == null) {
				mentionOfTable.put(uri, men);
			} else {
				mentionOfTable.put(uri, resolveDuplicate(men, entry));
			}
		}


		public void doAction(HadMember mem) {
			logger.debug("!!!!!!!!!!!!!!!!!!!!!adding " + mem);
			membershipList.add(mem);

		}

		public void doAction(QualifiedHadMember mem) {
			logger.debug("!!!!!!!!!!!!!!!!!!!!!adding " + mem);
			membershipList.add(mem);
		}


		public void doAction(DictionaryMembership s) {
		}


		public void doAction(DerivedByRemovalFrom s) {
		}


		public void doAction(DerivedByInsertionFrom s) {
		}


		public void doAction(Bundle s,
							 org.openprovenance.prov.model.ProvUtilities provUtilities) {
		}

	}

	public  void addTypeAttributes(T entry) {
		addTypeAttributes(getTypeTable(entry), entry);
	}

	public  Hashtable> getTypeTable(T entry) {
		Object o=u.doAction(entry, new TypeTable());
		@SuppressWarnings("unchecked")
		Hashtable> table=(Hashtable>) o;
		return table;
	}

	public class TypeTable implements StatementActionValue {

		public Object doAction(Entity e) {
			return entityTypeTable;
		}


		public Object doAction(Activity a) {
			return activityTypeTable;
		}


		public Object doAction(Agent ag) {
			return agentTypeTable;
		}


		public Object doAction(WasGeneratedBy gen) {
			return wasGeneratedByTypeTable;
		}


		public Object doAction(Used use) {
			return usedTypeTable;
		}


		public Object doAction(WasInvalidatedBy inv) {
			return wasInvalidatedByTypeTable;
		}


		public Object doAction(WasStartedBy start) {
			return wasStartedByTypeTable;
		}

		public Object doAction(WasEndedBy end) {
			return wasEndedByTypeTable;
		}


		public Object doAction(WasInformedBy inf) {
			return wasInformedByTypeTable;
		}


		public Object doAction(WasDerivedFrom der) {
			return wasDerivedFromTypeTable;
		}


		public Object doAction(WasAssociatedWith assoc) {
			return wasAssociatedWithTypeTable;
		}


		public Object doAction(WasAttributedTo attr) {
			return wasAttributedToTypeTable;
		}


		public Object doAction(ActedOnBehalfOf del) {
			return actedOnBehalfOfTypeTable;
		}


		public Object doAction(WasInfluencedBy inf) {
			return wasInfluencedByTypeTable;
		}


		public Object doAction(AlternateOf alt) {
			// no attributes
			throw new UnsupportedOperationException();
		}


		public Object doAction(MentionOf men) {
			// no attributes
			throw new UnsupportedOperationException();
		}


		public Object doAction(SpecializationOf spec) {
			// no attributes
			throw new UnsupportedOperationException();
		}

		public Object doAction(QualifiedSpecializationOf spec) {
			// no attributes
			throw new UnsupportedOperationException();
		}

		public Object doAction(QualifiedHadMember mem) {
			// no attributes
			throw new UnsupportedOperationException();
		}

		public Object doAction(QualifiedAlternateOf mem) {
			// no attributes
			throw new UnsupportedOperationException();
		}

		public Object doAction(HadMember mem) {
			throw new UnsupportedOperationException();

		}


		public Object doAction(DictionaryMembership s) {
			return null;
		}


		public Object doAction(DerivedByRemovalFrom s) {
			return null;
		}


		public Object doAction(DerivedByInsertionFrom s) {
			return null;
		}


		public Object doAction(Bundle s,
							   org.openprovenance.prov.model.ProvUtilities provUtilities) {
			return null;
		}
	}


	public void index(List recs) {
		u.forAllStatement(recs, new IndexAction());
	}

	public  void addTypeAttributes(Hashtable> typeTable,
														T e) {
		if (e instanceof Identifiable) {
			if (e instanceof HasType) {
				HasType o=(HasType)e;
				String id=((Identifiable) o).getId().getUri();
				Set entry=typeTable.get(id);
				if (entry==null) {
					entry= new HashSet<>();
					typeTable.put(id, entry);
				}
				entry.addAll(o.getType());
			}
		}
	}

	public String summary() {
		List el = new LinkedList<>(entityTable.keySet());
		Collections.sort(el);
		List agl = new LinkedList<>(agentTable.keySet());
		Collections.sort(agl);
		List al = new LinkedList<>(activityTable.keySet());
		Collections.sort(al);
		List ul = new LinkedList<>(usedTable.keySet());
		Collections.sort(ul);
		List gl = new LinkedList<>(wasGeneratedByTable.keySet());
		Collections.sort(gl);
		List il = new LinkedList<>(wasInvalidatedByTable.keySet());
		Collections.sort(il);
		List sl = new LinkedList<>(wasStartedByTable.keySet());
		Collections.sort(sl);
		List edl = new LinkedList<>(wasEndedByTable.keySet());
		Collections.sort(edl);
		List dl = new LinkedList<>(wasDerivedFromTable.keySet());
		Collections.sort(dl);
		List wibl = new LinkedList<>(wasInformedByTable.keySet());
		Collections.sort(wibl);
		List assocl = new LinkedList<>(wasAssociatedWithTable.keySet());
		Collections.sort(assocl);
		List watl = new LinkedList<>(wasAttributedToTable.keySet());
		Collections.sort(watl);
		List winflbl = new LinkedList<>(wasInfluencedByTable.keySet());
		Collections.sort(watl);
		List aobl = new LinkedList<>(actedOnBehalfOfTable.keySet());
		Collections.sort(aobl);
		return "index: \n" + " entity: " + el + "\n" + " activity: " + al
				+ "\n" + " agent: " + agl + "\n" + " used: " + ul + "\n"
				+ " wasGeneratedBy: " + gl + "\n" + " wasInvalidatedBy: " + il
				+ "\n" + " wasStartedBy: " + sl + "\n" + " wasEndedBy: " + edl
				+ "\n" + " wasInformedBy: " + wibl + "\n"
				+ " wasAssociatedWith: " + assocl + "\n" + " wasAttributedTo: "
				+ watl + "\n" + " actedOnBehalfOf: " + aobl + "\n"
				+ " wasInfluencedBy: " + winflbl + "\n" + " wasDerivedFrom: "
				+ dl;
	}

	public boolean existentialVariable(String uri) {
		return uri.startsWith(g.this_VAL_URI);
	}

	public boolean existentialVariable(QualifiedName name) {
		return (name instanceof VarQName);
		// return g.this_VAL_URI.equals(qname.getNamespaceURI());
	}



	public Hashtable entityTable = new Hashtable<>();
	public Hashtable activityTable = new Hashtable<>();
	public Hashtable agentTable = new Hashtable<>();
	public Hashtable usedTable = new Hashtable<>();
	public Hashtable wasGeneratedByTable = new Hashtable<>();
	public Hashtable wasInvalidatedByTable = new Hashtable<>();
	public Hashtable wasStartedByTable = new Hashtable<>();
	public Hashtable wasEndedByTable = new Hashtable<>();
	public Hashtable wasDerivedFromTable = new Hashtable<>();
	public Hashtable wasInformedByTable = new Hashtable<>();
	public Hashtable wasAssociatedWithTable = new Hashtable();
	public Hashtable wasAttributedToTable = new Hashtable<>();
	public Hashtable wasInfluencedByTable = new Hashtable<>();
	public Hashtable actedOnBehalfOfTable = new Hashtable<>();
	public Hashtable mentionOfTable = new Hashtable<>();

	public List alternateOfList = new LinkedList<>();
	public List specializationOfList = new LinkedList<>();
	public List membershipList= new LinkedList<>();


	public Hashtable> entityTypeTable = new Hashtable<>();
	public Hashtable> activityTypeTable = new Hashtable<>();
	public Hashtable> agentTypeTable = new Hashtable<>();
	public Hashtable> usedTypeTable = new Hashtable<>();
	public Hashtable> wasGeneratedByTypeTable = new Hashtable<>();
	public Hashtable> wasInvalidatedByTypeTable = new Hashtable<>();
	public Hashtable> wasStartedByTypeTable = new Hashtable<>();
	public Hashtable> wasEndedByTypeTable = new Hashtable<>();
	public Hashtable> wasDerivedFromTypeTable = new Hashtable<>();
	public Hashtable> wasInformedByTypeTable = new Hashtable<>();
	public Hashtable> wasAssociatedWithTypeTable = new Hashtable<>();
	public Hashtable> wasAttributedToTypeTable = new Hashtable<>();
	public Hashtable> wasInfluencedByTypeTable = new Hashtable<>();
	public Hashtable> actedOnBehalfOfTypeTable = new Hashtable<>();



	public  void putInTable(Hashtable> t,
							   Class c, Hashtable e) {
		t.put(c, e);
	}

	/**
	 * TODO: can it be: Hashtable<Class,Hashtable<String,T>>
	 * TODO use this index to make other functions polymorphic.
	 * @param  a type
	 * @return a hashtable
	 */
	public  Hashtable makeTableIndex() {
		Hashtable table = new Hashtable<>();
		table.put(Entity.class, entityTable);
		table.put(Activity.class, activityTable);

		return table;
	}

	final public Hashtable> successfulMerge = new Hashtable<>();
	final public Hashtable> failedMerge = new Hashtable<>();
	final public Hashtable> qualifiedNameMismatch = new Hashtable<>();

	/*
	 * TODO: check if resolveDuplicate for entity/activity/agent can be made
	 * generic.
	 */

	public Entity resolveDuplicate(Entity e, Entity entry) {
		String uri = e.getId().getUri();
        List l = successfulMerge.computeIfAbsent(uri, k -> new LinkedList<>());
        l.add(e);
		return p.addAttributes(e, entry);
	}

	private void showStacktrace() {
		try {
			throw new NullPointerException();
		} catch (NullPointerException npe) {
			npe.printStackTrace();
		}
	}

	public Activity resolveDuplicate(Activity a, Activity entry) {
		String uri = a.getId().getUri();
		Activity isMerged = merger.merge(a, entry);
		if (isMerged!=null) {
            List l = successfulMerge.computeIfAbsent(uri, k -> new LinkedList<>());
            l.add(a);
			return p.addAttributes(a, entry);
		} else {
			addToFailedMerge(a, entry, uri);
			return entry;
		}
	}



	public Agent resolveDuplicate(Agent ag, Agent entry) {
		String uri = ag.getId().getUri();
        List l = successfulMerge.computeIfAbsent(uri, k -> new LinkedList<>());
        l.add(ag);
		return p.addAttributes(ag, entry);
	}

	public  T resolveDuplicate(T fluen, T entry) {
		String uri = fluen.getId().getUri();
		T isMerged = merger.merge(fluen, entry);
		if (isMerged != null) {
			List l = successfulMerge.get(uri);
			if (l == null) {
				l = new LinkedList<>();
				l.add(entry);
				successfulMerge.put(uri, l);
			}
			l.add(fluen);
			return addAttributes(fluen, entry);
		} else {
			addToFailedMerge(fluen, entry, uri);
			return entry;
		}
	}


	@SuppressWarnings("unchecked")
	public  T addAttributes(T from, T to) {
		if (from instanceof Used) {
			return (T) p.addAttributes((Used) from, (Used) to);
		}
		if (from instanceof WasStartedBy) {
			return (T) p.addAttributes((WasStartedBy) from, (WasStartedBy) to);
		}
		if (from instanceof WasEndedBy) {
			return (T) p.addAttributes((WasEndedBy) from, (WasEndedBy) to);
		}
		if (from instanceof WasGeneratedBy) {
			return (T) p.addAttributes((WasGeneratedBy) from,
					(WasGeneratedBy) to);
		}
		if (from instanceof WasDerivedFrom) {
			return (T) p.addAttributes((WasDerivedFrom) from,
					(WasDerivedFrom) to);
		}
		if (from instanceof WasAssociatedWith) {
			return (T) p.addAttributes((WasAssociatedWith) from,
					(WasAssociatedWith) to);
		}
		if (from instanceof WasInvalidatedBy) {
			return (T) p.addAttributes((WasInvalidatedBy) from,
					(WasInvalidatedBy) to);
		}

		if (from instanceof WasAttributedTo) {
			return (T) p.addAttributes((WasAttributedTo) from,
					(WasAttributedTo) to);
		}
		/*
		 * if (from instanceof WasRevisionOf) { return (T)
		 * of.addAttributes((WasRevisionOf)from, (WasRevisionOf)to); } if (from
		 * instanceof AlternateOf) { return (T)
		 * of.addAttributes((AlternateOf)from, (AlternateOf)to); } if (from
		 * instanceof SpecializationOf) { return (T)
		 * of.addAttributes((SpecializationOf)from, (SpecializationOf)to); }
		 */
		if (from instanceof WasInformedBy) {
			return (T) p.addAttributes((WasInformedBy) from,
					(WasInformedBy) to);
		}
		if (from instanceof WasInfluencedBy) {
			return (T) p.addAttributes((WasInfluencedBy) from,
					(WasInfluencedBy) to);
		}
		if (from instanceof ActedOnBehalfOf) {
			return (T) p.addAttributes((ActedOnBehalfOf) from,
					(ActedOnBehalfOf) to);
		}

		/*
		 * if (from instanceof DerivedByInsertionFrom) { return T
		 * of.addAttributes((DerivedByInsertionFrom)from,
		 * (DerivedByInsertionFrom)to); }
		 */
		System.out.println("addAttributes Unknown relation " + from);
		throw new UnsupportedOperationException();
	}


	public MentionOf resolveDuplicate(MentionOf men, MentionOf entry) {
		String uri = men.getSpecificEntity().getUri();
		MentionOf isMerged = merger.merge(men, entry);
		if (isMerged != null) {
			List l = successfulMerge.get(uri);
			if (l == null) {
				l = new LinkedList<>();
				l.add(entry);
				successfulMerge.put(uri, l);
			}
			l.add(men);
			return entry;
		} else {
			addToFailedMerge(men, entry, uri);
			return entry;
		}
	}

	public void addToFailedMerge(Activity a, Activity entry, String uri) {
		List l = failedMerge.get(uri);
		if (l == null) {
			l = new LinkedList<>();
			l.add(entry);
			failedMerge.put(uri, l);
		}
		l.add(a);
	}

	public void addToFailedMerge(MentionOf a, MentionOf entry, String uri) {
		List l = failedMerge.get(uri);
		if (l == null) {
			l = new LinkedList<>();
			l.add(entry);
			failedMerge.put(uri, l);
		}
		l.add(a);
	}

	public void addToFailedMerge(Activity a, Statement entry, String uri) {
		List l = failedMerge.get(uri);
		if (l == null) {
			l = new LinkedList<>();
			l.add(entry);
			failedMerge.put(uri, l);
		}
		l.add(a);
	}

	public  void addToFailedMerge(T fluen, T entry,
													   String uri) {
		List l = failedMerge.get(uri);
		if (l == null) {
			l = new LinkedList<>();
			l.add(entry);
			failedMerge.put(uri, l);
		}
		l.add(fluen);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy