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();
}
}
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);
}
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"));
}
}
}
Aggregations