org.tweetyproject.preferences.io.UPParser.jj Maven / Gradle / Ivy
The newest version!
/*
* This file is part of "TweetyProject", a collection of Java libraries for
* logical aspects of artificial intelligence and knowledge representation.
*
* Tweety is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* Copyright 2016 The Tweety Project Team
*/
/**
* JavaCC file
*/
options
{
JDK_VERSION = "1.6";
STATIC = false;
}
PARSER_BEGIN(UPParser)
package org.tweetyproject.preferences.io;
import java.util.*;
import org.tweetyproject.preferences.*;
import org.tweetyproject.preferences.update.Update;
import org.tweetyproject.preferences.Operation;
/**
Update Parser for reading updates for dynamic Preference Aggregation
Please note: update file syntax changed from
(index, operation, amount, element)
e.g. (1, WEAKEN, 2, a)
to
(index, operation(amount), element)
e.g. (1, -2, a)
where
-(/+) is equal to WEAKEN(/STRENGTHEN)
and 2 is the amount
*/
@SuppressWarnings("all")
public class UPParser
{
public UPParser()
{
}
public static ArrayList> parse(String updatefile) throws ParseException, java.io.FileNotFoundException
{
UPParser parser;
parser = new UPParser(new java.io.FileInputStream(updatefile));
return parser.getUpdate();
}
public static void main(String args []) throws ParseException
{
try
{
ArrayList> testUP = new ArrayList>();
testUP = parse("/home/bwolf/Universität/Tweety/tweety-code/projects/net-sf-tweety-preferences/src/main/java/net/sf/tweety/preferences/io/test.up");
System.out.println(testUP);
}
catch (Exception e)
{
System.out.println("error while parsing: ");
e.printStackTrace();
}
}
}
PARSER_END(UPParser)
SKIP :
{
" "
}
TOKEN : /* OPERATORS */
{
< LBRA : "(" >
| < RBRA : ")" >
| < INDEX :
(
(["0"-"9"]) | (["1"-"9"]) ( ["0"-"9"])+
) >
| < OPERATION : ("+"["1"-"9"]) | ("-"["1"-"9"])>
| < ELEMENT : ([ "a"-"z" ])+ >
/**
| < AMOUNT :
(
([ "1"-"9" ])
)
(
([ "0"-"9" ])
)* >
*/
| < EOL :
"\n"
| "\r\n"
| "\t" >
}
ArrayList > getUpdate() throws java.io.FileNotFoundException :
{
Token index, op, element;
ArrayList < Update < String >> updates = new ArrayList < Update < String >> ();
}
{
(
< LBRA >
(
index = < INDEX > (",")? op = < OPERATION > (",")? element = < ELEMENT >
{
Integer ind = Integer.parseInt(index.image);
// finding element to perform update on
String elm = element.image;
Integer amnt = Integer.parseInt(op.image.substring(1));
if (op.image.startsWith("-"))
{
Update < String > tmp = new Update(ind, Operation.WEAKEN, amnt, elm);
updates.add(tmp);
}
else if (op.image.startsWith("+"))
{
Update < String > tmp = new Update(ind, Operation.STRENGTHEN, amnt, elm);
updates.add(tmp);
}
else
{
continue;
}
}
< RBRA >
)
< EOL >
)+
{
return updates;
}
}