Search in sources :

Example 6 with ConstantTerm

use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm in project Alpha by alpha-asp.

the class IntervalLiteral method getIntervalSubstitutions.

private List<Substitution> getIntervalSubstitutions(Substitution partialSubstitution) {
    List<Substitution> substitutions = new ArrayList<>();
    List<Term> terms = getTerms();
    Term intervalRepresentingVariable = terms.get(1);
    IntervalTerm intervalTerm = (IntervalTerm) terms.get(0);
    // Check whether intervalRepresentingVariable is bound already.
    if (intervalRepresentingVariable instanceof VariableTerm) {
        // Still a variable, generate all elements in the interval.
        for (int i = intervalTerm.getLowerBound(); i <= intervalTerm.getUpperBound(); i++) {
            Substitution ith = new BasicSubstitution(partialSubstitution);
            ith.put((VariableTerm) intervalRepresentingVariable, Terms.newConstant(i));
            substitutions.add(ith);
        }
        return substitutions;
    } else {
        // The intervalRepresentingVariable is bound already, check if it is in the interval.
        if (!(intervalRepresentingVariable instanceof ConstantTerm) || !(((ConstantTerm<?>) intervalRepresentingVariable).getObject() instanceof Integer)) {
            // Term is not bound to an integer constant, not in the interval.
            return Collections.emptyList();
        }
        // TODO to avoid that type of unchecked cast, we may want interval terms to not extend AbstractTerm
        @SuppressWarnings("unchecked") Integer integer = ((ConstantTerm<Integer>) intervalRepresentingVariable).getObject();
        if (intervalTerm.getLowerBound() <= integer && integer <= intervalTerm.getUpperBound()) {
            return Collections.singletonList(partialSubstitution);
        }
        return Collections.emptyList();
    }
}
Also used : Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ArrayList(java.util.ArrayList) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 7 with ConstantTerm

use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm in project Alpha by alpha-asp.

the class AspStandardLibrary method datetimeParse.

/**
 * Parses a string representing a datetime without time-zone and returns the
 * year, month, day, hours, minutes and seconds as separate symbolic integer
 * terms.
 * Example:
 *
 * <pre>
 * 		A valid ground instance of the atom &stdlib_datetime_parse[DTSTR, "dd.mm.yyyy hh:MM:ss"](YEAR, MONTH, DAY, HOUR, MIN, SEC)
 * 		would be: &stdlib_datetime_parse["20.05.2020 01:19:13", "dd.mm.yyyy hh:MM:ss"](2020, 5, 20, 1, 19, 13)
 * </pre>
 *
 * Timezones are not supported by this function. Datetime values are parsed
 * using {@link LocalDateTime.parse}.
 *
 * @param datetime a string representing a datetime without time zone
 *                 information
 * @param format   a format string that is accepted by {@link DateTimeFormatter}
 * @return a 6-value integer tuple of format (YEAR, MONTH, DAY, HOUR, MIN, SEC)
 */
@Predicate(name = "stdlib_datetime_parse")
public static Set<List<ConstantTerm<Integer>>> datetimeParse(String dtstr, String format) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    LocalDateTime datetime = LocalDateTime.parse(dtstr, formatter);
    List<ConstantTerm<Integer>> terms = Terms.asTermList(datetime.getYear(), datetime.getMonth().getValue(), datetime.getDayOfMonth(), datetime.getHour(), datetime.getMinute(), datetime.getSecond());
    return Collections.singleton(terms);
}
Also used : LocalDateTime(java.time.LocalDateTime) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) DateTimeFormatter(java.time.format.DateTimeFormatter) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate)

Example 8 with ConstantTerm

use of at.ac.tuwien.kr.alpha.api.terms.ConstantTerm in project Alpha by alpha-asp.

the class AlphaImplTest method withNegatedExternal.

@Test
@SuppressWarnings("unchecked")
public void withNegatedExternal() throws IOException {
    Alpha alpha = new AlphaImpl();
    ASPCore2Program prog = alpha.readProgram(InputConfig.forString(NEGATED_EXTERNAL_ASP));
    Set<AnswerSet> answerSets = alpha.solve(prog).collect(Collectors.toSet());
    assertEquals(31, answerSets.size());
    // Verify every result string has length 6 and contains "foo"
    for (AnswerSet as : answerSets) {
        for (Atom atom : as.getPredicateInstances(Predicates.getPredicate("resultstring", 1))) {
            String resultstring = ((ConstantTerm<String>) atom.getTerms().get(0)).getObject();
            LOGGER.debug("ResultString is {}", resultstring);
            assertEquals(6, resultstring.length());
            assertTrue(resultstring.contains("foo"));
        }
    }
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Test(org.junit.jupiter.api.Test)

Aggregations

ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)5 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)4 ArrayList (java.util.ArrayList)4 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)3 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)3 Test (org.junit.jupiter.api.Test)3 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)2 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)2 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)2 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)2 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)2 List (java.util.List)2 Predicate (at.ac.tuwien.kr.alpha.api.externals.Predicate)1 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)1 ArithmeticTerm (at.ac.tuwien.kr.alpha.api.terms.ArithmeticTerm)1 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)1 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)1 AnswerSets (at.ac.tuwien.kr.alpha.commons.AnswerSets)1 Predicates (at.ac.tuwien.kr.alpha.commons.Predicates)1