...
 
Commits (3)
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
package fr.tetras_libre.unltools.unl.builders;
import fr.tetras_libre.unltools.unl.Graph;
import java.util.function.Function;
public class GraphBuilder {
Graph graph;
private GraphBuilder() {
this.graph = new Graph();
}
public static IBuild graph() {
return new GraphBuilder.Builder();
}
public interface IBuild {
IBuild addGraphRelation(Function<GraphRelationBuilder.RequiredLabel, GraphRelationBuilder.IBuild> c);
Graph buildGraph();
}
private static class Builder implements IBuild {
private final GraphBuilder instance;
public Builder() {
instance = new GraphBuilder();
}
@Override
public IBuild addGraphRelation(Function<GraphRelationBuilder.RequiredLabel, GraphRelationBuilder.IBuild> c) {
instance.graph.add(c.apply(GraphRelationBuilder.createBuilder()).buildGraphRelation());
return this;
}
@Override
public Graph buildGraph() {
return instance.graph;
}
}
}
package fr.tetras_libre.unltools.unl.builders;
import fr.tetras_libre.unltools.unl.GraphNode;
import fr.tetras_libre.unltools.unl.GraphRelation;
import java.util.function.Function;
public class GraphRelationBuilder {
private String label;
private String graphReference;
private GraphNode node1;
private GraphNode node2;
public static RequiredLabel createBuilder() {
return new GraphRelationBuilder.Builder();
}
public interface RequiredLabel {
OptionalGraphReference label(String label);
}
public interface OptionalGraphReference {
RequiredLeftNode reference(String nodeName);
RequiredLeftNode noReference();
}
public interface RequiredLeftNode {
RequiredRightNode leftNode(Function<UniversalWordNodeBuilder.IRequiredUniversalWord, UniversalWordNodeBuilder.IBuild> f);
IBuild rightSubGraphNode(Function<SubGraphReferenceNodeBuilder.IRequiredReferenceNumber, SubGraphReferenceNodeBuilder.IBuild> f);
}
public interface RequiredRightNode {
IBuild rightNode(Function<UniversalWordNodeBuilder.IRequiredUniversalWord, UniversalWordNodeBuilder.IBuild> f);
IBuild rightSubGraphNode(Function<SubGraphReferenceNodeBuilder.IRequiredReferenceNumber, SubGraphReferenceNodeBuilder.IBuild> f);
}
public interface IBuild {
GraphRelation buildGraphRelation();
}
private static class Builder implements RequiredLabel, OptionalGraphReference, RequiredLeftNode, RequiredRightNode, IBuild {
private final GraphRelationBuilder instance;
public Builder() {
instance = new GraphRelationBuilder();
}
@Override
public RequiredLeftNode reference(String nodeName) {
instance.graphReference = nodeName;
return this;
}
@Override
public RequiredLeftNode noReference() {
instance.graphReference = "";
return this;
}
@Override
public RequiredRightNode leftNode(Function<UniversalWordNodeBuilder.IRequiredUniversalWord, UniversalWordNodeBuilder.IBuild> f) {
this.instance.node1 = f.apply(UniversalWordNodeBuilder.createBuilder()).buildUniversalWordNode();
return this;
}
@Override
public IBuild rightSubGraphNode(Function<SubGraphReferenceNodeBuilder.IRequiredReferenceNumber, SubGraphReferenceNodeBuilder.IBuild> f) {
instance.node2 = f.apply(SubGraphReferenceNodeBuilder.createBuilder()).buildSubGraphReferenceNode();
return this;
}
@Override
public GraphRelation buildGraphRelation() {
return new GraphRelation(instance.node1, instance.node2, instance.label, instance.graphReference);
}
@Override
public OptionalGraphReference label(String label) {
instance.label = label;
return this;
}
@Override
public IBuild rightNode(Function<UniversalWordNodeBuilder.IRequiredUniversalWord, UniversalWordNodeBuilder.IBuild> f) {
instance.node2 = f.apply(UniversalWordNodeBuilder.createBuilder()).buildUniversalWordNode();
return this;
}
}
}
package fr.tetras_libre.unltools.unl.builders;
import fr.tetras_libre.unltools.unl.Restriction;
import fr.tetras_libre.unltools.unl.UniversalWord;
public class RestrictionBuilder {
private String label;
private char orientation;
private UniversalWord universalWord;
private RestrictionBuilder() {
label = "";
orientation = ' ';
universalWord = null;
}
public interface IRequiredLabel {
IOrientation label(String label);
}
public static Restriction simpleRestriction(String label, RestrictionOrientation orientation, String headwordLabel) {
return RestrictionBuilder
.createBuilder()
.label(label)
.withOrientation(orientation)
.universalWord(UniversalWordBuilder.simpleUniversalWord(headwordLabel))
.buildRestriction();
}
public static IRequiredLabel createBuilder() {
return new RestrictionBuilder.Builder();
}
public static Restriction ternaryTransitiveRestriction(String label1, String label2, String label3, RestrictionOrientation orientation) {
return RestrictionBuilder
.createBuilder()
.label(label1)
.withOrientation(orientation)
.universalWord(
UniversalWordBuilder
.createBuilder()
.headWord(label2)
.addSimpleRestriction(label1, orientation, label3 )
.buildUniversalWord()
)
.buildRestriction();
}
public interface IOrientation {
IRequiredUniversalWord withOrientation(RestrictionOrientation orientation);
}
public interface IRequiredUniversalWord {
IBuild universalWord(UniversalWord universalWord);
}
public interface IBuild {
Restriction buildRestriction();
}
private static class Builder implements IRequiredLabel, IRequiredUniversalWord, IOrientation, IBuild {
private final RestrictionBuilder instance;
public Builder(){
instance = new RestrictionBuilder();
}
@Override
public IBuild universalWord(UniversalWord universalWord) {
instance.universalWord = universalWord;
return this;
}
@Override
public IRequiredUniversalWord withOrientation(RestrictionOrientation orientation) {
switch (orientation) {
case Left:
instance.orientation = '<';
break;
case None:
instance.orientation = ' ';
break;
case Right:
instance.orientation = '>';
break;
}
return this;
}
@Override
public Restriction buildRestriction() {
return new Restriction(instance.label, instance.orientation, instance.universalWord);
}
@Override
public IOrientation label(String label) {
if(null == label)
throw new IllegalArgumentException();
instance.label = label;
return this;
}
}
}
package fr.tetras_libre.unltools.unl.builders;
public enum RestrictionOrientation {
None,
Right,
Left
}
package fr.tetras_libre.unltools.unl.builders;
import fr.tetras_libre.unltools.unl.SubGraphReferenceNode;
import java.util.HashSet;
public class SubGraphReferenceNodeBuilder {
private String referenceNumber;
private HashSet<String> attributes;
public static IRequiredReferenceNumber createBuilder()
{
return new SubGraphReferenceNodeBuilder.Builder();
}
private SubGraphReferenceNodeBuilder() {
referenceNumber = null;
attributes = new HashSet<>();
}
public interface IRequiredReferenceNumber {
IBuild referenceNumber(String referenceNumber);
}
public interface IBuild {
IBuild addAttribute(String attribute);
SubGraphReferenceNode buildSubGraphReferenceNode();
}
private static class Builder implements IRequiredReferenceNumber, IBuild {
SubGraphReferenceNodeBuilder instance;
public Builder() {
instance = new SubGraphReferenceNodeBuilder();
}
@Override
public IBuild referenceNumber(String referenceNumber) {
if (null == referenceNumber || referenceNumber.isBlank())
throw new IllegalArgumentException();
this.instance.referenceNumber = referenceNumber;
return this;
}
@Override
public IBuild addAttribute(String attribute) {
if (null != attribute && !attribute.isBlank())
this.instance.attributes.add(attribute);
return this;
}
@Override
public SubGraphReferenceNode buildSubGraphReferenceNode() {
return new SubGraphReferenceNode(instance.referenceNumber, instance.attributes);
}
}
}
package fr.tetras_libre.unltools.unl.builders;
import fr.tetras_libre.unltools.unl.Restriction;
import fr.tetras_libre.unltools.unl.UniversalWord;
import java.util.TreeSet;
import java.util.function.Function;
public class UniversalWordBuilder {
private final TreeSet<Restriction> restrictions;
private String headWord;
private UniversalWordBuilder() {
restrictions = new TreeSet<>();
}
public static IRequiredHeadWord createBuilder() {
return new UniversalWordBuilder.Builder();
}
public static UniversalWord simpleUniversalWord(String headWord) {
return UniversalWordBuilder.createBuilder()
.headWord(headWord)
.buildUniversalWord();
}
public interface IBuild {
IBuild addRestriction(Function<RestrictionBuilder.IRequiredLabel, RestrictionBuilder.IBuild> f);
IBuild addSimpleRestriction(String label1, RestrictionOrientation orientation, String label2);
IBuild addTernaryTransitiveRestriction(String firstLabel, String secondLabel, String thirdLabel, RestrictionOrientation orientation);
UniversalWord buildUniversalWord();
}
public interface IRequiredHeadWord {
IBuild headWord(String headword);
}
private static class Builder implements IRequiredHeadWord, IBuild {
private final UniversalWordBuilder instance;
Builder() {
instance = new UniversalWordBuilder();
}
@Override
public IBuild addRestriction(Function<RestrictionBuilder.IRequiredLabel, RestrictionBuilder.IBuild> f) {
instance.restrictions.add(f.apply(RestrictionBuilder.createBuilder()).buildRestriction());
return this;
}
@Override
public IBuild addSimpleRestriction(String label1, RestrictionOrientation orientation, String label2) {
instance.restrictions.add(RestrictionBuilder.simpleRestriction(label1, orientation, label2));
return this;
}
@Override
public IBuild addTernaryTransitiveRestriction(String firstLabel, String secondLabel, String thirdLabel, RestrictionOrientation orientation) {
instance.restrictions.add(RestrictionBuilder.ternaryTransitiveRestriction(firstLabel, secondLabel, thirdLabel, orientation));
return this;
}
@Override
public UniversalWord buildUniversalWord() {
return new UniversalWord(instance.headWord, instance.restrictions);
}
@Override
public IBuild headWord(String headword) {
if (null == headword)
throw new IllegalArgumentException();
instance.headWord = headword;
return this;
}
}
}
package fr.tetras_libre.unltools.unl.builders;
import fr.tetras_libre.unltools.unl.UniversalWord;
import fr.tetras_libre.unltools.unl.UniversalWordNode;
import java.util.HashSet;
import java.util.function.Function;
import java.util.function.Supplier;
public class UniversalWordNodeBuilder {
private final HashSet<String> attributes;
private UniversalWord universalWord;
private String instanceNumber;
private UniversalWordNodeBuilder() {
this.attributes = new HashSet<>();
this.instanceNumber = "";
}
public static IRequiredUniversalWord createBuilder() {
return new UniversalWordNodeBuilder.Builder();
}
public interface IRequiredUniversalWord {
IBuild universalWord(Function<UniversalWordBuilder.IRequiredHeadWord, UniversalWordBuilder.IBuild> f);
IBuild simpleUniversalWord(Supplier<String> s);
}
public interface IBuild {
IBuild addAttribute(String attribute);
IBuild instanceNumber(String nodeReference);
UniversalWordNode buildUniversalWordNode();
}
private static class Builder implements IRequiredUniversalWord, IBuild {
private final UniversalWordNodeBuilder instance;
public Builder() {
this.instance = new UniversalWordNodeBuilder();
}
@Override
public IBuild addAttribute(String attribute) {
if (null != attribute
&& !attribute.isBlank())
this.instance.attributes.add(attribute);
return this;
}
@Override
public IBuild instanceNumber(String nodeReference) {
if(null == nodeReference){
throw new IllegalArgumentException();
}
this.instance.instanceNumber = nodeReference;
return this;
}
@Override
public UniversalWordNode buildUniversalWordNode() {
return new UniversalWordNode(instance.universalWord, instance.attributes, instance.instanceNumber);
}
@Override
public IBuild universalWord(Function<UniversalWordBuilder.IRequiredHeadWord, UniversalWordBuilder.IBuild> f) {
instance.universalWord = f.apply(UniversalWordBuilder.createBuilder()).buildUniversalWord();
return this;
}
@Override
public IBuild simpleUniversalWord(Supplier<String> s) {
instance.universalWord = UniversalWordBuilder.simpleUniversalWord(s.get());
return this;
}
}
}
package fr.tetras_libre.unltools.unl.builders;
import fr.tetras_libre.unltools.unl.Graph;
import fr.tetras_libre.unltools.unl.UnlDocumentNode;
import fr.tetras_libre.unltools.unl.UnlDocumentNodeType;
import java.util.function.Function;
public class UnlDocumentNodeBuilder {
private UnlDocumentNodeType unlDocumentNodeType;
private String docNodeLabel;
private Graph graph;
private UnlDocumentNodeBuilder() {
docNodeLabel = null;
graph = null;
}
public static IUnlDocumentNodeType createBuilder() {
return new UnlDocumentNodeBuilder.Builder();
}
public interface IUnlDocumentNodeType {
ILabel withNodeType(UnlDocumentNodeType nodeType);
}
public interface ILabel {
IGraph withLabel(String label);
}
public interface IGraph {
IBuild withGraph(Function<GraphBuilder.IBuild, GraphBuilder.IBuild> f);
}
public interface IBuild {
UnlDocumentNode buildUnlDocumentNode();
}
private static class Builder implements IBuild, ILabel, IGraph, IUnlDocumentNodeType {
private final UnlDocumentNodeBuilder instance;
Builder() {
instance = new UnlDocumentNodeBuilder();
}
@Override
public UnlDocumentNode buildUnlDocumentNode() {
return new UnlDocumentNode(instance.unlDocumentNodeType, instance.docNodeLabel, instance.graph);
}
@Override
public IGraph withLabel(String label) {
if (null == label || label.isBlank())
throw new IllegalArgumentException();
instance.docNodeLabel = label;
return this;
}
@Override
public IBuild withGraph(Function<GraphBuilder.IBuild, GraphBuilder.IBuild> f) {
if (null != instance.graph)
throw new IllegalStateException();
instance.graph = f.apply(GraphBuilder.graph()).buildGraph();
return this;
}
@Override
public ILabel withNodeType(UnlDocumentNodeType nodeType) {
instance.unlDocumentNodeType = nodeType;
return this;
}
}
}
......@@ -72,7 +72,7 @@ TOKEN : /* Character classes */
// QUESTION: Quels sont les caractères officiellement acceptés dans les headwords...
| <#HEADWORD_CHARACTERS : ( <ALPHANUM> | "'" | "#" | "&" | "/" |
["\u00c0"-"\u00ff"] |
("." ~["@"]) ) >
("." ~["@"]) | "=" ) >
// | <#HEADWORD_CHARACTERS : ( <ALPHANUM> | "'" | "#" | "&" | "/" | "+" | "@" | "=" | "$" | "�" | "�" | "[" | "]" |
// "\u221a" | "\u00a9" |
......
/* UnlParserTokenManager.java */
/* Generated By:JavaCC: Do not edit this line. UnlParserTokenManager.java */
package fr.tetras_libre.unltools.unl.parser;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Set;
import java.util.Vector;
import fr.tetras_libre.unltools.unl.*;
/** Token Manager. */
@SuppressWarnings ("unused")
......@@ -201,7 +209,7 @@ private int jjMoveNfa_0(int startState, int curPos)
switch(jjstateSet[--i])
{
case 0:
if ((0x3ffa0c800000000L & l) != 0L)
if ((0x23ffa0c800000000L & l) != 0L)
{
if (kind > 37)
kind = 37;
......@@ -274,7 +282,7 @@ private int jjMoveNfa_0(int startState, int curPos)
kind = 36;
break;
case 21:
if ((0x3ffa0c800000000L & l) == 0L)
if ((0x23ffa0c800000000L & l) == 0L)
break;
if (kind > 37)
kind = 37;
......
package fr.tetras_libre.unltools.unl;
import fr.tetras_libre.unltools.unl.builders.RestrictionOrientation;
import fr.tetras_libre.unltools.unl.builders.UnlDocumentNodeBuilder;
import java.util.*;
public class CatsUnl extends AbstractFlyweightUnlArgumentProvider implements GraphImporter {
public static UnlDocumentNode buildSentenceNode() {
private static UnlDocumentNode CreateWithOldBuilder() {
var graph = new Graph();
// mod
......@@ -62,6 +65,110 @@ public class CatsUnl extends AbstractFlyweightUnlArgumentProvider implements Gra
return new UnlDocumentNode(UnlDocumentNodeType.Sentence, "[S:00]", graph);
}
private static UnlDocumentNode newBuilder(){
return UnlDocumentNodeBuilder
.createBuilder()
.withNodeType(UnlDocumentNodeType.Sentence)
.withLabel("[S:00]")
.withGraph(
gb ->
gb.addGraphRelation(
grb -> grb
.label("mod")
.reference(":01")
.leftNode(
lnb ->
lnb
.universalWord(
uwb ->
uwb
.headWord("cat1")
.addTernaryTransitiveRestriction("icl", "feline", "thing", RestrictionOrientation.Right)
)
.instanceNumber(":01")
.addAttribute(".@entry")
.addAttribute(".@def")
)
.rightNode(
rnb -> rnb
.universalWord(
uwb ->
uwb
.headWord("black")
.addSimpleRestriction("icl", RestrictionOrientation.Right, "adj")
)
)
)
.addGraphRelation(grb ->
grb.label("and")
.reference(":01")
.leftNode(
lnb -> lnb.universalWord(
uwb -> uwb.headWord("cat1")
.addTernaryTransitiveRestriction("icl", "feline", "thing", RestrictionOrientation.Right)
)
.instanceNumber(":01")
.addAttribute(".@entry")
.addAttribute(".@def")
)
.rightNode(
lnb -> lnb.universalWord(
uwb -> uwb.headWord("cat2")
.addTernaryTransitiveRestriction("icl", "feline", "thing", RestrictionOrientation.Right)
)
.instanceNumber(":02")
.addAttribute(".@entry")
.addAttribute(".@def")
)
)
.addGraphRelation(
grb -> grb.label("mod")
.reference(":01")
.leftNode(
lnb -> lnb.universalWord(
uwb -> uwb.headWord("cat2")
.addTernaryTransitiveRestriction("icl", "feline", "thing", RestrictionOrientation.Right)
)
.instanceNumber(":02")
.addAttribute(".@entry")
.addAttribute(".@def")
)
.rightNode(
rnb -> rnb.universalWord(
uwb -> uwb.headWord("white")
.addSimpleRestriction("icl", RestrictionOrientation.Right, "adj")
)
)
)
.addGraphRelation(
grb -> grb.label("agt")
.noReference()
.leftNode(
lnb -> lnb.universalWord(
uwb -> uwb.headWord("eat")
.addTernaryTransitiveRestriction("icl", "consume", "do", RestrictionOrientation.Right)
.addSimpleRestriction("agt", RestrictionOrientation.Right, "living_thing")
.addSimpleRestriction("obj", RestrictionOrientation.Right, "concrete_thing")
.addSimpleRestriction("ins", RestrictionOrientation.Right, "thing")
)
.addAttribute(".@entry")
.addAttribute(".@pl")
.addAttribute(".@present")
.addAttribute(".@progress")
)
.rightSubGraphNode(
rnb -> rnb.referenceNumber(":01")
.addAttribute(".@_hn-scope")
)
)
)
.buildUnlDocumentNode();
}
public static UnlDocumentNode buildSentenceNode() {
return newBuilder();
}
protected List<UnlDocument> buildUnlDocuments() {
var unlDocument = new UnlDocument("[D]");
......
......@@ -93,4 +93,19 @@ public class UnlSentences {
"{/unl}\n" +
"[/S]\n" +
"[/D]";
public static final String Issue13 = "[S:00]\n" +
"{org:en}\n" +
"(M) = Mandatory:The requirement shall be respected in every ETCS application. \n" +
"{/org}\n" +
"{unl}\n" +
"mod(m(icl>metric_linear_unit>thing,equ>meter).@entry.@parenthesis,=)\n" +
"mod(=,mandatory(icl>adj,equ>compulsory).@maiuscul)\n" +
"obj(respect(icl>consider>be,rsn>thing,obj>thing,aoj>person).@present.@promise,requirement(icl>duty>thing).@def.@topic)\n" +
"cnt(mandatory(icl>adj,equ>compulsory).@maiuscul,respect(icl>consider>be,rsn>thing,obj>thing,aoj>person).@present.@promise)\n" +
"mod(application(icl>information,equ>request,agt>thing,obj>uw),every(icl>quantity,per>thing))\n" +
"mod(application(icl>information,equ>request,agt>thing,obj>uw),etcs)\n" +
"scn(respect(icl>consider>be,rsn>thing,obj>thing,aoj>person).@present.@promise,application(icl>information,equ>request,agt>thing,obj>uw))\n" +
"{/unl}\n" +
"[/S]\n";
}
package fr.tetras_libre.unltools.unl.parser;
import fr.tetras_libre.unltools.unl.Issue13Unl;
import fr.tetras_libre.unltools.unl.UnlSentences;
public class Issue13WithExpectedUnlDocumentChecker extends SentenceWithExpectedUnlDocumentsChecker {
public Issue13WithExpectedUnlDocumentChecker() {
super(UnlSentences.Issue13, new Issue13Unl().getUnlDocuments());
}
}
......@@ -14,13 +14,14 @@ class UnlParserTest {
private static Stream<SentenceWithExpectedUnlDocumentsChecker> unlParserShouldParseSentenceAndGetExpectedUnlDocuments() {
return Stream.of(new R1SentenceWithExpectedUnlDocumentsChecker(),
new R2SentenceWithExpectedUnlDocumentsChecker(),
new CatsSentenceWithExpectedUnlDocumentsChecker(),
new AllSentenceWithExpectedUnlDocumentsChecker());
new R2SentenceWithExpectedUnlDocumentsChecker(),
new CatsSentenceWithExpectedUnlDocumentsChecker(),
new AllSentenceWithExpectedUnlDocumentsChecker(),
new Issue13WithExpectedUnlDocumentChecker());
}
@ParameterizedTest
@ValueSource(strings = {UnlSentences.R1, UnlSentences.R2, UnlSentences.Cats, UnlSentences.All})
@ValueSource(strings = {UnlSentences.R1, UnlSentences.R2, UnlSentences.Cats, UnlSentences.All, UnlSentences.Issue13})
void unlParserShouldParseSimpleSentenceWithoutException(String input) {
UnlParserFactory unlFactory = new UnlParserFactoryFromString(input);
var wrapper = createAndCheckUnlParserValidity(unlFactory);
......