org.modelcc.metamodel.Model Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ModelCC Show documentation
Show all versions of ModelCC Show documentation
ModelCC is a model-based parser generator (a.k.a. compiler compiler) that decouples language specification from language processing, avoiding some of the problems caused by grammar-driven parser generators. ModelCC receives a conceptual model as input, along with constraints that annotate it. It is then able to create a parser for the desired textual language and the generated parser fully automates the instantiation of the language conceptual model. ModelCC also includes a built-in reference resolution mechanism that results in abstract syntax graphs, rather than mere abstract syntax trees.
The newest version!
/*
* ModelCC, distributed under ModelCC Shared Software License, www.modelcc.org
*/
package org.modelcc.metamodel;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
* ModelCC metamodel
*
* @author Fernando Berzal ([email protected])
*/
public abstract class Model implements java.io.Serializable
{
/**
* Set of elements of this model.
*/
private Set elements;
/**
* ModelElement subclasses map.
*/
private Map> subelements;
/**
* ModelElement superclass map.
*/
private Map superelements;
/**
* Class to element map.
*/
private Map classToElement;
/**
* Constructor.
* @param elements the set of elements of this model.
* @param subelements the element subclass map
* @param superelements the element superclass map
* @param classToElement the class to element map
*/
public Model(
Set elements,
Map> subelements,
Map superelements,
Map classToElement)
{
this.elements = elements;
this.subelements = subelements;
this.superelements = superelements;
this.classToElement = classToElement;
}
/**
* @return the elements of this model
*/
public Set getElements() {
return Collections.unmodifiableSet(elements);
}
/**
* @return the element subelements map
*/
public Map> getSubelements() {
return Collections.unmodifiableMap(subelements);
}
/**
* @return the element superelements map
*/
public Map getSuperelements() {
return Collections.unmodifiableMap(superelements);
}
/**
* @return the classToElement
*/
public Map getClassToElement() {
return Collections.unmodifiableMap(classToElement);
}
/**
* Find element by name
* @param name element name
* @return Model element
* @throws ModelException
*/
public final E findElement (String name) throws ModelException
{
E me = null;
for (E mec: getElements()) {
if (mec.getElementClass().getCanonicalName().endsWith(name) && name.endsWith(mec.getElementClass().getSimpleName())) {
if (me == null)
me = mec;
else
throw new ModelException("Ambiguous element definition???");
}
}
if (me != null)
return me;
for (E mec: getElements()) {
if (mec.getElementClass().getName().endsWith(name) && name.endsWith(mec.getElementClass().getSimpleName())) {
if (me == null)
me = mec;
else
throw new ModelException("Ambiguous element definition???");
}
}
if (me != null)
return me;
for (E mec: getElements()) {
if (mec.getElementClass().getSimpleName().endsWith(name) && name.endsWith(mec.getElementClass().getSimpleName())) {
if (me == null)
me = mec;
else
throw new ModelException("Ambiguous element definition???");
}
}
if (me != null)
return me;
return null;
}
// toString
public String toString ()
{
return "MODEL "+elements.toString();
}
}