rql-parser.jj

来自「这是外国一个开源推理机」· JJ 代码 · 共 1,312 行 · 第 1/3 页

JJ
1,312
字号
}{	q = var()    { return q; }| 	q = null_value() { return q; }|	q = uri()    { return q; }|	q = domain() { return q; }|	q = range()  { return q; }|	q = subClassOf() { return q; }|	q = subPropertyOf() { return q; }|	q = typeOf() { return q; }|	q = superPropertyOf() { return q; }|	q = superClassOf() { return q; }}DataVar	null_value() :{    Token t;}{    t = <NULL>    {	// FIXME a null token is now returned as a DataVar, because there is	// no specific 'nulltoken' object in the data model. should we change	// this?	return new DataVar(t.image);    }}    void rangeslist(List selectors) :{}{	ranges_elem(selectors) ( <COMMA> ranges_elem(selectors) )*}void ranges_elem(List selectors) :{}{	// Force class_or_property_selector when next token is Class or Property:	LOOKAHEAD(1) class_or_property_selector(selectors)|	pathexpr(selectors)}void class_or_property_selector(List selectors) :{	RdfsClass rdfsClass;	ClassVar classVar;	RdfProperty rdfProperty;	PropertyVar propVar;}{	rdfsClass = rdfs_class() <LBRACE> classVar = classvar() <RBRACE>	{		ClassSelector classSel = new ClassSelector(classVar, rdfsClass);		_unboundVarList.remove(classVar);		selectors.add(classSel);	}|	rdfProperty = rdf_property() <LBRACE> propVar = propertyvar() <RBRACE>	{		PropertySelector propSel = new PropertySelector(propVar, rdfProperty);		_unboundVarList.remove(propVar);		selectors.add(propSel);	}}void pathexpr(List selectors) :{	// Vars for implicit joins between path elements	FromTo fromTo = new FromTo();}{	fromTo = first_pathelem(selectors)	(		fromTo = joined_pathelem(selectors, fromTo)	)*}/* The first path element of a row (possibly) */FromTo first_pathelem(List selectors) :{	FromTo sourceFromTo = null;	FromTo targetFromTo = null;	PropertyQuery propQ = null;	ClassQuery classQ = null;	URI uri = null;}{	(<LBRACE> sourceFromTo = from_to(selectors) <RBRACE>)?	(		propQ = property_query()	|	classQ = class_query()	|	uri = uri()	)	(<LBRACE> targetFromTo = from_to(selectors) <RBRACE>)?	{		if (classQ != null) {			if (targetFromTo == null) {				throw new ParseException("from_to part missing for " +						classQ.toString());			}			if (sourceFromTo != null) {				throw new ParseException("Not a property query: " +						classQ.toString());			}			if (targetFromTo.dataVar == null) {				if (targetFromTo.classQ == null) {					throw new ParseException("Data or class var missing for " +							classQ.toString());				}				else if (targetFromTo.classQ instanceof ClassVar) {					// Query like: classQ { : $X}					ClassSelector classSel = new						ClassSelector((ClassVar)targetFromTo.classQ, classQ);					_unboundVarList.remove(targetFromTo.classQ);					_unboundVarList.remove(classQ);					selectors.add(classSel);				}				else {					throw new ParseException("target should be class var for " +							classQ.toString());				}			}			else {				// Query like: classQ {X}				InstanceSelector insSel = new InstanceSelector(						targetFromTo.dataVar, classQ, false);				_unboundVarList.remove(targetFromTo.dataVar);				_unboundVarList.remove(classQ);				selectors.add(insSel);			}		}		else if (uri != null) {			if (sourceFromTo == null && 			    targetFromTo != null && 			    targetFromTo.classQ != null &&			    targetFromTo.classQ instanceof ClassVar)			{				// query is of the form 'uri{ : $X}' or 'uri{X : $X}'				// FIXME what if targetFromTo.classQ is not just a var?				// create a class selector constraining $X to subclasses				// of uri. FIXME currently subClassOf(X) does not return X				// itself, this is necessary here.				classQ = new SubClassOf((ClassQuery)uri, true);				ClassSelector classSel = new ClassSelector((ClassVar)targetFromTo.classQ, classQ);				_unboundVarList.remove(targetFromTo.classQ);				_unboundVarList.remove(classQ);				selectors.add(classSel);				if (targetFromTo.dataVar != null) {					InstanceSelector instSel = new InstanceSelector(targetFromTo.dataVar, targetFromTo.classQ, true);					_unboundVarList.remove(targetFromTo.dataVar);					_unboundVarList.remove(targetFromTo.classQ);					selectors.add(instSel);				}			}			else if (sourceFromTo != null ||			         targetFromTo == null ||        // implicit join using "."			         targetFromTo.dataVar == null || // schema path selector			         targetFromTo.classQ != null)    // schema path selector			{				// This is a property selector. This will be handled in				// the next if-statement				propQ = (PropertyQuery)uri;			}			else {				// Don't know what kind of selector this is, use an				// UnknownSelector				UnknownSelector unknownSel = new UnknownSelector(						uri, targetFromTo.dataVar);				_unboundVarList.remove(targetFromTo.dataVar);				selectors.add(unknownSel);			}		}		// Not 'else if' because propQ can be set later in case of uri !!!		if (propQ != null) {			// Both FromTo's are needed:			if (sourceFromTo == null) {				sourceFromTo = new FromTo();			}			if (targetFromTo == null) {				targetFromTo = new FromTo();			}			// In case just one of the FromTo's has a dataVar, the other			// should get an anonymous DataVar.			if (sourceFromTo.dataVar != null) {				if (targetFromTo.dataVar == null) {					targetFromTo.dataVar = new DataVar(getAnonymousVarName());					if (targetFromTo.classQ != null) {						// We've created a new class restriction						InstanceSelector resSel = new InstanceSelector(								targetFromTo.dataVar, targetFromTo.classQ,								true);						_unboundVarList.remove(targetFromTo.classQ);						selectors.add(resSel);					}				}			}			else { // sourceFromTo.dataVar == null				if (targetFromTo.dataVar != null) {					sourceFromTo.dataVar = new DataVar(getAnonymousVarName());					if (sourceFromTo.classQ != null) {						// We've created a new class restriction						InstanceSelector resSel = new InstanceSelector(								sourceFromTo.dataVar, sourceFromTo.classQ,								true);						_unboundVarList.remove(sourceFromTo.classQ);						selectors.add(resSel);					}				}			}			// Now both FromTo's are ensured to be complete			// Create a property selector for the property in case propQ			// is not a PropertyVar itself			PropertyVar propVar = null;			if (propQ instanceof PropertyVar) {				propVar = (PropertyVar)propQ;				// If both source and target are empty				// (e.g. {} @P {} ), we still need a				// property selector.				if (sourceFromTo.dataVar == null &&				    targetFromTo.dataVar == null) 				{				    PropertySelector propSel = 					new PropertySelector(propVar, new RdfProperty());				    _unboundVarList.remove(propVar);				    selectors.add(propSel);				}			}			else {				// Use anonymous var that can be shared with other selectors				propVar = new PropertyVar(getAnonymousVarName());				PropertySelector propSel = new PropertySelector(propVar, propQ);				_unboundVarList.remove(propQ);				selectors.add(propSel);			}			// If appropriate, create DataPathSelector			if (sourceFromTo.dataVar != null &&				targetFromTo.dataVar != null)			{				boolean extendedPropInterpretation = true;				if (propQ instanceof PropertyVar) {					extendedPropInterpretation = false;				}				DataPathSelector dps = new DataPathSelector(						sourceFromTo.dataVar, propVar, targetFromTo.dataVar,						extendedPropInterpretation);				_unboundVarList.remove(sourceFromTo.dataVar);				_unboundVarList.remove(propVar);				_unboundVarList.remove(targetFromTo.dataVar);				selectors.add(dps);			}			// If appropriate, create DomainSelector			if (sourceFromTo.classQ != null) {				DomainSelector domainSel = new DomainSelector(						sourceFromTo.classQ, propVar);				_unboundVarList.remove(propVar);				_unboundVarList.remove(sourceFromTo.classQ);				selectors.add(domainSel);			}			// If appropriate, create RangeSelector			if (targetFromTo.classQ != null) {				RangeSelector rangeSel = new RangeSelector(						propVar, targetFromTo.classQ);				_unboundVarList.remove(propVar);				_unboundVarList.remove(targetFromTo.classQ);				selectors.add(rangeSel);			}		}		// Return target FromTo for implicit join with next path element		return targetFromTo;	}}/* * A path element whose domain is joined with the range of another * using a ".". The supplied sourceFromTo containing the range of the * other path element must never be null. */FromTo joined_pathelem(List selectors, FromTo sourceFromTo) :{	FromTo targetFromTo = null;	PropertyQuery propQ = null;}{	<DOT> propQ = ext_property_query()	(<LBRACE> targetFromTo = from_to(selectors) <RBRACE>)?	{		// targetFromTo must always be present for implicit joins		// with next path element:		if (targetFromTo == null) {			targetFromTo = new FromTo();		}		// In case just one of the FromTo's has a dataVar, the other		// should get an anonymous DataVar.		if (sourceFromTo.dataVar != null) {			if (targetFromTo.dataVar == null) {				targetFromTo.dataVar = new DataVar(getAnonymousVarName());				if (targetFromTo.classQ != null) {					// We've created a new class restriction					InstanceSelector resSel = new InstanceSelector(							targetFromTo.dataVar, targetFromTo.classQ,							true);					_unboundVarList.remove(targetFromTo.dataVar);					_unboundVarList.remove(targetFromTo.classQ);					selectors.add(resSel);				}			}		}		else { // sourceFromTo.dataVar == null			if (targetFromTo.dataVar != null) { //				sourceFromTo.dataVar = new DataVar(getAnonymousVarName());				if (sourceFromTo.classQ != null) {					// We've created a new class restriction					InstanceSelector resSel = new InstanceSelector(							sourceFromTo.dataVar, sourceFromTo.classQ,							true);					_unboundVarList.remove(sourceFromTo.dataVar);					_unboundVarList.remove(sourceFromTo.classQ);					selectors.add(resSel);				}			}		}		// Create a property selector for the property in case propQ		// is not a PropertyVar itself		PropertyVar propVar = null;		if (propQ instanceof PropertyVar) {			propVar = (PropertyVar)propQ;		}		else {			// Use anonymous var that can be shared with other selectors			propVar = new PropertyVar(getAnonymousVarName());			PropertySelector propSel = new PropertySelector(propVar, propQ);			_unboundVarList.remove(propQ);			selectors.add(propSel);		}		// If appropriate, create DataPathSelector		if (targetFromTo.dataVar != null) {			boolean extendedPropInterpretation = true;			if (propQ instanceof PropertyVar) {				extendedPropInterpretation = false;			}			DataPathSelector dps = new DataPathSelector(					sourceFromTo.dataVar, propVar, targetFromTo.dataVar,					extendedPropInterpretation);			_unboundVarList.remove(sourceFromTo.dataVar);			_unboundVarList.remove(propVar);			_unboundVarList.remove(targetFromTo.dataVar);			selectors.add(dps);		}		// If appropriate, create DomainSelector		if (sourceFromTo.classQ != null) {			DomainSelector domainSel = new DomainSelector(					sourceFromTo.classQ, propVar);			_unboundVarList.remove(propVar);			_unboundVarList.remove(sourceFromTo.classQ);			selectors.add(domainSel);		}		// If appropriate, create RangeSelector		if (targetFromTo.classQ != null) {			RangeSelector rangeSel = new RangeSelector(					propVar, targetFromTo.classQ);			_unboundVarList.remove(propVar);			_unboundVarList.remove(targetFromTo.classQ);			selectors.add(rangeSel);		}		// Return target FromTo for implicit join with next path element		return targetFromTo;	}}FromTo from_to(List selectors) :{	FromTo result = new FromTo();}{	(result.dataVar = datavar())?  (<COLON> result.classQ = schema_part())?	{		if (result.dataVar != null && result.classQ != null) {		    // if of the form {X : someClass} do extended		    // interpretation, but if the schema constraint is		    // more complex do restricted interpretation.		    boolean properInstancesOnly = true;		    if (result.classQ instanceof URI) {			properInstancesOnly = false;		    }		    InstanceSelector resSel = new InstanceSelector(			    result.dataVar, result.classQ,			    properInstancesOnly);		    _unboundVarList.remove(result.dataVar);		    _unboundVarList.remove(result.classQ);		    selectors.add(resSel);		}		return result;	}}ClassQuery schema_part() :{	ClassQuery result;}{	result = classvar() { return result; }|	result = uri() { return result; }}/*-------------------------------------------------+| DATA QUERIES                                     |+-------------------------------------------------*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?