Search in sources :

Example 1 with TopLevel

use of org.sbolstandard.core2.TopLevel in project libSBOLj by SynBioDex.

the class SBOLDocument method addTopLevel.

/**
 * @param newTopLevel
 * @param instancesMap
 * @param typeName
 * @param maps
 * @throws SBOLValidationException
 *             if either of the following SBOL validation rules was violated:
 *             10202, 10220.
 */
@SafeVarargs
private final <TL extends TopLevel> void addTopLevel(TL newTopLevel, Map<URI, TL> instancesMap, String typeName, Map<URI, ? extends Identified>... maps) throws SBOLValidationException {
    boolean childrenCompliant = true;
    try {
        URIcompliance.isURIcompliant(newTopLevel);
    // newTopLevel.checkDescendantsURIcompliance();
    } catch (SBOLValidationException e) {
        childrenCompliant = false;
    }
    if (compliant && childrenCompliant) {
        URI persistentId = URI.create(extractPersistentId(newTopLevel.getIdentity()));
        if (keyExistsInAnyMap(persistentId, maps))
            throw new SBOLValidationException("sbol-10220", newTopLevel);
        if (instancesMap.containsKey(newTopLevel.getIdentity()))
            throw new SBOLValidationException("sbol-10202", newTopLevel);
        String prefix = extractURIprefix(persistentId);
        while (prefix != null) {
            if (keyExistsInAnyMap(URI.create(prefix), maps))
                throw new SBOLValidationException("sbol-10202", newTopLevel);
            if (instancesMap.containsKey(URI.create(prefix)))
                throw new SBOLValidationException("sbol-10202", newTopLevel);
            prefix = extractURIprefix(URI.create(prefix));
        }
        if (prefixes.contains(persistentId.toString())) {
            throw new IllegalArgumentException("Persistent identity `" + persistentId.toString() + "' matches URI prefix in document.");
        }
        prefix = extractURIprefix(persistentId);
        while (prefix != null) {
            prefixes.add(prefix);
            prefix = extractURIprefix(URI.create(prefix));
        }
        instancesMap.put(newTopLevel.getIdentity(), newTopLevel);
        Identified latest = instancesMap.get(persistentId);
        if (latest == null) {
            instancesMap.put(persistentId, newTopLevel);
        } else {
            if (isFirstVersionNewer(extractVersion(newTopLevel.getIdentity()), extractVersion(latest.getIdentity()))) {
                instancesMap.put(persistentId, newTopLevel);
            }
        }
    } else {
        // Only check if URI exists in all maps.
        if (keyExistsInAnyMap(newTopLevel.getIdentity()))
            throw new SBOLValidationException("sbol-10202", newTopLevel);
        if (instancesMap.containsKey(newTopLevel.getIdentity()))
            throw new SBOLValidationException("sbol-10202", newTopLevel);
        instancesMap.put(newTopLevel.getIdentity(), newTopLevel);
        if (newTopLevel.isSetPersistentIdentity()) {
            Identified latest = instancesMap.get(newTopLevel.getPersistentIdentity());
            if (latest == null) {
                instancesMap.put(newTopLevel.getPersistentIdentity(), newTopLevel);
            } else {
                if (isFirstVersionNewer(extractVersion(newTopLevel.getIdentity()), extractVersion(latest.getIdentity()))) {
                    instancesMap.put(newTopLevel.getPersistentIdentity(), newTopLevel);
                }
            }
        }
    }
    newTopLevel.setSBOLDocument(this);
}
Also used : URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI)

Example 2 with TopLevel

use of org.sbolstandard.core2.TopLevel in project libSBOLj by SynBioDex.

the class SBOLDocument method updateReferences.

private void updateReferences(HashMap<URI, URI> uriMap) throws SBOLValidationException {
    for (TopLevel topLevel : getTopLevels()) {
        for (URI wasDerivedFrom : topLevel.getWasDerivedFroms()) {
            if (uriMap.get(wasDerivedFrom) != null) {
                topLevel.removeWasDerivedFrom(wasDerivedFrom);
                topLevel.addWasDerivedFrom(uriMap.get(wasDerivedFrom));
            }
        }
        for (URI wasGeneratedBy : topLevel.getWasGeneratedBys()) {
            if (uriMap.get(wasGeneratedBy) != null) {
                topLevel.removeWasGeneratedBy(wasGeneratedBy);
                topLevel.addWasGeneratedBy(uriMap.get(wasGeneratedBy));
            }
        }
        for (URI attachmentURI : topLevel.getAttachmentURIs()) {
            if (uriMap.get(attachmentURI) != null) {
                topLevel.removeAttachment(attachmentURI);
                topLevel.addAttachment(uriMap.get(attachmentURI));
            }
        }
    }
    for (Collection collection : getCollections()) {
        for (URI memberURI : collection.getMemberURIs()) {
            if (uriMap.get(memberURI) != null) {
                collection.removeMember(memberURI);
                collection.addMember(uriMap.get(memberURI));
            }
        }
        updateReferences(collection, uriMap);
    }
    for (Experiment experiment : getExperiments()) {
        for (URI experimentalDatumURI : experiment.getExperimentalDataURIs()) {
            if (uriMap.get(experimentalDatumURI) != null) {
                experiment.removeExperimentalData(experimentalDatumURI);
                experiment.addExperimentalData(uriMap.get(experimentalDatumURI));
            }
        }
        updateReferences(experiment, uriMap);
    }
    for (ComponentDefinition componentDefinition : getComponentDefinitions()) {
        updateReferences(componentDefinition, uriMap);
        for (URI sequenceURI : componentDefinition.getSequenceURIs()) {
            if (uriMap.get(sequenceURI) != null) {
                componentDefinition.removeSequence(sequenceURI);
                componentDefinition.addSequence(uriMap.get(sequenceURI));
            }
        }
        for (Component component : componentDefinition.getComponents()) {
            if (uriMap.get(component.getDefinitionURI()) != null) {
                component.setDefinition(uriMap.get(component.getDefinitionURI()));
                for (MapsTo mapsTo : component.getMapsTos()) {
                    ComponentDefinition cd = getComponentDefinition(component.getDefinitionURI());
                    if (cd != null) {
                        String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
                        URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
                        mapsTo.setRemote(newURI);
                    }
                }
            }
            updateReferences(component, uriMap);
            for (MapsTo mapsTo : component.getMapsTos()) {
                updateReferences(mapsTo, uriMap);
            }
            for (Location sourceLocation : component.getSourceLocations()) {
                if (sourceLocation.isSetSequence() && uriMap.get(sourceLocation.getSequenceURI()) != null) {
                    sourceLocation.setSequence(uriMap.get(sourceLocation.getSequenceURI()));
                }
            }
        }
        for (SequenceAnnotation sa : componentDefinition.getSequenceAnnotations()) {
            for (Location loc : sa.getLocations()) {
                if (loc.isSetSequence() && uriMap.get(loc.getSequenceURI()) != null) {
                    loc.setSequence(uriMap.get(loc.getSequenceURI()));
                }
                updateReferences(loc, uriMap);
            }
            updateReferences(sa, uriMap);
        }
        for (SequenceConstraint sc : componentDefinition.getSequenceConstraints()) {
            updateReferences(sc, uriMap);
        }
    }
    for (ModuleDefinition moduleDefinition : getModuleDefinitions()) {
        updateReferences(moduleDefinition, uriMap);
        for (FunctionalComponent functionalComponent : moduleDefinition.getFunctionalComponents()) {
            if (uriMap.get(functionalComponent.getDefinitionURI()) != null) {
                functionalComponent.setDefinition(uriMap.get(functionalComponent.getDefinitionURI()));
                for (MapsTo mapsTo : functionalComponent.getMapsTos()) {
                    ComponentDefinition cd = getComponentDefinition(functionalComponent.getDefinitionURI());
                    if (cd != null) {
                        String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
                        URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
                        mapsTo.setRemote(newURI);
                    }
                }
            }
            updateReferences(functionalComponent, uriMap);
            for (MapsTo mapsTo : functionalComponent.getMapsTos()) {
                updateReferences(mapsTo, uriMap);
            }
        }
        for (Module module : moduleDefinition.getModules()) {
            if (uriMap.get(module.getDefinitionURI()) != null) {
                module.setDefinition(uriMap.get(module.getDefinitionURI()));
                for (MapsTo mapsTo : module.getMapsTos()) {
                    ModuleDefinition md = getModuleDefinition(module.getDefinitionURI());
                    if (md != null) {
                        String displayId = URIcompliance.extractDisplayId(mapsTo.getRemoteURI());
                        URI newURI = URIcompliance.createCompliantURI(md.getPersistentIdentity().toString(), displayId, md.getVersion());
                        mapsTo.setRemote(newURI);
                    }
                }
            }
            updateReferences(module, uriMap);
            for (MapsTo mapsTo : module.getMapsTos()) {
                updateReferences(mapsTo, uriMap);
            }
        }
        for (Interaction interaction : moduleDefinition.getInteractions()) {
            updateReferences(interaction, uriMap);
            for (Participation participation : interaction.getParticipations()) {
                updateReferences(participation, uriMap);
            }
        }
        for (URI modelURI : moduleDefinition.getModelURIs()) {
            if (uriMap.get(modelURI) != null) {
                moduleDefinition.removeModel(modelURI);
                moduleDefinition.addModel(uriMap.get(modelURI));
            }
        }
    }
    for (Model model : getModels()) {
        if (uriMap.get(model.getSource()) != null) {
            model.setSource(uriMap.get(model.getSource()));
        }
        updateReferences(model, uriMap);
    }
    for (Sequence sequence : getSequences()) {
        updateReferences(sequence, uriMap);
    }
    for (Attachment attachment : getAttachments()) {
        updateReferences(attachment, uriMap);
    }
    for (Implementation implementation : getImplementations()) {
        if (implementation.isSetBuilt()) {
            URI built = implementation.getBuiltURI();
            if (uriMap.get(built) != null) {
                implementation.setBuilt(uriMap.get(built));
            }
        }
        updateReferences(implementation, uriMap);
    }
    for (GenericTopLevel genericTopLevel : getGenericTopLevels()) {
        updateReferences(genericTopLevel, uriMap);
    }
    for (CombinatorialDerivation combinatorialDerivation : getCombinatorialDerivations()) {
        updateReferences(combinatorialDerivation, uriMap);
        if (uriMap.get(combinatorialDerivation.getTemplateURI()) != null) {
            combinatorialDerivation.setTemplate(uriMap.get(combinatorialDerivation.getTemplateURI()));
            ComponentDefinition cd = getComponentDefinition(combinatorialDerivation.getTemplateURI());
            if (cd != null) {
                for (VariableComponent variableComponent : combinatorialDerivation.getVariableComponents()) {
                    String displayId = URIcompliance.extractDisplayId(variableComponent.getVariableURI());
                    URI newURI = URIcompliance.createCompliantURI(cd.getPersistentIdentity().toString(), displayId, cd.getVersion());
                    variableComponent.setVariable(newURI);
                }
            }
        }
        for (VariableComponent variableComponent : combinatorialDerivation.getVariableComponents()) {
            for (URI variantURI : variableComponent.getVariantURIs()) {
                if (uriMap.get(variantURI) != null) {
                    variableComponent.removeVariant(variantURI);
                    variableComponent.addVariant(uriMap.get(variantURI));
                }
            }
            for (URI variantCollectionURI : variableComponent.getVariantCollectionURIs()) {
                if (uriMap.get(variantCollectionURI) != null) {
                    variableComponent.removeVariantCollection(variantCollectionURI);
                    variableComponent.addVariantCollection(uriMap.get(variantCollectionURI));
                }
            }
            for (URI variantDerivationURI : variableComponent.getVariantDerivationURIs()) {
                if (uriMap.get(variantDerivationURI) != null) {
                    variableComponent.removeVariantDerivation(variantDerivationURI);
                    variableComponent.addVariantDerivation(uriMap.get(variantDerivationURI));
                }
            }
            updateReferences(variableComponent, uriMap);
        }
    }
    for (Activity activity : getActivities()) {
        updateReferences(activity, uriMap);
        for (Association association : activity.getAssociations()) {
            if (uriMap.get(association.getAgentURI()) != null) {
                association.setAgent(uriMap.get(association.getAgentURI()));
            }
            if (uriMap.get(association.getPlanURI()) != null) {
                association.setPlan(uriMap.get(association.getPlanURI()));
            }
            updateReferences(association, uriMap);
        }
        for (Usage usage : activity.getUsages()) {
            if (uriMap.get(usage.getEntityURI()) != null) {
                usage.setEntity(uriMap.get(usage.getEntityURI()));
            }
            updateReferences(usage, uriMap);
        }
    }
    for (Agent agent : getAgents()) {
        updateReferences(agent, uriMap);
    }
    for (Plan plan : getPlans()) {
        updateReferences(plan, uriMap);
    }
}
Also used : URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI)

Example 3 with TopLevel

use of org.sbolstandard.core2.TopLevel in project libSBOLj by SynBioDex.

the class SBOLReader method parseActivity.

@SuppressWarnings("unchecked")
private static Activity parseActivity(SBOLDocument SBOLDoc, IdentifiableDocument<QName> topLevel, Map<URI, NestedDocument<QName>> nested) throws SBOLValidationException {
    String displayId = null;
    String name = null;
    String description = null;
    URI persistentIdentity = null;
    String version = null;
    Set<URI> wasDerivedFroms = new HashSet<>();
    Set<URI> wasGeneratedBys = new HashSet<>();
    Set<URI> attachments = new HashSet<>();
    Set<URI> type = new HashSet<>();
    DateTime startedAtTime = null;
    DateTime endedAtTime = null;
    Set<URI> wasInformedBys = new HashSet<>();
    Set<Association> qualifiedAssociations = new HashSet<>();
    Set<Usage> qualifiedUsages = new HashSet<>();
    List<Annotation> annotations = new ArrayList<>();
    for (NamedProperty<QName> namedProperty : topLevel.getProperties()) {
        if (namedProperty.getName().equals(Sbol2Terms.Identified.persistentIdentity)) {
            if (!(namedProperty.getValue() instanceof Literal) || persistentIdentity != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10203", topLevel.getIdentity());
            }
            persistentIdentity = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.version)) {
            if (!(namedProperty.getValue() instanceof Literal) || version != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10206", topLevel.getIdentity());
            }
            version = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.displayId)) {
            if (!(namedProperty.getValue() instanceof Literal) || displayId != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10204", topLevel.getIdentity());
            }
            displayId = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.title)) {
            if (!(namedProperty.getValue() instanceof Literal) || name != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10212", topLevel.getIdentity());
            }
            name = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.description)) {
            if (!(namedProperty.getValue() instanceof Literal) || description != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10213", topLevel.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasDerivedFrom)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10208", topLevel.getIdentity());
            }
            wasDerivedFroms.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasGeneratedBy)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10221", topLevel.getIdentity());
            }
            wasGeneratedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.Activity.type)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-12412", topLevel.getIdentity());
            }
            type.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.TopLevel.hasAttachment)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
                }
                attachments.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof IdentifiableDocument) {
                if (((IdentifiableDocument<QName>) namedProperty).getType().equals(Sbol2Terms.Attachment.Attachment)) {
                    Attachment attachment = parseAttachment(SBOLDoc, (IdentifiableDocument<QName>) namedProperty.getValue());
                    attachments.add(attachment.getIdentity());
                } else {
                    throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
                }
            } else {
                throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Activity.startedAtTime)) {
            if (!(namedProperty.getValue() instanceof Literal) || startedAtTime != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-12402", topLevel.getIdentity());
            }
            DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
            try {
                startedAtTime = fmt.parseDateTime(((Literal<QName>) namedProperty.getValue()).getValue().toString());
            } catch (IllegalArgumentException e) {
                throw new SBOLValidationException("sbol-12402", topLevel.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Activity.endedAtTime)) {
            if (!(namedProperty.getValue() instanceof Literal) || endedAtTime != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-12403", topLevel.getIdentity());
            }
            DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
            try {
                endedAtTime = fmt.parseDateTime(((Literal<QName>) namedProperty.getValue()).getValue().toString());
            } catch (IllegalArgumentException e) {
                throw new SBOLValidationException("sbol-12403", topLevel.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Activity.wasInformedBy)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-12406", topLevel.getIdentity());
                }
                wasInformedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof IdentifiableDocument) {
                if (((IdentifiableDocument<QName>) namedProperty).getType().equals(Sbol2Terms.Activity.Activity)) {
                    Activity activity = parseActivity(SBOLDoc, (IdentifiableDocument<QName>) namedProperty.getValue(), nested);
                    wasInformedBys.add(activity.getIdentity());
                } else {
                    throw new SBOLValidationException("sbol-12406", topLevel.getIdentity());
                }
            } else {
                throw new SBOLValidationException("sbol-12406", topLevel.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Activity.qualifiedAssociation)) {
            if (namedProperty.getValue() instanceof NestedDocument) {
                NestedDocument<QName> nestedDocument = ((NestedDocument<QName>) namedProperty.getValue());
                if (nestedDocument.getType() == null || !nestedDocument.getType().equals(Sbol2Terms.Association.Association)) {
                    throw new SBOLValidationException("sbol-12404", topLevel.getIdentity());
                }
                qualifiedAssociations.add(parseAssociation(SBOLDoc, ((NestedDocument<QName>) namedProperty.getValue()), nested));
            } else {
                URI uri = (URI) ((Literal<QName>) namedProperty.getValue()).getValue();
                NestedDocument<QName> nestedDocument = nested.get(uri);
                if (nestedDocument == null || nestedDocument.getType() == null || !nestedDocument.getType().equals(Sbol2Terms.Association.Association)) {
                    throw new SBOLValidationException("sbol-12404", topLevel.getIdentity());
                }
                qualifiedAssociations.add(parseAssociation(SBOLDoc, nestedDocument, nested));
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Activity.qualifiedUsage)) {
            if (namedProperty.getValue() instanceof NestedDocument) {
                NestedDocument<QName> nestedDocument = ((NestedDocument<QName>) namedProperty.getValue());
                if (nestedDocument.getType() == null || !nestedDocument.getType().equals(Sbol2Terms.Usage.Usage)) {
                    throw new SBOLValidationException("sbol-12405", topLevel.getIdentity());
                }
                qualifiedUsages.add(parseUsage(SBOLDoc, ((NestedDocument<QName>) namedProperty.getValue()), nested));
            } else {
                URI uri = (URI) ((Literal<QName>) namedProperty.getValue()).getValue();
                NestedDocument<QName> nestedDocument = nested.get(uri);
                if (nestedDocument == null || nestedDocument.getType() == null || !nestedDocument.getType().equals(Sbol2Terms.Usage.Usage)) {
                    throw new SBOLValidationException("sbol-12405", topLevel.getIdentity());
                }
                qualifiedUsages.add(parseUsage(SBOLDoc, nestedDocument, nested));
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Description.type) && ((Literal<QName>) namedProperty.getValue()).getValue().toString().equals(Sbol2Terms.Activity.Activity.getNamespaceURI() + Sbol2Terms.Activity.Activity.getLocalPart())) {
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    // GenericTopLevel t = SBOLDoc.createGenericTopLevel(topLevel.getIdentity(), topLevel.getType());
    Activity t = new Activity(topLevel.getIdentity());
    if (persistentIdentity != null)
        t.setPersistentIdentity(persistentIdentity);
    if (version != null)
        t.setVersion(version);
    if (displayId != null)
        t.setDisplayId(displayId);
    if (name != null)
        t.setName(name);
    if (description != null)
        t.setDescription(description);
    if (!type.isEmpty()) {
        t.setTypes(type);
    }
    t.setWasDerivedFroms(wasDerivedFroms);
    t.setWasGeneratedBys(wasGeneratedBys);
    if (!annotations.isEmpty())
        t.setAnnotations(annotations);
    if (startedAtTime != null)
        t.setStartedAtTime(startedAtTime);
    if (endedAtTime != null)
        t.setEndedAtTime(endedAtTime);
    if (!qualifiedAssociations.isEmpty())
        t.setAssociations(qualifiedAssociations);
    if (!qualifiedUsages.isEmpty())
        t.setUsages(qualifiedUsages);
    if (!wasInformedBys.isEmpty())
        t.setWasInformedBys(wasInformedBys);
    if (!attachments.isEmpty())
        t.setAttachments(attachments);
    Activity oldA = SBOLDoc.getActivity(topLevel.getIdentity());
    if (oldA == null) {
        SBOLDoc.addActivity(t);
    } else {
        if (!t.equals(oldA)) {
            throw new SBOLValidationException("sbol-10202", t);
        }
    }
    return t;
}
Also used : StringifyQName(org.sbolstandard.core.io.json.StringifyQName) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI) NestedDocument(org.sbolstandard.core.datatree.NestedDocument) DateTime(org.joda.time.DateTime) IdentifiableDocument(org.sbolstandard.core.datatree.IdentifiableDocument) Literal(org.sbolstandard.core.datatree.Literal) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) HashSet(java.util.HashSet)

Example 4 with TopLevel

use of org.sbolstandard.core2.TopLevel in project libSBOLj by SynBioDex.

the class SBOLReader method parseCollection.

/**
 * @param SBOLDoc
 * @param topLevel
 * @return
 * @throws SBOLValidationException if either of the following conditions is satisfied:
 * <ul>
 * <li>any of the following SBOL validation rules was violated:
 * 10202, 10203, 10204, 10206, 10208, 10212, 10213, 12102; or</li>
 * <li>an SBOL validation rule violation occurred in the following constructor or methods:
 * 	<ul>
 * 		<li>{@link Collection#Collection(URI)}, </li>
 * 		<li>{@link Collection#setDisplayId(String)}, </li>
 * 		<li>{@link Collection#setVersion(String)}, </li>
 * 		<li>{@link Collection#setMembers(Set)}, </li>
 * 		<li>{@link Collection#setWasDerivedFrom(URI)}, </li>
 * 		<li>{@link Identified#setAnnotations(List)}, or</li>
 * 		<li>{@link SBOLDocument#addCollection(Collection)}.</li>
 * 	</ul>
 * </li>
 * </ul>
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private static Collection parseCollection(SBOLDocument SBOLDoc, IdentifiableDocument<QName> topLevel, Map<URI, NestedDocument<QName>> nested) throws SBOLValidationException {
    // URIcompliance.extractDisplayId(topLevel.getIdentity());
    String displayId = null;
    String name = null;
    String description = null;
    // URI.create(URIcompliance.extractPersistentId(topLevel.getIdentity()));
    URI persistentIdentity = null;
    String version = null;
    Set<URI> wasDerivedFroms = new HashSet<>();
    Set<URI> wasGeneratedBys = new HashSet<>();
    Set<URI> attachments = new HashSet<>();
    Set<URI> members = new HashSet<>();
    List<Annotation> annotations = new ArrayList<>();
    for (NamedProperty<QName> namedProperty : topLevel.getProperties()) {
        if (namedProperty.getName().equals(Sbol2Terms.Identified.persistentIdentity)) {
            if (!(namedProperty.getValue() instanceof Literal) || persistentIdentity != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10203", topLevel.getIdentity());
            }
            persistentIdentity = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.version)) {
            if (!(namedProperty.getValue() instanceof Literal) || version != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10206", topLevel.getIdentity());
            }
            version = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.displayId)) {
            if (!(namedProperty.getValue() instanceof Literal) || displayId != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10204", topLevel.getIdentity());
            }
            displayId = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Collection.hasMembers)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-12102", topLevel.getIdentity());
                }
                members.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof NestedDocument) {
                if (((IdentifiableDocument) namedProperty).getType().equals(Sbol2Terms.Collection.Collection))
                    parseCollection(SBOLDoc, (IdentifiableDocument) namedProperty, nested);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.ModuleDefinition.ModuleDefinition))
                    parseModuleDefinition(SBOLDoc, (IdentifiableDocument) namedProperty, nested);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.Model.Model))
                    parseModel(SBOLDoc, (IdentifiableDocument) namedProperty);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.Sequence.Sequence))
                    parseSequence(SBOLDoc, (IdentifiableDocument) namedProperty);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.ComponentDefinition.ComponentDefinition))
                    parseComponentDefinition(SBOLDoc, (IdentifiableDocument) namedProperty, nested);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.CombinatorialDerivation.CombinatorialDerivation))
                    parseCombinatorialDerivation(SBOLDoc, (IdentifiableDocument) namedProperty, nested);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.Implementation.Implementation))
                    parseImplementation(SBOLDoc, (IdentifiableDocument) namedProperty, nested);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.Attachment.Attachment))
                    parseAttachment(SBOLDoc, (IdentifiableDocument) namedProperty);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.Activity.Activity))
                    parseActivity(SBOLDoc, (IdentifiableDocument) namedProperty, nested);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.Agent.Agent))
                    parseAgent(SBOLDoc, (IdentifiableDocument) namedProperty);
                else if (((IdentifiableDocument) namedProperty).equals(Sbol2Terms.Plan.Plan))
                    parsePlan(SBOLDoc, (IdentifiableDocument) namedProperty);
                else
                    parseGenericTopLevel(SBOLDoc, (IdentifiableDocument) namedProperty);
            } else {
                throw new SBOLValidationException("sbol-12102", topLevel.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.title)) {
            if (!(namedProperty.getValue() instanceof Literal) || name != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10212", topLevel.getIdentity());
            }
            name = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.description)) {
            if (!(namedProperty.getValue() instanceof Literal) || description != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10213", topLevel.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasDerivedFrom)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10208", topLevel.getIdentity());
            }
            wasDerivedFroms.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasGeneratedBy)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10221", topLevel.getIdentity());
            }
            wasGeneratedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.TopLevel.hasAttachment)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
                }
                attachments.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof IdentifiableDocument) {
                if (((IdentifiableDocument<QName>) namedProperty).getType().equals(Sbol2Terms.Attachment.Attachment)) {
                    Attachment attachment = parseAttachment(SBOLDoc, (IdentifiableDocument<QName>) namedProperty.getValue());
                    attachments.add(attachment.getIdentity());
                } else {
                    throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
                }
            } else {
                throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Description.type) && ((Literal<QName>) namedProperty.getValue()).getValue().toString().equals(Sbol2Terms.Collection.Collection.getNamespaceURI() + Sbol2Terms.Collection.Collection.getLocalPart())) {
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    Collection c = new Collection(topLevel.getIdentity());
    if (displayId != null)
        c.setDisplayId(displayId);
    if (version != null)
        c.setVersion(version);
    if (persistentIdentity != null)
        c.setPersistentIdentity(persistentIdentity);
    if (!members.isEmpty())
        c.setMembers(members);
    if (name != null)
        c.setName(name);
    if (description != null)
        c.setDescription(description);
    c.setWasDerivedFroms(wasDerivedFroms);
    c.setWasGeneratedBys(wasGeneratedBys);
    c.setAttachments(attachments);
    if (!annotations.isEmpty())
        c.setAnnotations(annotations);
    Collection oldC = SBOLDoc.getCollection(topLevel.getIdentity());
    if (oldC == null) {
        SBOLDoc.addCollection(c);
    } else {
        if (!c.equals(oldC)) {
            throw new SBOLValidationException("sbol-10202", c);
        }
    }
    return c;
}
Also used : StringifyQName(org.sbolstandard.core.io.json.StringifyQName) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI) NestedDocument(org.sbolstandard.core.datatree.NestedDocument) IdentifiableDocument(org.sbolstandard.core.datatree.IdentifiableDocument) Literal(org.sbolstandard.core.datatree.Literal) HashSet(java.util.HashSet)

Example 5 with TopLevel

use of org.sbolstandard.core2.TopLevel in project libSBOLj by SynBioDex.

the class SBOLReader method parseAgent.

@SuppressWarnings("unchecked")
private static Agent parseAgent(SBOLDocument SBOLDoc, IdentifiableDocument<QName> topLevel) throws SBOLValidationException {
    String displayId = null;
    String name = null;
    String description = null;
    URI persistentIdentity = null;
    String version = null;
    Set<URI> wasDerivedFroms = new HashSet<>();
    Set<URI> wasGeneratedBys = new HashSet<>();
    Set<URI> attachments = new HashSet<>();
    List<Annotation> annotations = new ArrayList<>();
    for (NamedProperty<QName> namedProperty : topLevel.getProperties()) {
        if (namedProperty.getName().equals(Sbol2Terms.Identified.persistentIdentity)) {
            if (!(namedProperty.getValue() instanceof Literal) || persistentIdentity != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10203", topLevel.getIdentity());
            }
            persistentIdentity = URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString());
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.version)) {
            if (!(namedProperty.getValue() instanceof Literal) || version != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10206", topLevel.getIdentity());
            }
            version = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.displayId)) {
            if (!(namedProperty.getValue() instanceof Literal) || displayId != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10204", topLevel.getIdentity());
            }
            displayId = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.title)) {
            if (!(namedProperty.getValue() instanceof Literal) || name != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10212", topLevel.getIdentity());
            }
            name = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.description)) {
            if (!(namedProperty.getValue() instanceof Literal) || description != null || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof String))) {
                throw new SBOLValidationException("sbol-10213", topLevel.getIdentity());
            }
            description = ((Literal<QName>) namedProperty.getValue()).getValue().toString();
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasDerivedFrom)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10208", topLevel.getIdentity());
            }
            wasDerivedFroms.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.Identified.wasGeneratedBy)) {
            if (!(namedProperty.getValue() instanceof Literal) || (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI))) {
                throw new SBOLValidationException("sbol-10221", topLevel.getIdentity());
            }
            wasGeneratedBys.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
        } else if (namedProperty.getName().equals(Sbol2Terms.TopLevel.hasAttachment)) {
            if (namedProperty.getValue() instanceof Literal) {
                if (!(((Literal<QName>) namedProperty.getValue()).getValue() instanceof URI)) {
                    throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
                }
                attachments.add(URI.create(((Literal<QName>) namedProperty.getValue()).getValue().toString()));
            } else if (namedProperty.getValue() instanceof IdentifiableDocument) {
                if (((IdentifiableDocument<QName>) namedProperty).getType().equals(Sbol2Terms.Attachment.Attachment)) {
                    Attachment attachment = parseAttachment(SBOLDoc, (IdentifiableDocument<QName>) namedProperty.getValue());
                    attachments.add(attachment.getIdentity());
                } else {
                    throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
                }
            } else {
                throw new SBOLValidationException("sbol-10306", topLevel.getIdentity());
            }
        } else if (namedProperty.getName().equals(Sbol2Terms.Description.type) && ((Literal<QName>) namedProperty.getValue()).getValue().toString().equals(Sbol2Terms.Agent.Agent.getNamespaceURI() + Sbol2Terms.Agent.Agent.getLocalPart())) {
        } else {
            annotations.add(new Annotation(namedProperty));
        }
    }
    Agent t = new Agent(topLevel.getIdentity());
    if (persistentIdentity != null)
        t.setPersistentIdentity(persistentIdentity);
    if (version != null)
        t.setVersion(version);
    if (displayId != null)
        t.setDisplayId(displayId);
    if (name != null)
        t.setName(name);
    if (description != null)
        t.setDescription(description);
    t.setWasDerivedFroms(wasDerivedFroms);
    t.setWasGeneratedBys(wasGeneratedBys);
    t.setAttachments(attachments);
    if (!annotations.isEmpty())
        t.setAnnotations(annotations);
    Agent oldA = SBOLDoc.getAgent(topLevel.getIdentity());
    if (oldA == null) {
        SBOLDoc.addAgent(t);
    } else {
        if (!t.equals(oldA)) {
            throw new SBOLValidationException("sbol-10202", t);
        }
    }
    return t;
}
Also used : StringifyQName(org.sbolstandard.core.io.json.StringifyQName) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) URIcompliance.createCompliantURI(org.sbolstandard.core2.URIcompliance.createCompliantURI) URI(java.net.URI) IdentifiableDocument(org.sbolstandard.core.datatree.IdentifiableDocument) Literal(org.sbolstandard.core.datatree.Literal) HashSet(java.util.HashSet)

Aggregations

URI (java.net.URI)28 URIcompliance.createCompliantURI (org.sbolstandard.core2.URIcompliance.createCompliantURI)25 QName (javax.xml.namespace.QName)21 HashSet (java.util.HashSet)19 ArrayList (java.util.ArrayList)18 Literal (org.sbolstandard.core.datatree.Literal)18 StringifyQName (org.sbolstandard.core.io.json.StringifyQName)18 IdentifiableDocument (org.sbolstandard.core.datatree.IdentifiableDocument)15 NestedDocument (org.sbolstandard.core.datatree.NestedDocument)9 SBOLDocument (org.sbolstandard.core2.SBOLDocument)5 ComponentDefinition (org.sbolstandard.core2.ComponentDefinition)3 GenericTopLevel (org.sbolstandard.core2.GenericTopLevel)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 Test (org.junit.Test)2 Sequence (org.sbolstandard.core2.Sequence)2 FileNotFoundException (java.io.FileNotFoundException)1 DateTime (org.joda.time.DateTime)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1 TopLevel (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.union.rev150121.TopLevel)1