package semantics;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.junit.BeforeClass;
import org.junit.Test;
import semantics.err.UnsatisfiableQueryException;
import semantics.Individuals;
import semantics.KnowBase;
import semantics.model.*;
import static org.junit.Assert.*;


public class KnowBaseTest {
  private static KnowBase kb;

  @BeforeClass
  public static void setUpClass() {
    kb = KnowBase.of("music.rdf");
  }


  @Test
  public void cache() {
    KnowBase music = KnowBase.of("music.rdf");
    assertSame(kb, music);
    assertEquals(kb, music);

    KnowBase wine = KnowBase.of("wine.rdf");
    assertNotSame(kb, wine);
    assertNotEquals(kb, wine);
  }


  private static void isSub(boolean expected, Conceptual sup, Conceptual sub) {
    assertEquals(expected, kb.isSubtype(sup, sub));
  }

  @Test
  public void subtype() {
    isSub(true,  new Concept(":MusicArtist"), new Concept(":MusicArtist"));
    isSub(true,  new One    (":hendrix"    ), new Concept(":MusicArtist"));
    isSub(false, new Concept(":MusicArtist"), new One    (":hendrix"    ));
  }


  private static void isMember(boolean expected, Conceptual c, String iri) {
    assertEquals(expected, kb.isMember(c, kb.nominal(iri)));
  }

  @Test
  public void member() {
    isMember(true, new Concept(":MusicArtist"), ":beatles");
    isMember(true, new Concept(":MusicArtist"), ":hendrix");

    Conceptual inf = new Existence(new Role(":influencedBy"), new Everything());
    isMember(false, inf, ":beatles");
    isMember(true,  inf, ":hendrix");
  }


  private static void nominalIs(String expected, String actual) {
    assertEquals(expected, kb.nominal(actual).getIri());
  }

  @Test
  public void nominal() {
    nominalIs(":hendrix", ":hendrix");
    nominalIs(":hendrix", "http://example.org/music#hendrix");
    nominalIs("<what:ever>", "what:ever");
    nominalIs("<http://example.org/what#ever>", "http://example.org/what#ever");
  }


  private static void queryIs(Conceptual c, String... iris) {
    assertTrue(kb.isSatisfiable(c));
    assertEquals(new HashSet<>(Arrays.asList(iris)), kb.query(c).getIris());
  }

  @Test
  public void everythingQuery() {
    queryIs(new Everything(), ":beatles", ":coolFm", ":hendrix", ":machineGun");
  }

  @Test
  public void conceptQuery() {
    queryIs(new Concept(":MusicArtist"), ":beatles", ":hendrix");
  }

  @Test
  public void nestedQuery() {
    Conceptual inf = new Existence(new Role(":influencedBy"), new Everything());
    queryIs(inf, ":hendrix");
  }

  @Test(expected = UnsatisfiableQueryException.class)
  public void unsatisfiableQuery() {
    Conceptual nothing = new Nothing();
    assertFalse(kb.isSatisfiable(nothing));
    kb.query(nothing);
  }


  private static void projectIs(Individual i, Roleish r, String... iris) {
    assertEquals(new HashSet<>(Arrays.asList(iris)), i.project(r).getIris());
  }

  @Test
  public void project() {
    projectIs(kb.nominal(":beatles"), new Role(":influencedBy"));
    projectIs(kb.nominal(":hendrix"), new Role(":influencedBy"), ":beatles");
  }
}

@@ -193,38 +193,8 @@
 on;%0A
-import semantics.Individuals;%0A
 impo
@@ -276,16 +276,61 @@
 sert.*;%0A
+import static semantics.Individuals.getIris;%0A
 %0A%0Apublic
@@ -2134,16 +2134,24 @@
 iris)), 
+getIris(
 kb.query
@@ -2153,25 +2153,16 @@
 query(c)
-.getIris(
 ));%0A  %7D%0A
@@ -2886,16 +2886,24 @@
 iris)), 
+getIris(
 i.projec
@@ -2910,17 +2910,8 @@
 t(r)
-.getIris(
 ));%0A

Fix up tests to use proper Individuals methods