001/* 002 * Copyright 2020 nils.hoffmann. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.isas.lipidomics.palinom.sumformula; 017 018import de.isas.lipidomics.domain.Element; 019import de.isas.lipidomics.domain.ElementTable; 020import de.isas.lipidomics.palinom.SumFormulaBaseVisitor; 021import de.isas.lipidomics.palinom.SumFormulaParser; 022import java.util.Optional; 023 024/** 025 * 026 * Base visitor implementation for the SumFormula grammar. 027 * 028 * Overriding implementation of {@link SumFormulaBaseVisitor}. Creates 029 * {@link ElementTable} instances from the provided context. 030 * 031 * @see SumFormulaVisitorParser 032 * @author nils.hoffmann 033 */ 034public class SumFormulaVisitorImpl extends SumFormulaBaseVisitor<ElementTable> { 035 036 @Override 037 public ElementTable visitMolecule(SumFormulaParser.MoleculeContext ctx) { 038 if (ctx.molecule_rule() != null) { 039 SumFormulaParser.Molecule_groupContext moleculeGroup = ctx.molecule_rule().molecule_group(); 040 return visitMoleculeGroup(moleculeGroup, new ElementTable()); 041 } 042 return new ElementTable(); 043 } 044 045 private ElementTable visitMoleculeGroup(SumFormulaParser.Molecule_groupContext moleculeGroup, ElementTable table) { 046 if (moleculeGroup.element_group() != null) { 047 visitElementGroup(moleculeGroup.element_group(), table); 048 } else if (moleculeGroup.single_element() != null) { 049 visitSingleElement(moleculeGroup.single_element(), table); 050 } else if (moleculeGroup.molecule_group() != null) { 051 moleculeGroup.molecule_group().stream().forEach((moleculeGroupCtx) -> { 052 visitMoleculeGroup(moleculeGroupCtx, table); 053 }); 054 } 055 return table; 056 } 057 058 private ElementTable visitSingleElement(SumFormulaParser.Single_elementContext singleElement, ElementTable table) { 059 Optional<Element> element = Element.forName(singleElement.element().getText()); 060 if (element.isPresent()) { 061 table.increment(element.get()); 062 } 063 return table; 064 } 065 066 private ElementTable visitElementGroup(SumFormulaParser.Element_groupContext elementGroup, ElementTable table) { 067 Integer count = 0; 068 if (elementGroup.count() == null) { 069 count = 1; 070 } else { 071 count = Integer.parseInt(elementGroup.count().getText()); 072 } 073 Optional<Element> element = Element.forName(elementGroup.element().getText()); 074 if (element.isPresent()) { 075 table.incrementBy(element.get(), count); 076 } 077 return table; 078 } 079}