All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.boreal.io.dlgp.DlgpUtil Maven / Gradle / Ivy

The newest version!
package fr.boreal.io.dlgp;

import java.util.HashSet;
import java.util.Set;

import fr.boreal.io.api.ParseException;
import fr.boreal.model.formula.api.FOFormula;
import fr.boreal.model.logicalElements.api.Atom;
import fr.boreal.model.query.api.FOQuery;
import fr.boreal.model.rule.api.FORule;

/**
 * 
 * Util methods for DLGP parsing
 * 
 * @author Florent Tornil
 *
 */
public class DlgpUtil {

	/**
	 * Parses atoms from a dlgp String.
	 * 
	 * @param dlgp a dlgp-formatted String.
	 * @return the atoms.
	 * @throws ParseException if an exception occur while parsing DLGP input data
	 */
	public static Set parseFacts(String dlgp) throws ParseException {
		Set atoms = new HashSet<>();
		DlgpParser dlgp_parser = new DlgpParser(dlgp);
		while (dlgp_parser.hasNext()) {
			Object result = dlgp_parser.next();
			if (result instanceof Atom a) {
				atoms.add(a);
			}
		}
		dlgp_parser.close();
		return atoms;
	}

	/**
	 * Parses rules from a dlgp String.
	 * 
	 * @param dlgp a dlgp-formatted String.
	 * @return the rules.
	 * @throws ParseException if an exception occur while parsing DLGP input data
	 */
	public static Set parseRules(String dlgp) throws ParseException {
		Set rules = new HashSet<>();
		DlgpParser dlgp_parser = new DlgpParser(dlgp);
		while (dlgp_parser.hasNext()) {
			Object result = dlgp_parser.next();
			if (result instanceof FORule r) {
				rules.add(r);
			}
		}
		dlgp_parser.close();
		return rules;
	}

	/**
	 * Parses queries from a dlgp String.
	 * 
	 * @param dlgp a dlgp-formatted String.
	 * @return the queries.
	 * @throws ParseException if an exception occur while parsing DLGP input data
	 */
	public static Set> parseQueries(String dlgp) throws ParseException {
		Set> queries = new HashSet<>();
		DlgpParser dlgp_parser = new DlgpParser(dlgp);
		while (dlgp_parser.hasNext()) {
			Object result = dlgp_parser.next();
			if (result instanceof FOQuery q) {
				queries.add(q);
			}
		}
		dlgp_parser.close();
		return queries;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy