Java Code Examples for org.eclipse.emf.ecore.EClass#isInstance()

The following examples show how to use org.eclipse.emf.ecore.EClass#isInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestAssertions.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Assert that the given issue is inside the list of issues.
 *
 * @param issues the list of issues.
 * @param severity the expected severity.
 * @param model the issued model.
 * @param objectType the type of the object.
 * @param code the issue code.
 * @param messageParts the parts of the issue message that are expected.
 */
public static void assertIssue(List<Issue> issues, Severity severity, EObject model,
		EClass objectType, String code, String... messageParts) {
	Iterator<Issue> iterator = issues.iterator();
	while (iterator.hasNext()) {
		Issue issue = iterator.next();
		if (Objects.equal(issue.getCode(), code) && issue.getSeverity() == severity) {
			EObject object = model.eResource().getResourceSet().getEObject(issue.getUriToProblem(), true);
			if (objectType.isInstance(object)) {
				if (TestIssues.isIssueMessage(issue, messageParts)) {
					iterator.remove();
					return;
				}
			}
		}
	}
	StringBuilder message = new StringBuilder("Expected ");
	message.append(severity);
	message.append(" '");
	message.append(code);
	message.append("' on ");
	message.append(objectType.getName());
	message.append(" but got\n");
	TestIssues.getIssuesAsString(model, issues, message);
	fail(message.toString());
}
 
Example 2
Source File: AbstractPackageJSONValidatorExtension.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks that the given JSON {@code value} is an instance of the given {@code valueClass}.
 *
 * Adds an {@link JSONIssueCodes#JSON_EXPECTED_DIFFERENT_VALUE_TYPE} to {@code value} if not.
 *
 * @param value
 *            The value whose type to check.
 * @param valueClass
 *            The expected class
 * @param locationClause
 *            Additional clause to append to the error message to improve the validation message quality (e.g.
 *            Expected string instead of object <<for property 'Z'>>.)
 */
protected boolean checkIsType(JSONValue value, EClass valueClass, String locationClause) {
	// add no issue in case of a broken AST
	if (null == value) {
		return false;
	}
	if (!valueClass.isInstance(value)) {
		addIssue(JSONIssueCodes.getMessageForJSON_EXPECTED_DIFFERENT_VALUE_TYPE(getJSONValueDescription(valueClass),
				getJSONValueDescription(value), locationClause), value,
				JSONIssueCodes.JSON_EXPECTED_DIFFERENT_VALUE_TYPE);
		return false;
	}
	return true;
}
 
Example 3
Source File: EObjectViewerFilter.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tells if the given {@link EObject} is instance of any {@link EClass} in the given {@link Set}.
 * 
 * @param eObj
 *            the {@link EObject} to test
 * @param eClasses
 *            the {@link Set} of {@link EClass}
 * @return <code>true</code> if the given {@link EObject} is instance of any {@link EClass} in the given {@link Set}, <code>false</code>
 *         otherwise
 */
private boolean isInstanceOfAny(EObject eObj, Set<EClass> eClasses) {
    boolean res = false;

    for (EClass eCls : eClasses) {
        if (eCls.isInstance(eObj)) {
            res = true;
            break;
        }
    }

    return res;
}
 
Example 4
Source File: RenderingSession.java    From textuml with Eclipse Public License 1.0 5 votes vote down vote up
public E getPrevious(EClass eClass) {
    if (stack.size() <= 1)
        return null;
    int start = stack.size() - 2;
    for (int i = start; i >= 0; i--) {
        E current = stack.get(i);
        if (eClass.isInstance(current))
            return current;
    }
    return null;
}
 
Example 5
Source File: Repository.java    From textuml with Eclipse Public License 1.0 5 votes vote down vote up
public Package[] getTopLevelPackages(EClass packageClass) {
    if (packageClass == null)
        packageClass = PACKAGE.getPackage();
    List<Resource> results = resourceSet.getResources();
    List<Package> result = new ArrayList<Package>(results.size());
    for (Resource currentResource : results)
        for (EObject element : currentResource.getContents())
            if (packageClass.isInstance(element))
                result.add((Package) element);
    return result.toArray(new Package[result.size()]);
}
 
Example 6
Source File: ActivityUtils.java    From textuml with Eclipse Public License 1.0 5 votes vote down vote up
private static void collectUpstreamActions(List<Action> collected, Action current, EClass... actionClasses) {
    for (EClass actionClass : actionClasses)
        if (actionClass.isInstance(current))
            collected.add(current);
    for (InputPin pin : current.getInputs())
        collectUpstreamActions(collected, getSourceAction(pin), actionClasses);
}
 
Example 7
Source File: MDDUtil.java    From textuml with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> List<T> filterByClass(List elements, EClass elementClass) {
    List<T> filtered = new ArrayList<T>(elements.size());
    for (Object object : elements)
        if (elementClass.isInstance(object))
            filtered.add((T) object);
    return filtered;
}
 
Example 8
Source File: MDDUtil.java    From textuml with Eclipse Public License 1.0 5 votes vote down vote up
public static <T extends Element> Optional<T> findNearest(Element reference, EClass eClass) {
    if (eClass.isInstance(reference))
        return Optional.of((T) reference);
    Element owner = reference.getOwner();
    if (owner == null)
        return Optional.empty();
    return findNearest(owner, eClass);
}
 
Example 9
Source File: NamedElementUtils.java    From textuml with Eclipse Public License 1.0 5 votes vote down vote up
public static Namespace findNearestNamespace(NamedElement element, EClass... classes) {
    Namespace namespace = element instanceof Namespace ? (Namespace) element : element.getNamespace();
    while (namespace != null) {
        for (EClass clazz : classes)
            if (clazz.isInstance(namespace))
                return namespace;
        namespace = namespace.getNamespace();
    }
    throw new IllegalArgumentException("Could not find a namespace of the given types");
}
 
Example 10
Source File: NamespaceTracker.java    From textuml with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the closest containing namespace that is an instance of the given
 * type. If the type is <code>null</code>, returns the closest containing
 * namespace regardless its type.
 */
public Namespace currentNamespace(EClass namespaceType) {
    if (namespaceType == null)
        return namespaces.isEmpty() ? null : (Namespace) namespaces.peek();
    for (int i = namespaces.size() - 1; i >= 0; i--) {
        Namespace current = namespaces.get(i);
        if (namespaceType.isInstance(current))
            return current;
    }
    return null;
}
 
Example 11
Source File: GetDataObjectsByTypeDatabaseAction.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<DataObject> execute() throws UserException, BimserverLockConflictException, BimserverDatabaseException {
	EClass eClass = getDatabaseSession().getEClassForName(packageName, className);
	Revision virtualRevision = getRevisionByRoid(roid);
	if (virtualRevision == null) {
		throw new UserException("No revision with roid " + roid + " found");
	}
	Map<Integer, Long> pidRoidMap = new HashMap<>();
	IfcModelSet ifcModelSet = new IfcModelSet();
	pidRoidMap.put(virtualRevision.getProject().getId(), virtualRevision.getOid());
	PackageMetaData lastPackageMetaData = null;
	Project project = virtualRevision.getProject();
	for (ConcreteRevision concreteRevision : virtualRevision.getConcreteRevisions()) {
		PackageMetaData packageMetaData = getBimServer().getMetaDataManager().getPackageMetaData(concreteRevision.getProject().getSchema());
		int highestStopId = findHighestStopRid(project, concreteRevision);
		OldQuery query = new OldQuery(packageMetaData, concreteRevision.getProject().getId(), concreteRevision.getId(), -1, Deep.NO, highestStopId);
		lastPackageMetaData = packageMetaData;
		IfcModelInterface subModel = getDatabaseSession().getAllOfType(packageMetaData.getEPackage().getName(), className, query);
		subModel.getModelMetaData().setDate(concreteRevision.getDate());
		ifcModelSet.add(subModel);
	}
	IfcModelInterface ifcModel = getDatabaseSession().createServerModel(lastPackageMetaData, pidRoidMap);
	if (ifcModelSet.size() > 1) {
		try {
			ifcModel = getBimServer().getMergerFactory().createMerger(getDatabaseSession(), getAuthorization().getUoid()).merge(project, ifcModelSet, new ModelHelper(getBimServer().getMetaDataManager(), ifcModel));
		} catch (MergeException e) {
			throw new UserException(e);
		}
	} else {
		ifcModel = ifcModelSet.iterator().next();
	}
	List<DataObject> dataObjects = new ArrayList<DataObject>();
	for (Long oid : new HashSet<>(ifcModel.keySet())) {
		EObject eObject = ifcModel.get(oid);
		if (eClass.isInstance(eObject)) {
			DataObject dataObject = StoreFactory.eINSTANCE.createDataObject();
			if (eObject instanceof IfcRoot) {
				IfcRoot ifcRoot = (IfcRoot)eObject;
				String guid = ifcRoot.getGlobalId() != null ? ifcRoot.getGlobalId() : "";
				String name = ifcRoot.getName() != null ? ifcRoot.getName() : "";
				dataObject = StoreFactory.eINSTANCE.createDataObject();
				dataObject.setType(eObject.eClass().getName());
				((IdEObjectImpl)dataObject).setOid(oid);
				dataObject.setGuid(guid);
				dataObject.setName(name);
			} else {
				dataObject = StoreFactory.eINSTANCE.createDataObject();
				dataObject.setType(eObject.eClass().getName());
				((IdEObjectImpl)dataObject).setOid(oid);
				dataObject.setGuid("");
				dataObject.setName("");
			}
			if (!flat) {
				GetDataObjectByOidDatabaseAction.fillDataObject(ifcModel.getObjects(), eObject, dataObject);
			}
			dataObjects.add(dataObject);
		}
	}
	return dataObjects;
}
 
Example 12
Source File: MDDUtil.java    From textuml with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns the outermost parent of the given class. Fails if cannot find
 * one. Returns the reference itself if is an instance of the given class
 * and no parent can be found.
 * 
 * @param reference
 *            the reference element
 * @param eClass
 *            the type
 * @return the outermost enclosing element of the given class
 */
public static <T extends Element> T getFarthest(Element reference, EClass eClass) {
    T parent = getFarthest(reference.getOwner(), eClass);
    if (eClass.isInstance(parent))
        return parent;
    return eClass.isInstance(reference) ? (T) reference : null;
}