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

org.openprovenance.prov.template.Groupings Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package org.openprovenance.prov.template;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.openprovenance.prov.model.Document;
import org.openprovenance.prov.model.HasOther;
import org.openprovenance.prov.model.Identifiable;
import org.openprovenance.prov.model.Bundle;
import org.openprovenance.prov.model.Other;
import org.openprovenance.prov.model.ProvFactory;
import org.openprovenance.prov.model.ProvUtilities;
import org.openprovenance.prov.model.QualifiedName;
import org.openprovenance.prov.model.Statement;

import static org.openprovenance.prov.template.Expand.LINKED_URI;;

public class Groupings {
    final private List> variables;
    
    
        static ProvUtilities u= new ProvUtilities();

    
    public Groupings() {
	variables=new LinkedList>();
    }
    
    public List get(int group) {
	return variables.get(group);
    }
    
    public int size() {
	return variables.size();
    }
   
    public void addVariable(QualifiedName name) {
	List ll=new LinkedList();
	ll.add(name);
	variables.add(ll);
    }

    
    public void addVariable(int group, QualifiedName name) {
	List v=variables.get(group);
	v.add(name);
    }
    
    
 
    
    @Override
    public String toString () {
	return "" + variables;
    }
    
    static public Groupings fromDocument(Document doc) {
	Hashtable> linked=new Hashtable>();
	Hashtable linkedGroups=new Hashtable();
		
	Bundle bun=u.getBundle(doc).get(0);
	Groupings grps=new Groupings();
	Set allVars=new HashSet();
	for (Statement statement: bun.getStatement()) {
	    Set vars=Expand.freeVariables(statement);
	    allVars.addAll(vars);
	    if (statement instanceof HasOther) {
		HasOther stmt2=(HasOther)statement;
		for (Other other: stmt2.getOther()) {
		    if (LINKED_URI.equals(other.getElementName().getUri())) {
			QualifiedName id=((Identifiable)statement).getId();
			QualifiedName otherId=(QualifiedName) other.getValue();
			addEntry(linked, otherId, id);
			addEntry(linked, id, otherId);
		    }
		}
	    }
	}
	QualifiedName [] sorted=allVars.toArray(new QualifiedName[0]);
	Arrays.sort(sorted, new Comparator() {
	    @Override
	    public int compare(QualifiedName arg0, QualifiedName arg1) {
		return arg0.getUri().compareTo(arg1.getUri());
	    }
	    
	});;
	
	int currentGroup=0;	
	for (QualifiedName qn: sorted) {
	    Set links=linked.get(qn);
	    if (links==null || links.isEmpty()) {
		grps.addVariable(qn);
	    } else {
		Integer aGroup=linkedGroups.get(qn);
		if (aGroup!=null) {
		    grps.addVariable(aGroup,qn);
		} else {
		    grps.addVariable(qn);
		    for (QualifiedName otherQn: links) {
			linkedGroups.put(otherQn,currentGroup);
		    }
		}	
	    }
	    currentGroup++;
	}
	
	return grps;
	
    }

    static  void addEntry(Hashtable> linked,
			  QualifiedName id, QualifiedName otherId) {
	if (linked.get(otherId)==null) {
	    linked.put(otherId,new HashSet());
	}
	linked.get(otherId).add(id);
    }
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy