org.openprovenance.prov.notation.TreeTraversal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov-n Show documentation
Show all versions of prov-n Show documentation
A Parser for PROV-N Provenance Notation.
package org.openprovenance.prov.notation;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import javax.xml.datatype.XMLGregorianCalendar;
import org.antlr.runtime.tree.Tree;
import org.antlr.runtime.tree.CommonTree;
import org.openprovenance.prov.model.*;
/* Class to traverse the syntax tree generated by antl parser, invoking constructor methods in TreeConstructor interface. */
public class TreeTraversal {
final private ModelConstructor c;
final private ProvFactory pFactory;
final private Name name;
final private ValueConverter vconv;
private final DateTimeOption dateTimeOption;
private final TimeZone optionalTimeZone;
public TreeTraversal(ModelConstructor c, ProvFactory pFactory, DateTimeOption dateTimeOption, TimeZone optionalTimeZone) {
this.c=c;
this.pFactory=pFactory;
this.name=pFactory.getName();
this.namespace=pFactory.newNamespace();
this.namespace.addKnownNamespaces();
this.dateTimeOption = dateTimeOption;
this.optionalTimeZone = optionalTimeZone;
this.vconv=new ValueConverter(pFactory);
}
public String getTokenString(Tree t) {
if (t==null) return null;
if (((CommonTree)t).getToken()==null) return null;
return ((CommonTree)t).getToken().getText();
}
public String convertToken(String token) {
return token;
}
public int convertInt(String token) {
return Integer.valueOf(token);
}
public String stripAmpersand(String token) {
return token.substring(1);
}
public String unquoteTwice(String token) {
return token.substring(2,token.length()-2);
}
public String unquoteTrice(String token) {
return token.substring(3,token.length()-3);
}
public Object convert(Tree ast) {
switch(ast.getType()) {
/* Component 1 */
case PROV_NParser.ENTITY:
QualifiedName id=(QualifiedName)convert(ast.getChild(0));
List eAttrs=(List) convert(ast.getChild(1));
return c.newEntity(id,eAttrs);
case PROV_NParser.ACTIVITY:
id=(QualifiedName)convert(ast.getChild(0));
XMLGregorianCalendar startTime=(XMLGregorianCalendar)convert(ast.getChild(1));
XMLGregorianCalendar endTime=(XMLGregorianCalendar)convert(ast.getChild(2));
List aAttrs=(List) convert(ast.getChild(3));
return c.newActivity(id,startTime,endTime,aAttrs);
case PROV_NParser.START:
case PROV_NParser.END:
if (ast.getChildCount()==0) return null;
if (ast.getChild(0)==null) return null;
return convert (ast.getChild(0));
case PROV_NParser.USED:
Tree uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
QualifiedName uid=(QualifiedName) convert(uidTree);
QualifiedName id2=(QualifiedName) convert(ast.getChild(1));
QualifiedName id1=(QualifiedName) convert(ast.getChild(2));
XMLGregorianCalendar time=(XMLGregorianCalendar) convert(ast.getChild(3));
List rAttrs=(List) convert(ast.getChild(4));
return c.newUsed(uid, id2, id1,time,rAttrs);
case PROV_NParser.WGB:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName) convert(uidTree);
id2=(QualifiedName) convert(ast.getChild(1));
id1=(QualifiedName) convert(ast.getChild(2));
time=(XMLGregorianCalendar) convert(ast.getChild(3));
rAttrs=(List) convert(ast.getChild(4));
return c.newWasGeneratedBy(uid,id2,id1,time,rAttrs);
case PROV_NParser.WSB:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
QualifiedName id3=(QualifiedName)convert(ast.getChild(3));
time=(XMLGregorianCalendar) convert(ast.getChild(4));
rAttrs=(List) convert(ast.getChild(5));
return c.newWasStartedBy(uid,id2,id1,id3,time,rAttrs);
case PROV_NParser.WEB:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
id3=(QualifiedName) convert(ast.getChild(3));
time=(XMLGregorianCalendar) convert(ast.getChild(4));
rAttrs=(List) convert(ast.getChild(5));
return c.newWasEndedBy(uid,id2,id1,id3,time,rAttrs);
case PROV_NParser.TIME:
if (ast.getChildCount()==0) return null;
if (ast.getChild(0)==null) return null;
return pFactory.newISOTime(getTokenString(ast.getChild(0)), dateTimeOption, optionalTimeZone);
case PROV_NParser.WINVB:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
time=(XMLGregorianCalendar) convert(ast.getChild(3));
rAttrs=(List) convert(ast.getChild(4));
return c.newWasInvalidatedBy(uid,id2,id1,time,rAttrs);
case PROV_NParser.WIB:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
rAttrs=(List) convert(ast.getChild(3));
return c.newWasInformedBy(uid,id2,id1,rAttrs);
/* Component 2 */
case PROV_NParser.AGENT:
id=(QualifiedName) convert(ast.getChild(0));
List agAttrs=(List) convert(ast.getChild(1));
return c.newAgent(id,agAttrs);
case PROV_NParser.WAT:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
rAttrs=(List) convert(ast.getChild(3));
return c.newWasAttributedTo(uid,id2,id1,rAttrs);
case PROV_NParser.WAW:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName) ((ast.getChild(2)==null)?null : convert(ast.getChild(2)));
QualifiedName pl=(QualifiedName) ((ast.getChild(3)==null)?null : convert(ast.getChild(3)));
rAttrs=(List) convert(ast.getChild(4));
return c.newWasAssociatedWith(uid,id2,id1,pl,rAttrs);
case PROV_NParser.AOBO:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
QualifiedName a=(QualifiedName) ((ast.getChild(3)==null)?null : convert(ast.getChild(3)));
rAttrs=(List) convert(ast.getChild(4));
return c.newActedOnBehalfOf(uid,id2,id1,a,rAttrs);
/* Component 3 */
case PROV_NParser.WDF:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
QualifiedName pe=(QualifiedName) convert(ast.getChild(3));
QualifiedName q2=(QualifiedName) convert(ast.getChild(4));
QualifiedName q1=(QualifiedName) convert(ast.getChild(5));
List dAttrs=(List) convert(ast.getChild(6));
return c.newWasDerivedFrom(uid,id2,id1,pe,q2,q1,dAttrs);
/*
case PROV_NParser.WRO:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
pe=convert(ast.getChild(3));
q2=convert(ast.getChild(4));
q1=convert(ast.getChild(5));
dAttrs=convert(ast.getChild(6));
return c.newWasRevisionOf(uid,id2,id1,pe,q2,q1,dAttrs);
case PROV_NParser.WQF:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
pe=convert(ast.getChild(3));
q2=convert(ast.getChild(4));
q1=convert(ast.getChild(5));
dAttrs=convert(ast.getChild(6));
return c.newWasQuotedFrom(uid,id2,id1,pe,q2,q1,dAttrs);
case PROV_NParser.PRIMARYSOURCE:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
pe=convert(ast.getChild(3));
q2=convert(ast.getChild(4));
q1=convert(ast.getChild(5));
dAttrs=convert(ast.getChild(6));
return c.newHadPrimarySource(uid,id2,id1,pe,q2,q1,dAttrs);
*/
case PROV_NParser.INFL:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
dAttrs=(List) convert(ast.getChild(3));
return c.newWasInfluencedBy(uid,id2,id1,dAttrs);
/* Component 4 */
/* Component 5 */
case PROV_NParser.ALTERNATE:
id2=(QualifiedName)convert(ast.getChild(0));
id1=(QualifiedName)convert(ast.getChild(1));
return c.newAlternateOf(id2,id1);
case PROV_NParser.SPECIALIZATION:
id2=(QualifiedName)convert(ast.getChild(0));
id1=(QualifiedName)convert(ast.getChild(1));
return c.newSpecializationOf(id2,id1);
case PROV_NParser.MEM:
id2=(QualifiedName)convert(ast.getChild(0));
id1=(QualifiedName)convert(ast.getChild(1));
List ll=new LinkedList();
if (id1!=null) ll.add(id1);
return c.newHadMember(id2,ll);
case PROV_NParser.CTX:
QualifiedName su=(QualifiedName)convert(ast.getChild(0));
QualifiedName bu=(QualifiedName)convert(ast.getChild(1));
QualifiedName ta=(QualifiedName)convert(ast.getChild(2));
return c.newMentionOf(su,bu,ta);
/* Component 6 */
case PROV_NParser.DBIF:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
List keymap=(List)convert(ast.getChild(3));
dAttrs=(List) convert(ast.getChild(4));
return c.newDerivedByInsertionFrom(uid,id2,id1,keymap,dAttrs);
case PROV_NParser.DBRF:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
id1=(QualifiedName)convert(ast.getChild(2));
Object keyset=convert(ast.getChild(3));
dAttrs=(List)convert(ast.getChild(4));
return c.newDerivedByRemovalFrom(uid,id2,id1,(List)keyset,dAttrs);
case PROV_NParser.DMEM:
//uidTree=ast.getChild(0);
//if (uidTree.getChildCount()>0) {
// uidTree=uidTree.getChild(0);
//}
//uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
keymap=(List) convert(ast.getChild(2));
//dAttrs=(List) convert(ast.getChild(4));
return c.newDictionaryMembership(id2,keymap);
case PROV_NParser.CMEM:
uidTree=ast.getChild(0);
if (uidTree.getChildCount()>0) {
uidTree=uidTree.getChild(0);
}
uid=(QualifiedName)convert(uidTree);
id2=(QualifiedName)convert(ast.getChild(1));
Object cmemEntities=convert(ast.getChild(2));
//complete=convert(ast.getChild(3));
dAttrs=(List) convert(ast.getChild(4));
//return c.newCollectionMemberOf(uid,id2,cmemEntities,complete,dAttrs);
return null;
case PROV_NParser.KEYS:
List keys=new LinkedList();
for (int i=0; i< ast.getChildCount(); i++) {
Object o=convert(ast.getChild(i));
if (o instanceof LangString) {
keys.add(pFactory.newKey(o, name.PROV_LANG_STRING));
} else {
Object [] pair=(Object[]) o;
keys.add(pFactory.newKey(pair[0], (QualifiedName)pair[1]));
}
}
return keys;
case PROV_NParser.VALUES:
List