Search in sources :

Example 6 with StringInputStream

use of org.apache.tools.ant.filters.StringInputStream in project Gemma by PavlidisLab.

the class ExpressionExperimentFormController method updateBioMaterialMap.

/**
 * Change the relationship between bioassays and biomaterials.
 *
 * @param request              request
 * @param expressionExperiment ee
 * @return true if there were changes
 */
private boolean updateBioMaterialMap(HttpServletRequest request, ExpressionExperiment expressionExperiment) {
    // parse JSON-serialized map
    String jsonSerialization = request.getParameter("assayToMaterialMap");
    // convert back to a map
    Map<String, JSONValue> bioAssayMap;
    try (StringInputStream aStream = new StringInputStream(jsonSerialization)) {
        JSONParser parser = new JSONParser(aStream);
        bioAssayMap = ((JSONObject) parser.nextValue()).getValue();
    } catch (IOException | ANTLRException e) {
        throw new RuntimeException(e);
    }
    Map<BioAssay, BioMaterial> deleteAssociations = new HashMap<>();
    Set<Entry<String, JSONValue>> bioAssays = bioAssayMap.entrySet();
    boolean anyChanges = false;
    int newBioMaterialCount = 0;
    for (Entry<String, JSONValue> entry : bioAssays) {
        // if it is, skip over this entry
        if (entry.getKey().equalsIgnoreCase("nullElement")) {
            continue;
        }
        Long bioAssayId = Long.parseLong(entry.getKey());
        JSONValue value = entry.getValue();
        Long newMaterialId;
        if (value.isString()) {
            newMaterialId = Long.parseLong(((JSONString) value).getValue());
        } else {
            newMaterialId = ((JSONInteger) value).getValue().longValue();
        }
        // maybe we need to do
        BioAssay bioAssay = bioAssayService.load(bioAssayId);
        if (bioAssay == null) {
            throw new IllegalArgumentException("Bioassay with id=" + bioAssayId + " was not associated with the experiment");
        }
        BioMaterial currentBioMaterial = bioAssay.getSampleUsed();
        if (newMaterialId.equals(currentBioMaterial.getId())) {
            // / no change
            continue;
        }
        BioMaterial newMaterial;
        if (newMaterialId < 0) {
            // This kludge signifies that it is a 'brand new' biomaterial.
            newMaterial = bioMaterialService.copy(currentBioMaterial);
            newMaterial.setName("Modeled after " + currentBioMaterial.getName());
            newMaterial.getFactorValues().clear();
            newMaterial = (BioMaterial) persisterHelper.persist(newMaterial);
            newBioMaterialCount++;
        } else {
            // FIXME can we just use this from the experiment, probably no need to fetch it again.
            newMaterial = bioMaterialService.load(newMaterialId);
            if (newMaterial == null) {
                throw new IllegalArgumentException("BioMaterial with id=" + newMaterialId + " could not be loaded");
            }
        }
        anyChanges = true;
        BaseFormController.log.info("Associating " + bioAssay + " with " + newMaterial);
        bioAssayService.addBioMaterialAssociation(bioAssay, newMaterial);
    }
    if (anyChanges) {
        /*
             * FIXME Decide if we need to remove the biomaterial -> factor value associations, it could be completely
             * fouled up.
             */
        BaseFormController.log.info("There were changes to the BioMaterial -> BioAssay map");
        this.audit(expressionExperiment, BioMaterialMappingUpdate.Factory.newInstance(), // remove unnecessary biomaterial associations
        newBioMaterialCount + " biomaterials");
        Collection<BioAssay> deleteKeys = deleteAssociations.keySet();
        for (BioAssay assay : deleteKeys) {
            /*
                 * BUG: if this fails, we end up with a useless extra biomaterial associated with the bioassay.
                 */
            bioAssayService.removeBioMaterialAssociation(assay, deleteAssociations.get(assay));
        }
    } else {
        BaseFormController.log.info("There were no changes to the BioMaterial -> BioAssay map");
    }
    return anyChanges;
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) JSONInteger(com.sdicons.json.model.JSONInteger) JSONString(com.sdicons.json.model.JSONString) IOException(java.io.IOException) JSONValue(com.sdicons.json.model.JSONValue) ANTLRException(antlr.ANTLRException) StringInputStream(org.apache.tools.ant.filters.StringInputStream) Entry(java.util.Map.Entry) JSONParser(com.sdicons.json.parser.JSONParser) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay) JSONString(com.sdicons.json.model.JSONString)

Example 7 with StringInputStream

use of org.apache.tools.ant.filters.StringInputStream in project gora by apache.

the class StorageConfiguration method getGoraPropertiesAsProperties.

/**
 * Returns the properties data in a Properties instacne
 * @return
 * @throws IOException
 */
public Properties getGoraPropertiesAsProperties() throws IOException {
    Properties properties = new Properties();
    properties.load(new StringInputStream(this.goraProperties));
    if (this.getMapping() != null) {
        properties.setProperty("gora.mapping", mapping);
    }
    return properties;
}
Also used : StringInputStream(org.apache.tools.ant.filters.StringInputStream) Properties(java.util.Properties)

Example 8 with StringInputStream

use of org.apache.tools.ant.filters.StringInputStream in project kie-wb-common by kiegroup.

the class DMNMarshallerStandaloneTest method roundTripUnmarshalThenMarshalUnmarshal.

public void roundTripUnmarshalThenMarshalUnmarshal(InputStream dmnXmlInputStream, Consumer<Graph<?, Node<?, ?>>> checkGraphConsumer) throws IOException {
    DMNMarshallerStandalone m = getDMNMarshaller();
    // first unmarshal from DMN XML to Stunner DMN Graph
    @SuppressWarnings("unchecked") Graph<?, Node<?, ?>> g = m.unmarshall(createMetadata(), dmnXmlInputStream);
    checkGraphConsumer.accept(g);
    // round trip to Stunner DMN Graph back to DMN XML
    DiagramImpl diagram = createDiagram();
    diagram.setGraph(g);
    String mString = m.marshall(diagram);
    LOG.debug(mString);
    // now unmarshal once more, from the marshalled just done above, back again to Stunner DMN Graph to complete check for round-trip
    @SuppressWarnings("unchecked") Graph<?, Node<?, ?>> g2 = m.unmarshall(createMetadata(), new StringInputStream(mString));
    checkGraphConsumer.accept(g2);
}
Also used : StringInputStream(org.apache.tools.ant.filters.StringInputStream) DiagramImpl(org.kie.workbench.common.stunner.core.diagram.DiagramImpl) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) Node(org.kie.workbench.common.stunner.core.graph.Node)

Example 9 with StringInputStream

use of org.apache.tools.ant.filters.StringInputStream in project kie-wb-common by kiegroup.

the class DMNMarshallerStandaloneTest method marshallAndUnMarshallConnectors.

@SuppressWarnings("unchecked")
private ViewConnector marshallAndUnMarshallConnectors(final org.kie.workbench.common.stunner.core.graph.content.Bounds bounds, final String decisionNode1UUID, final String decisionNode2UUID, final String edgeUUID, final MagnetConnection edgeSourceConnection, final MagnetConnection edgeTargetConnection, final Consumer<MagnetConnection> sourceMagnetConsumer, final Consumer<MagnetConnection> targetMagnetConsumer) throws Exception {
    final DMNMarshallerStandalone marshaller = getDMNMarshaller();
    final Diagram<Graph, Metadata> mockedDiagram = connectTwoNodes(bounds, decisionNode1UUID, decisionNode2UUID, edgeUUID, edgeSourceConnection, edgeTargetConnection, sourceMagnetConsumer, targetMagnetConsumer);
    final String marshalledSource = marshaller.marshall(mockedDiagram);
    final Graph<?, Node<View, ?>> unmarshalledGraph = marshaller.unmarshall(createMetadata(), new StringInputStream(marshalledSource));
    assertNotNull(unmarshalledGraph);
    final Node<?, ?> decision1Node = unmarshalledGraph.getNode(decisionNode1UUID);
    final Node<?, ?> decision2Node = unmarshalledGraph.getNode(decisionNode2UUID);
    assertNotNull(decision1Node);
    assertNotNull(decision2Node);
    assertEquals(1, decision1Node.getOutEdges().size());
    assertEquals(2, decision2Node.getInEdges().size());
    final Edge decision1NodeOutEdge = decision1Node.getOutEdges().get(0);
    final Edge decision2NodeInEdge = decision2Node.getInEdges().get(0);
    assertEquals(decision1NodeOutEdge, decision2NodeInEdge);
    final ViewConnector edgeView = (ViewConnector) decision1NodeOutEdge.getContent();
    assertTrue(edgeView.getSourceConnection().isPresent());
    assertTrue(edgeView.getTargetConnection().isPresent());
    return edgeView;
}
Also used : StringInputStream(org.apache.tools.ant.filters.StringInputStream) Graph(org.kie.workbench.common.stunner.core.graph.Graph) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) Node(org.kie.workbench.common.stunner.core.graph.Node) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) PMMLDocumentMetadata(org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata) Edge(org.kie.workbench.common.stunner.core.graph.Edge) DMNEdge(org.kie.dmn.model.api.dmndi.DMNEdge)

Example 10 with StringInputStream

use of org.apache.tools.ant.filters.StringInputStream in project kie-wb-common by kiegroup.

the class DMNMarshallerTest method roundTripUnmarshalThenMarshalUnmarshal.

public void roundTripUnmarshalThenMarshalUnmarshal(InputStream dmnXmlInputStream, Consumer<Graph<?, Node<?, ?>>> checkGraphConsumer) throws IOException {
    DMNMarshaller m = new DMNMarshaller(new XMLEncoderDiagramMetadataMarshaller(), applicationFactoryManager);
    // first unmarshal from DMN XML to Stunner DMN Graph
    @SuppressWarnings("unchecked") Graph<?, Node<?, ?>> g = m.unmarshall(null, dmnXmlInputStream);
    checkGraphConsumer.accept(g);
    // round trip to Stunner DMN Graph back to DMN XML
    DiagramImpl diagram = new DiagramImpl("", null);
    diagram.setGraph(g);
    String mString = m.marshall(diagram);
    System.out.println(mString);
    // now unmarshal once more, from the marshalled just done above, back again to Stunner DMN Graph to complete check for round-trip
    @SuppressWarnings("unchecked") Graph<?, Node<?, ?>> g2 = m.unmarshall(null, new StringInputStream(mString));
    checkGraphConsumer.accept(g2);
}
Also used : StringInputStream(org.apache.tools.ant.filters.StringInputStream) XMLEncoderDiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.backend.service.XMLEncoderDiagramMetadataMarshaller) DiagramImpl(org.kie.workbench.common.stunner.core.diagram.DiagramImpl) Node(org.kie.workbench.common.stunner.core.graph.Node) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) Matchers.anyString(org.mockito.Matchers.anyString)

Aggregations

StringInputStream (org.apache.tools.ant.filters.StringInputStream)17 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 Test (org.junit.Test)5 IFile (org.eclipse.core.resources.IFile)4 Node (org.kie.workbench.common.stunner.core.graph.Node)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 IFolder (org.eclipse.core.resources.IFolder)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 PMMLDocumentMetadata (org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata)2 DiagramImpl (org.kie.workbench.common.stunner.core.diagram.DiagramImpl)2 Metadata (org.kie.workbench.common.stunner.core.diagram.Metadata)2 Graph (org.kie.workbench.common.stunner.core.graph.Graph)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Statistics (org.opensolaris.opengrok.web.Statistics)2 Document (org.w3c.dom.Document)2 SAXException (org.xml.sax.SAXException)2